Wednesday, August 9, 2017

Android JUnit @BeforeClass flakiness for longer running routines

Using @BeforeClass in a JUnit test seems like a great way to make tests run more efficiently. Surely it's a natural place to put long-running setup routines!

Unfortunately, Android's test runner in Android Studio will, depending on how it feels, might run the next test, might skip the next test, might skip a couple more tests just for good measure, or it might just give up the tests entirely. Looking for an error message or a hint?

So instead of a nice elegant solution:
@BeforeClass
public static void someSlowSetupRoutine() {
    //run long running code
}
We must put the long running procedure in the test itself and use an if statement to avoid running it multiple times to speed things up:
@Test
public void someTest() {
    if(!ready){
        //run setup
    }
}




No comments:

Post a Comment