Reputation: 77
How to execute JUnit test suites with Gradle task?
P.S. I saw another threads with questions but it is not help.
Technology stack:
Configured test suite:
@Tag("parallel")
@Suite
@SelectClasses(OneTest.class)
public class OneSuite {
@Test
void method() {
System.out.println("execute: " + this.getClass().getSimpleName());
}
}
'OneSuite' configured for run test class 'OneTest.class'.
Test in suite:
public class OneTest {
@Test
void firstMethod() throws InterruptedException {
System.out.println("start " + this.getClass().getSimpleName() + ". Method one. Thread:" + Thread.currentThread().getName());
TimeUnit.MILLISECONDS.sleep(500);
System.out.println("end " + this.getClass().getSimpleName() + ". Method one. Thread:" + Thread.currentThread().getName());
}
}
I try gradle task:
tasks.register('parallelTests', Test) {
group 'tests'
description 'Parallel test execution'
useJUnitPlatform {
includeTags("parallel")
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
systemProperty("junit.jupiter.execution.parallel.enabled", true)
systemProperty("junit.jupiter.execution.parallel.mode.classes.default", "concurrent")
}
ignoreFailures = true
}
Task 'parallelTests' run olny test in suite 'method()' but doesn't run 'OneTest.class'
Console after run:
|-Test Results
|-OneSuite
|-method
I trying use annotation (@Tag()) on test classes without suit. Test executing normal. But when I delete tag from test and set tag to suite, gradle test running only in OneSuite, method(); do not run OneTest.class.
If running with IDE Intellij idea, suite executing good.
Upvotes: 1
Views: 452
Reputation: 77
The solution is use the folder location with suites but not a tag.
tasks.register('parallelTestSuites', Test) {
group 'tests'
description 'Parallel test execution'
useJUnitPlatform {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
}
systemProperty("junit.jupiter.execution.parallel.enabled", true)
systemProperty("junit.jupiter.execution.parallel.mode.classes.default", "concurrent")
ignoreFailures = true
include('com/myProject/somePackage/suites/**')}
Upvotes: 2
Reputation: 38639
This has nothing to do with Gradle, just JUnit.
Add the @Tag
to OneTest
and it should work.
The @Tag
is not inherited from the suite down to the selected classes.
The suite just says "these classes are going to be considered", but as you filter for a specific tag, then only the tests that actually have that tag are executed.
If you run from IntelliJ without Gradle delegation, you do not filter for the tag and thus both tests are executed.
Upvotes: 2