2016-08-28 187 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()拋出InterruptedException' – piotrek1543

+0

@ piotrek1543的主要問題是,我無法找到一種方式來運行測試特濃多次(環路)。我該怎麼做? – 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

解決方案由@mklimek in Kotlin提供。

如何使用:

@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

的Android Studio允許您運行測試了N次的運行,直到它失敗。點擊您的測試規則的「編輯配置」並搜索「重複:」設置。

Screenshot