Skip to content

Conditional Test Execution

Marcel Schnelle edited this page Nov 19, 2019 · 10 revisions

One of the most exciting features of JUnit 5 is the functionality provided by its Conditional Test Execution annotations. At runtime, JUnit Jupiter evaluates external properties in order to make a decision about whether or not to run an annotated test method. This allows developers to write specific tests that only run on certain Java versions or operating systems, or depend on certain environment variables or system properties, for example.

Starting with version 1.2.0, the android-test-core library includes a couple of annotations that drive conditional test execution in scenarios tailored towards Android developers. The following sections introduce these annotations and their effect on the execution of instrumentation tests.

@EnabledOnManufacturer/@DisabledOnManufacturer

Skip or drive execution for manufacturer-specific test cases. This helps with testing bugs scoped towards a certain subset of devices. For example, you can run a test only on Samsung devices, or skip it if the test suite is executed on a Huawei device.

// Matches e.g. "Samsung", "SAMSUNG", "sAmSuNg" if ignoreCase == true (default),
// and "Samsung" only if ignoreCase == false
@EnabledOnManufacturer(value = "Samsung", ignoreCase = true)
@Test
void testForSamsungDevicesOnly() {
}

// Matches e.g. "Google", "GOOGLE", "gOoGlE" if ignoreCase == true (default),
// and "Google" only if ignoreCase == false
@DisabledOnManufacturer(value = "Google", ignoreCase = true)
@Test
void testForAllButGoogleDevices() {
}

If one of these annotations causes a test to be skipped, you can find the following line in the Logcat output:

W/AndroidJUnit5: testForSamsungDevicesOnly is ignored. Disabled on Manufacturer: Samsung

@EnabledOnSdkVersion/@DisabledOnSdkVersion

Skip or drive execution on specific API levels. This helps with testing apparent behavior on a certain range of OS versions. For example, you can run a test only on devices running Android P or newer, or skip it if the test suite is executed on a device older than Q.

// There is also an "until" parameter, indicating the highest API level where the test should be enabled
@EnabledOnSdkVersion(from = 28)
@Test
void testRunningOnPieAndAbove() {
}

// There is also a "from" parameter, indicating the lowest API level where the test should be disabled
@DisabledOnSdkVersion(until = 28)
@Test
void testNeverRunningOnPieAndBelow() {
}

If one of these annotations causes a test to be skipped, you can find the following line in the Logcat output:

W/AndroidJUnit5: testRunningOnPieAndAbove is ignored. Disabled on API 27
Clone this wiki locally