How can we automatically only run tests on newer A

2019-07-15 00:42发布

问题:

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.