5

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?

user846316
  • 6,037
  • 6
  • 31
  • 40
  • 1
    I don't think IJ supports that. If you don't want to touch your test code, you could do `set -e; for i in {1..20}; do ./gradlew connectedAndroidTest; done`. – Daniel Lubarov May 11 '15 at 18:45

4 Answers4

2

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

espinchi
  • 9,144
  • 6
  • 58
  • 65
2

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.

ris8_allo_zen0
  • 1,537
  • 1
  • 15
  • 35
1

If you want to run a test couple of times to see if its stable, In Android studio you can repeat a test by mentioning that in Run/Debug configurations for the test enter image description here

KingKongCoder
  • 600
  • 4
  • 15
  • 1
    As mentioned in https://stackoverflow.com/a/46900137/7548103, the "Repeat" option is only available for "Java Unit Tests" (and doesn't work for "Java Instrumented Tests"). – Darwin Huang Jan 12 '22 at 18:04
-3

Just use cycle FOR. For example:

@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;
}
  • 1
    That is not correct! Before every test, some setup/config is to be executed. With your suggestion, it will be executed only once and then the test will just run. Secondly, once the test fails, it will exit. It won't keep on running for more iterations. Thirdly, this is to check the stability of test run and has to be executed on the actual test code which is going to be submitted. – user846316 Sep 11 '17 at 07:49
  • It was kind of inferred, @Konstantin :) – Martin Marconcini Nov 02 '18 at 23:14