我们如何自动仅在较新的Android版本上运行测试? [英] How can we automatically only run tests on newer Android versions?
问题描述
我发现,特别是在KitKat之前的设备上,一项测试始终失败.我相信这与旧版Android设备上使用的嵌入式WebView的更改有关(但也许这是错误的).无论如何,回到我的问题:我想要一种优雅的方式来控制是否根据设备上运行的Android版本运行测试.
I discovered that one test, in particular, fails consistently on pre-KitKat devices. I believe it's related to the change in embedded WebView used on older Android devices (but perhaps this is mistaken). Anyway, back to my question: I'd like an elegant way to control whether a test is run depending on the version of Android that's running on the device.
当前,我使用使测试短路的代码,如果运行时早于KitKat,则可以通过测试.这是相关的代码:
Currently, I use code that short-circuits the test and passes it if the runtime is earlier than KitKat. Here's the relevant code:
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
assertTrue("Skipping this test as the Android version is too low.", true);
return;
}
此后,我尝试依次使用两个注释:@TargetApi
和@RequiresApi
例如
I've since tried to use two annotations in turn: @TargetApi
and @RequiresApi
e.g.
@Test
@RequiresApi(Build.VERSION_CODES.KITKAT)
public void zimTest() {
Log.v("kiwixTesting", "Running zimTest() on Android version: " + Build.VERSION.SDK_INT);
...
}
在这两种情况下,测试都是在我的测试设备(包括Android 4.3、4.3、4.4和更高版本)上运行的.我可以告诉您,因为测试运行程序显示测试已成功运行,并且以下日志消息的输出
Log.v("kiwixTesting", "Running zimTest() on Android version: " + Build.VERSION.SDK_INT);
出现在日志中.
In both cases, the test was run on my test devices (which include Android 4.3, 4.3, 4.4, and newer versions). I can tell because the test runner shows the test was run successfully, and the output of the following log message
Log.v("kiwixTesting", "Running zimTest() on Android version: " + Build.VERSION.SDK_INT);
appears in the log.
The full code is here and I'm tracking the work here
有人能提出比我更好的方法吗?谢谢.
Can anyone suggest a better approach than mine please? Thank you.
推荐答案
You might wanna take a look at SdkSuppress annotation. It has two methods -- maxSdkVersion and minSdkVersion which you could use depending on your need.
这篇关于我们如何自动仅在较新的Android版本上运行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!