2016-08-28 4 views
4

가끔은 제 신청서에 드문 버그가 있습니다. 하지만 매우 희귀하기 때문에 재현 할 수는 없습니다. 그래서 간단 에스프레소 테스트를하기로했습니다.에스프레소 테스트를 여러 번 실행하십시오.

@RunWith(AndroidJUnit4::class) 
@LargeTest 
class MainActivityTest { 

    val password = "1234" 

    @Rule @JvmField 
    var mActivityRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java) 

    @Test 
    fun checkNotesListNotEmpty() { 
     onView(withId(R.id.password_edit_text)).perform(typeText(password)) 
     onView(withId(R.id.notes_recycler_view)).check { view, noMatchingViewException -> 
      if (noMatchingViewException != null) throw noMatchingViewException 
      assertThat((view as RecyclerView).adapter.itemCount, Matchers.`is`(1)) 
     } 
    } 
} 

어떻게 테스트를 중단하고 일치가 실패하면 멈출 수 있습니까?

+0

'재미 checkNotesListNotEmpty 시도는() piotrek1543 주요 문제 @ InterruptedException' – piotrek1543

+0

내가 (루프) 에스프레소 테스트를 여러 번 실행하는 방법을 찾을 수 있다는 것입니다 발생합니다. 내가 어떻게 해? – Alexandr

답변

5

사용 @Repeat 주석 :

@Test 
@Repeat(100) 
fun checkNotesListNotEmpty() { 
} 

그러나 당신은 스스로 구현해야한다 :

Repeat.java을 :

import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 
import static java.lang.annotation.ElementType.METHOD; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 
import java.lang.annotation.Target; 

@Retention(RetentionPolicy.RUNTIME) 
@Target({ METHOD, ANNOTATION_TYPE }) 
public @interface Repeat { 
    int value() default 1; 
} 

RepeatRule.java :

import org.junit.rules.TestRule; 
import org.junit.runner.Description; 
import org.junit.runners.model.Statement; 

public class RepeatRule implements TestRule { 

    private static class RepeatStatement extends Statement { 
     private final Statement statement; 
     private final int repeat;  

     public RepeatStatement(Statement statement, int repeat) { 
      this.statement = statement; 
      this.repeat = repeat; 
     } 

     @Override 
     public void evaluate() throws Throwable { 
      for (int i = 0; i < repeat; i++) { 
       statement.evaluate(); 
      } 
     } 

    } 

    @Override 
    public Statement apply(Statement statement, Description description) { 
     Statement result = statement; 
     Repeat repeat = description.getAnnotation(Repeat.class); 
     if (repeat != null) { 
      int times = repeat.value(); 
      result = new RepeatStatement(statement, times); 
     } 
     return result; 
    } 
} 
+0

감사합니다. 감사합니다. – Alexandr

+1

@Alexandr 확인 했습니까? 이 사이트를 더 좋게 만드는 몇 가지 대답을 수락하십시오 :) – mklimek

+0

나에게 며칠주세요 :) – Alexandr

1

수 너야. 그 하나를 반복하는 별도의 테스트가 있습니까? (해당되는 경우) :

@RunWith(AndroidJUnit4::class) 
@LargeTest 
class MainActivityTest { 
    ... 

    @Test fun checkNotesListNotEmpty() {...} 

    @Test fun loopCheckNotesListNotEmpty() { 
     while(true) 
      checkNotesListNotEmpty() 
    } 
} 
0

솔루션은 Kotlin의 @mklimek에서 제공됩니다.

는 사용 방법 :

@Rule @JvmField 
var repeatRule: RepeatRule = RepeatRule() 

@Test 
@RepeatTest(100) 
fun checkNotesListNotEmpty() { 

RepeatRule.kt을

class RepeatRule : TestRule { 

    private class RepeatStatement(private val statement: Statement, private val repeat: Int) : Statement() { 
     @Throws(Throwable::class) 
     override fun evaluate() { 
      for (i in 0..repeat - 1) { 
       statement.evaluate() 
      } 
     } 
    } 

    override fun apply(statement: Statement, description: Description): Statement { 
     var result = statement 
     val repeat = description.getAnnotation(RepeatTest::class.java) 
     if (repeat != null) { 
      val times = repeat.value 
      result = RepeatStatement(statement, times) 
     } 
     return result 
    } 
} 

RepeatTest.kt

@Retention(AnnotationRetention.RUNTIME) 
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.ANNOTATION_CLASS) 
annotation class RepeatTest(val value: Int = 1) 
0

안드로이드 스튜디오가 실패 할 때까지 테스트 N 시간을 실행하거나 실행할 수 있습니다 . 테스트 규칙에 대해 "구성 편집"을 클릭하고 "반복 :"설정을 검색하십시오.

Screenshot

관련 문제