本文介绍了多次运行浓缩咖啡测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有时我在应用程序中遇到罕见的错误。但我不能复制它,因为它非常罕见。因此,我决定编写简单的浓缩咖啡测试:
@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
批注:
@RunWith(AndroidJUnit4.class)
public class MyTest {
@Rule
public RepeatRule repeatRule = new RepeatRule();
@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;
}
}
这篇关于多次运行浓缩咖啡测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!