简体   繁体   中英

Run Android test on two (or more) Android versions with Robolectric

I'm working on Android code that includes a differentiation between two Android versions.

I wrote tests to run the code. I'm using Robolectric.

I found a SO answer that says how to configure the version that Robolectric uses for running the test:
Does Robolectric support API level?
But that way I'd have to copy the test and use different annotations (or rather have the test code in a private method and create two new public test methods that call the private method - but I've already got hundreds of tests, so this would be lots of work).

Is there any way to tell Robolectric to run a test twice, with different Android versions for each run?

I know your answer has been posted a year ago but I had the same problem and I found no answer on the web.

I found a solution with Robolectric 3.0 : you can use the class MultiApiRobolectricTestRunner . For example :

@RunWith(MultiApiRobolectricTestRunner.class)
@Config(constants = BuildConfig.class, manifest=Config.NONE, sdk = {16, 19, 21})
public class MyTestCase extends TestCase {

    @Test
    public void test1ToRunOnMultipleApis(){
        // My test
    }
}

The method test1ToRunOnMultipleApis will be tested 3 times : with API 16, 19 and 21.

Hope this can help ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM