JUnit5 Test Suites

September 30, 2020 | No comments

JUnit 5 Test Suites

The test suite is a container that has a set of tests that can be executed together. For instance, say we have multiple tests and want to execute all of them or subset of them under certain conditions. Like we want to run all the tests in the integration stage, a subset of tests in the developer environment before pushing code. All these can be done using Test Suite. Using JUnit 5 test suites, we can run tests spread into multiple test classes and different packages. JUnit 5 provides two annotations: @SelectPackages and @SelectClasses to create test suites. Additionally, you can use other annotations for filtering test packages, classes, or even test methods.

Filtering Tests in Suites

Tests in JUnit5 can be executed with @RunWith. The same annotation is also used for Junit 4. The only difference Is in Junit 5 we pass JUnitPlatform as a parameter. In this article we will go though some sample use cases of creating TestSuite in JUnit 5 and how to filter test classes, test methods in the test suite etc.

@SelectPackages

With this annotation, we can tell the Test Suite to scan all the mention packages and its sub-packages and execute all the Test Classes. We can also specify a signal package or multiple packages. Following example demonstrates this.
Select a single package
With @SelectPackages we can select test classes only from a specified package or list of packages. In the following example, all the Test classes from package com.bootng will be executed.
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@SelectPackages({ "com.bootng" })
public class TestSuiteWithPackageSingle {

}
Select multiple packages
Similarly, if we want to support multiple packages then we can specify multiple package names. The following example will execute tests from the two packages.
@RunWith(JUnitPlatform.class)
@SelectPackages({ "com.bootng.assertions", "com.bootng.display" })
public class TestSuiteWithPackageMultiple {

}

@SelectClasses

Select Two Classes
In this example rather than selecting packages, we can specify individual test classes that we want to include in the Test Suite. In the following example essentially we will have only two Test classes in the Test Suite.
@RunWith(JUnitPlatform.class)
@SelectClasses({ DisplayExampleTest.class, DisplayGeneratorExampleTest.class })
public class TestSuiteWithSelectClass {

}

@IncludePackages and @ExcludePackages

@IncludePackages and @ExcludePackages work only at the specified package level and it won't scan sub-packages. This annotation is used with @SelectPackages annotation to fine-tune the filtering. Son in the following example Test Classes from only com.bootng.assertions will be included.
IncludePackages Example
Here
@RunWith(JUnitPlatform.class)
@SelectPackages({ "com.bootng" })
@IncludePackages({ "com.bootng.assertions" })
public class TestSuite_IncludePackage {

}
ExcludePackages Example
Includes all the test form package com.bootng, but excludes tests from sub package com.bootng.assertions.
@RunWith(JUnitPlatform.class)
@SelectPackages({ "com.bootng" })
@IncludePackages({ "com.bootng.assertions" })
public class TestSuite_IncludePackage {

}

@IncludeClassNamePatterns and @ExcludeClassNamePatterns

Rather than filtering the test class from packages, we can also filter them based on the name of the Test Classes. To specify test class names patterns to exclude or include, you can use @IncludeClassNamePatterns and @ExcludeClassNamePatterns annotations.
IncludeClassNamePatterns Example
In this example, we can including only the test whose name ends with "Test" from the package (and sub packages) of com.bootng
@RunWith(JUnitPlatform.class)
@SelectPackages({ "com.bootng" })
@IncludeClassNamePatterns("^.*Test*$")
public class TestSuite_NamePattern {

}

@IncludeTags and @ExcludeTags

IncludeTags Example
With IncludeTags and Exclude Tags we can filter Classes or Methods that are Tagged with the specified tag. For Instance, the following example, we are scanning package com.bootng.assertions and then only interested in including Test Classes or Test Methods tagged with Weather.
@RunWith(JUnitPlatform.class)
@SelectPackages({ "com.bootng.assertions" })
@IncludeTags({ "Weather" })
public class TestSuite_TagInclude {

}
ExcludeTags Example
Similar to IncludeTags tag to filter Test Methods or Test Classes but for exclusion. The following example will exclude all the Tests tagged with "Weather"
@RunWith(JUnitPlatform.class)
@SelectPackages({ "com.bootng.assertions" })
@ExcludeTags({ "Weather" })
public class TestSuite_TagExclude {

}

Summary

JUnit 5 provides powerful ways to filter Test cases in Test Suites. We have several options starting from including/excluding a specific packages to include/exclude specific Classes to Include/Exclude specific methods.

No comments :

Post a Comment

Please leave your message queries or suggetions.