Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio - run same test case(s) several times in loop

Before submitting my test cases, I want to make sure they are running stably. Is there any way in Android Studio to run the same test case / class in loop for several times?

like image 563
user846316 Avatar asked Oct 31 '22 04:10

user846316


2 Answers

Annotate your test with @FlakyTest. See http://developer.android.com/reference/android/test/FlakyTest.html

For instance

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

Update: I see you're using Espresso. Then... no, this is not supported by android-test-kit, unfortunately. But here's the feature request: https://code.google.com/p/android-test-kit/issues/detail?id=153

like image 117
espinchi Avatar answered Nov 11 '22 13:11

espinchi


Use parameterized JUnit tests with several instances of empty parameter set:

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

A parameterized test class runs all its test methods once for every item in the method marked with the @Parameters annotation. It is normally used to run a test with different initial values, but if there are no values to set up, the test is simply repeated as many times as you want.

The test will pass only if all the instances pass.

like image 28
ris8_allo_zen0 Avatar answered Nov 11 '22 12:11

ris8_allo_zen0