简体   繁体   中英

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

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 eg

@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.

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

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