Sub topic 10
Skipping, Tagging, and Conditional Tests
Sometimes you do not want every test to run every time. A test might be broken while you fix a bug. A slow test might not need to run on every save. A test might only make sense on Windows. JUnit 5 gives you three tools for these cases: @Disabled, @Tag, and assumptions.
Turning a Test Off with @Disabled
An annotation is a label that starts with @ and tells JUnit how to treat a method. @Disabled tells JUnit to skip a test. The test still shows up in the report, marked as skipped.
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
class DisabledExampleTest {
@Test
@Disabled("Waiting for bug #42 to be fixed")
void brokenFeature() {
assertEquals(1, 2);
}
@Test
void workingFeature() {
assertEquals(2, 1 + 1);
}
}Always write a reason inside the parentheses. Without a reason, people forget why the test was turned off.
Grouping Tests with @Tag
A tag is a short name you attach to a test so you can pick groups of tests to run. For example, you can mark slow tests as "slow" and quick ones as "fast".
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
class TaggedTest {
@Test
@Tag("fast")
void quickCheck() {
assertEquals(4, 2 + 2);
}
@Test
@Tag("slow")
void longCheck() throws InterruptedException {
Thread.sleep(2000);
assertEquals(9, 3 * 3);
}
}Next, tell your build tool which tags to run. In Gradle (Kotlin DSL):
tasks.test {
useJUnitPlatform {
includeTags("fast")
}
}In Maven, you can pass the tag on the command line:
mvn test -Dgroups=fastTo skip a tag, use excludeTags("slow") in Gradle or -DexcludedGroups=slow in Maven.
Running Tests Only When Conditions Hold
An assumption is a check that decides whether a test should keep going. If the assumption is false, JUnit stops the test and marks it as skipped, not failed. You can use one when a test needs something your machine might not have.
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.junit.jupiter.api.Test;
class AssumptionTest {
@Test
void runsOnlyOnCiServer() {
assumeTrue("true".equals(System.getenv("CI")));
assertEquals(10, 5 + 5);
}
}CI (continuous integration) is a server that builds and tests your code on every change. Many CI systems set an environment variable named CI.
JUnit also has ready-made condition annotations:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
class OsTest {
@Test
@EnabledOnOs(OS.WINDOWS)
void onlyOnWindows() {
// Windows-specific checks go here
}
}Which Tool to Use
| Goal | Tool |
|---|---|
| Turn off a broken test for now | `@Disabled("reason")` |
| Run only some groups of tests | `@Tag` plus build settings |
| Skip when the environment is wrong | `assumeTrue` or `@EnabledOnOs` |
Exercise
A test reads a file that only exists on your team's CI server. On a laptop it fails. Should you use @Disabled or an assumption, and why?
Show answer
Use an assumption, such as assumeTrue that the CI variable is "true". The test is then skipped on laptops and still runs on the CI server. @Disabled would turn it off everywhere.
Exercise
What is the difference between a failed assumption and a failed assertion?
Show answer
A failed assumption marks the test as skipped, because its conditions were not met. A failed assertion marks the test as failed, because the code did the wrong thing.