There is no built in support from gardle to run tag-specific junit 5 tests from gradle command. In this article we will see how do we achieve that run tag specific tests from command line.
We can achieve running tag-specific tests from command line using one simplest way that providing System Properties or run time parameters via command line to the gradle.
1. Run Junit 5 tag specific tests
Starting with Gradle version 4.6, Gradle provides native support for executing tests on the JUnit Platform. To include or exclude tag specific Junit 5 tests you need to include includeTags
and excludeTags
in build.gradle
like following example.
test { useJUnitPlatform { includeTags 'api & acceptance', 'accessibility' excludeTags 'regression', 'issue-1962' } }
In the above case for every build execute the same tests defined in build.gradle
. If we can override or provide include or exclude tags from gradle command line, we will get more control over execute tag specific test cases.
We can achieve this in one way by providing System Properties to the gradle. In following example we will see how to provide System Properties to the gradle.
2. Provide System Properties to gradle
Technologies used in following example :
- Gradle 6.1.1
- Junit 5.6.0
- Java 8
- Spring Tool Suite 3.9.8
2.1. Example Command :
$ gradle clean test -DincludeTags='regression' -DexcludeTags='accessibility'
2.2. build.gradle configuration
Following code demonstrates how to obtain System Properties in gradle and assign them to test build task. You must provide default value otherwise endup with build error if System Property is null
.
test { String itags = System.getProperty("includeTags") ? System.getProperty("includeTags") : 'regression'; String etags = System.getProperty("excludeTags") ? System.getProperty("excludeTags") : 'acceptance'; useJUnitPlatform{ includeTags itags excludeTags etags } }
2.3. A simple Junit 5 test class to test the config
@Tags(value = {@Tag("regression"), @Tag("user_module") }) public class Junit5_Tag_Test { @Tag("issue-19162") @Test void user_create_test() { System.out.println("user_create_test()"); } @Tag("issue-19163") @Test void user_login_sso() { System.out.println("user_login_sso()"); } @Tag("accessibility") @Test void user_accessability_test1() { System.out.println("accessibility_test1()"); } @Tag("security") @Test void user_security_test1() { System.out.println("security_test1()"); } @Tag("api") @Test void user_api_test1() { System.out.println("api_test1()"); } @Tag("api") @Test void user_api_test2() { System.out.println("api_test2()"); } @Tag("load") @Test void user_load_test1() { System.out.println("load_test1()"); } @Tag("performance") @Test void user_performance_test1() { System.out.println("performance_test1()"); } }
Test 1 :
$ gradle clean test -DincludeTags='regression' -DexcludeTags='accessibility' -i
You will see following output, note that this is partial output.
Junit5_Tag_Test > user_create_test() STANDARD_OUT user_create_test() Junit5_Tag_Test > user_performance_test1() STANDARD_OUT performance_test1() Junit5_Tag_Test > user_security_test1() STANDARD_OUT security_test1() Junit5_Tag_Test > user_login_sso() STANDARD_OUT user_login_sso() Junit5_Tag_Test > user_load_test1() STANDARD_OUT load_test1() Junit5_Tag_Test > user_api_test1() STANDARD_OUT api_test1() Junit5_Tag_Test > user_api_test2() STANDARD_OUT api_test2() Finished generating test XML results (0.006 secs) into: Z: ..... Finished generating test html results (0.015 secs) into: Z: ..... :test (Thread[Daemon worker Thread 10,5,main]) completed. Took 1.955 secs. BUILD SUCCESSFUL in 4s 5 actionable tasks: 5 executed
Test 2 :
$ gradle clean test -DincludeTags='performance | security' -DexcludeTags='acceptance' -i
You will see following output.
Junit5_Tag_Test > user_performance_test1() STANDARD_OUT performance_test1() Junit5_Tag_Test > user_security_test1() STANDARD_OUT security_test1() Finished generating test XML results (0.006 secs) into: Z: ..... Finished generating test html results (0.015 secs) into: Z: ..... :test (Thread[Daemon worker Thread 3,5,main]) completed. Took 1.896 secs. BUILD SUCCESSFUL in 4s 5 actionable tasks: 5 executed
3. Project properties
Similarly like System properties, you can obtain project properties and set them to project task in build.gradle
. Following code show you how to obtain project properties in build.gradle
and how to provide them via command line.
test { String itags = project.hasProperty("includeTags") ? project.property("includeTags") : 'regression'; String etags = project.hasProperty("excludeTags") ? project.property("excludeTags") : 'acceptance'; useJUnitPlatform{ includeTags itags excludeTags etags } }
Example Command :
$ gradle clean test -PincludeTags='performance | security' -PexcludeTags='acceptance' -i
4. Conclusion
In this article we have seen how to provide System Properties to the test build task and run tag-specific junit 5 tests from gradle command.
References
- Support for tag-specific gradle task
- Gradle Document
- Running-tests-build-gradle
- Create Gradle project in eclipse
- Junt 5 tags and filter test cases for execution
Thanks a lot for this blog entry! I tried without luck to parse a comma separated list of tags to include or exclude from command list such as -DincludeTags=mytag1,mytag2
Naively passing a list of string in the useJUnitPlatform fails when building the closure. Any chance you got that solved ?
Hi Guillaem Berche, yet there is no native support from Gradle for this scenario, but updating following code in build.gradle will work in both cases (tag expressions and comma separated values)… as like -PincludeTags=’mytag1, mytag2′ or -PincludeTags=’mytag1 | mytag2′
test {
String itag = project.hasProperty("includeTags") ?
project.property("includeTags") : 'regression';
String etag = project.hasProperty("excludeTags") ?
project.property("excludeTags") : 'acceptance';
String[] itags = itag.split(",");
String[] etags = etag.split(",");
useJUnitPlatform{
includeTags itags
excludeTags etags
}
}
Refer Section 3 in this guide.
Great, thanks a lot!
Guillaume.