Android Studio - 在循环中多次运行相同的测试用例

时间:2015-05-11 10:38:15

标签: android-studio android-espresso

在提交我的测试用例之前,我想确保它们运行稳定。 Android Studio中有没有办法在循环中运行相同的测试用例/类几次?

4 个答案:

答案 0 :(得分:2)

使用@FlakyTest注释您的测试。见http://developer.android.com/reference/android/test/FlakyTest.html

例如

@FlakyTest(tolerance = 3)
public void myTest() {
    // Test that sometimes fails for no good reason
}

更新:我看到你正在使用Espresso。然后......不,遗憾的是android-test-kit不支持。但这是功能请求:https://code.google.com/p/android-test-kit/issues/detail?id=153

答案 1 :(得分:2)

如果您想多次运行测试以查看其是否稳定,在 Android Studio 中,您可以通过在测试的运行/调试配置中提及来重复测试 enter image description here

答案 2 :(得分:1)

对多个空参数集实例使用parameterized JUnit测试:

@RunWith(Parameterized.class)
public class RepeatedTest {

    private static final int NUM_REPEATS = 10;

    @Parameterized.Parameters()
    public static Collection<Object[]> data() {
        Collection<Object[]> out = new ArrayList<>();
        for (int i = 0; i < NUM_REPEATS; i++) {
            out.add(new Object[0]);
        }
        return out;
    }

    @Test
    public void unstableTest() {
        // your test code here
    }
}

参数化测试类为标有@Parameters注释的方法中的每个项运行一次所有测试方法。它通常用于运行具有不同初始值的测试,但如果没有要设置的值,则可以根据需要重复测试多次。

只有在所有实例都通过时,测试才会通过。

答案 3 :(得分:-2)

只需使用周期FOR。例如:

@Test // test loop
    public void openApp() {
       int x;
        for(x=1; x < 3; x++) {
            PageObject open = new PageObject(driver);
            waitUntilElmntToBeClckbl(open.sqlApp);
            open.sqlApp.click();
            driver.navigate().back;
}