多次运行浓缩咖啡测试
有时我遇到了我的应用程序中的罕见错误。 但是我不能重现它,因为它非常罕见。 所以,我决定写简单的咖啡测试:
@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)) } } }
如何循环测试并在匹配失败时停止测试?
使用@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; } }
你不能有一个单独的测试循环那个? (如果是独一无二的情况):
@RunWith(AndroidJUnit4::class) @LargeTest class MainActivityTest { ... @Test fun checkNotesListNotEmpty() {...} @Test fun loopCheckNotesListNotEmpty() { while(true) checkNotesListNotEmpty() } }
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)
Android Studio允许您运行N次测试或运行,直到失败。 点击您的测试规则的“编辑配置”,并搜索“重复:”设置。