How can we automatically only run tests on newer Android versions?

微笑、不失礼 提交于 2019-12-10 22:24:55

问题


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.

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;
}

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);
    ...
}

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.


回答1:


You might wanna take a look at SdkSuppress annotation. It has two methods -- maxSdkVersion and minSdkVersion which you could use depending on your need.



来源:https://stackoverflow.com/questions/45865658/how-can-we-automatically-only-run-tests-on-newer-android-versions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!