JUnit Disable Enable Tests

Filed Under: JUnit

JUnit 5 Jupiter API provides various ways to disable or enable a test. There are various annotations to help us easily disable/enable tests based on OS, JRE, System properties etc. We can also execute a script to enable or disable a test. Let’s look at various JUnit annotations to enable and disable a test.

JUnit Disable Test Method and Class

JUnit @Disabled annotation is the easiest way to disable a test. It can be applied to a test method as well as on the Class itself. If @Disabled annotation is applied to the class, then all the tests present in the class gets disabled. We can also specify a reason message for documentation purposes.


@Test
@Disabled
void test() {
	assertTrue(3 > 0);
}

@Disabled("Explicitly Disabled")
class DisabledTests {
//all tests disabled
}

JUnit Disable/Enable Tests Based on Condition

JUnit Jupiter API provides useful annotations in org.junit.jupiter.api.condition package to enable or disable tests based on the specified condition. These annotations can be applied to test methods as well as the class itself. Let’s look at them one by one.

@DisabledOnOs and @EnabledOnOs

We can use these annotations to disable or enable a test based on the operating system. Internally, these annotations use System property “os.name” to find out the operating system information.


@Test
@DisabledOnOs(value= {OS.MAC, OS.WINDOWS})
void test2() {
	assertFalse(3 < 0);
}

@Test
@EnabledOnOs(value= {OS.MAC})
void test3() {
	assertTrue(System.getProperty("os.name").startsWith("Mac"));
}

@DisabledOnJre and @EnabledOnJre

We can use these annotations to disable/enable test based on the JRE version. Internally, these annotations use System property “java.version” to find out the operating system information.


@Test
@DisabledOnJre(value= JRE.JAVA_10)
void test4() {
	assertFalse(3 < 0);
}

@Test
@EnabledOnJre(value= JRE.JAVA_10)
void test5() {
	assertTrue(System.getProperty("java.version").startsWith("10"));
}

@DisabledIfEnvironmentVariable and @EnabledIfEnvironmentVariable

We can use these annotations with system environment properties. We need to specify the system property name and the regular expression to match them.

These methods use System.getenv(string name) to get the system property and match with the specified regex.


@Test
@DisabledIfEnvironmentVariable(named="USER", matches="pankaj")
void test6() {
	assertFalse(3 < 0);
}

@Test
@EnabledIfEnvironmentVariable(named="USER", matches="pankaj")
void test7() {
	assertTrue("pankaj".equals(System.getenv("USER")));
}

@DisabledIfSystemProperty and @EnabledIfSystemProperty

Similar to environment variables, these methods use system properties and specified regex to disable or enable tests.


@Test
//My System Properties "os.name" value is "Mac OS X"
@DisabledIfSystemProperty(named="os.name", matches="Mac.*")
void test8() {
	assertFalse(3 < 0);
}

@Test
//My System Properties "user.name" value is "pankaj"
@EnabledIfSystemProperty(named="user.name", matches="pankaj")
void test9() {
	assertTrue("pankaj".equals(System.getProperty("user.name")));
}

@DisabledIf and @EnabledIf

These annotations are used to execute a script to determine if the tests will be disabled or enabled. The default script engine is Oracle Nashorn; however, the engine attribute may be used to override the default script engine name.

An accessor provides access to a map-like structure via a simple String get(String name) method. The following property accessors are automatically available within scripts.

  • systemEnvironment: Operating system environment variable accessor
  • systemProperty: JVM system property accessor

Let's look at how we can use these annotations to enable and disable tests.


@Test
@DisabledIf("'pankaj' == systemEnvironment.get('USER')")
void test10() {
	assertFalse(3 < 0);
}

@Test
@EnabledIf("'pankaj' == systemProperty.get('user.name')")
void test11() {
	assertTrue("pankaj".equals(System.getProperty("user.name")));
}

JUnit Disable Tests Execution

When you will execute a test class with disabled methods, it will produce the following view.

junit disabled tests

Note that JUnit skips the test methods that are disabled. If you will Disable the whole test class, then the report will show all the test methods inside it as being skipped.

Summary

JUnit Jupiter API provides extensive support to disable or enable a test based on specified conditions. It's very useful when our test cases are dependent on external factors such as OS, Java Version etc.

You can check out the complete code from our JUnit 5 example project GitHub Repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages