简体   繁体   English

JUnit4参数化测试不同Android SDK版本

[英]JUnit4 parameterized test with different Android SDK versions

I'd like to test a function having a conditional statement for different Android SDK build versions with some parameters.我想测试一个 function 具有针对不同 Android SDK 构建版本的条件语句和一些参数。 The function looks like: function 看起来像:

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
    // do something with parameter(s) and return it;
} else {
    // do something with parameter(s) and return it;
}

I can use Parameterized.class runner or JUnitParams to get it run with a set of parameters.我可以使用Parameterized.class runner 或 JUnitParams 让它使用一组参数运行。 I can also create a single test function and can change the SDK_INT field and call the function to test.我还可以创建单个测试 function 并可以更改SDK_INT字段并调用 function 进行测试。 But I was not able to combine both approaches into the same unit test class.但是我无法将这两种方法结合到同一个单元测试 class 中。

Is there any annotation to set the SDK for a particular test method?是否有任何注释可以为特定的测试方法设置 SDK? Or what is the best approach to test functions like the above?或者测试上述功能的最佳方法是什么?

A simple yet not elegant solution is to use the Enclosed runner and inner classes changing the SDK_INT field in a function annotated with @BeforeClass .一个简单但不优雅的解决方案是使用封闭的运行器和内部类更改带有@BeforeClass注释的 function 中的SDK_INT字段。

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

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