简体   繁体   English

使用Robolectric在两个(或更多)Android版本上运行Android测试

[英]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. 我正在研究包含两个Android版本之间区别的Android代码。

I wrote tests to run the code. 我编写了测试以运行代码。 I'm using Robolectric. 我正在使用Robolectric。

I found a SO answer that says how to configure the version that Robolectric uses for running the test: 我找到了一个SO答案,说如何配置Robolectric用于运行测试的版本:
Does Robolectric support API level? Robolectric是否支持API级别?
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? 有没有办法告诉Robolectric运行两次测试,每次运行使用不同的Android版本?

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 . 我发现了Robolectric 3.0的解决方案:您可以使用类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. 方法test1ToRunOnMultipleApis将被测试3次:使用API test1ToRunOnMultipleApis和21。

Hope this can help ;) 希望这可以帮助;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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