【问题标题】:Run espresso test multiple times多次运行浓缩咖啡测试
【发布时间】:2016-08-28 16:13:06
【问题描述】:

有时我会在我的应用程序中遇到罕见的错误。但我无法复制它,因为它非常罕见。所以,我决定写一个简单的浓缩咖啡测试:

@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))
        }
    }
}

如何循环这个测试并在匹配失败时停止它?

【问题讨论】:

  • 试试fun checkNotesListNotEmpty() throws InterruptedException
  • @piotrek1543 主要问题是我找不到多次运行 Espresso 测试的方法(循环中)。我该怎么做?

标签: android kotlin android-espresso


【解决方案1】:

使用@Repeat注解:

@RunWith(AndroidJUnit4.class)
public class MyTest {

    @Rule
    public RepeatRule repeatRule = new RepeatRule();

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

但你必须自己实现它:

重复.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;
    }
}

【讨论】:

  • @Alexandr 你检查了吗?接受一些答案以使这个网站更好:)
  • 给我几天时间 :)
  • 也不适合我。在 Espresso 中完全被忽略了。
  • 记得添加@Rule注解。我忘了它无法理解为什么它不起作用......
【解决方案2】:

@mklimek 在 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
    }
}

重复测试.kt

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

【讨论】:

    【解决方案3】:

    你不能有一个单独的测试来循环那个测试吗? (如果是独一无二的情况):

    @RunWith(AndroidJUnit4::class)
    @LargeTest
    class MainActivityTest {
        ...
    
        @Test fun checkNotesListNotEmpty() {...}
    
        @Test fun loopCheckNotesListNotEmpty() {
            while(true)
                checkNotesListNotEmpty()
        }
    }
    

    【讨论】:

      【解决方案4】:

      Android Studio 允许您运行测试 N 次或运行直到失败。单击测试规则的“编辑配置”并搜索“重复:”设置。

      【讨论】:

      • 这仅适用于 java 单元测试而不是 Instrumented Tests。
      【解决方案5】:

      我遇到了同样的问题,因此我创建了一个库来多次运行测试:https://github.com/stepstone-tech/AndroidTestXRunner

      运行后,您可以检查测试结果是否有任何故障。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-15
        相关资源
        最近更新 更多