Parallel tests based on tags or suites #4066
-
Hi, I was looking for parallel test execution based on tags or suites, similar to Junit 4 suites, suitesAndClasses and suitesAndMethods or TestNG tests, but I couldn't find any information about it. If there any way of having this kind of parallelization in Junit 5? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Have you seen the Parallel Execution section in the User Guide? You can, e.g., run classes in parallel but methods inside each class sequentially using this configuration:
|
Beta Was this translation helpful? Give feedback.
-
It's possible to follow the Parallel Execution guide-lines and use @ResourceLock("tag ...") to tag the test-classes. It's a compromise that would allow A1Test and A2Test in a sequence that executes in parallel with another sequence containing B1Test and B2Test. @ResourceLock("tag A")
class A1Test extends DelayBeforeAndAfter {
@Test void a1foo() {}
@Test void a1bar() {}
}
@ResourceLock("tag A")
class A2Test extends DelayBeforeAndAfter {
@Test void a2foo() {}
@Test void a2bar() {}
}
@ResourceLock("tag B")
class B1Test extends DelayBeforeAndAfter {
@Test void b1foo() {}
@Test void b1bar() {}
}
@ResourceLock("tag B")
class B2Test extends DelayBeforeAndAfter {
@Test void b2foo() {}
@Test void b2bar() {}
}
@Execution(ExecutionMode.CONCURRENT)
class DelayBeforeAndAfter {
static final long startTime = System.currentTimeMillis();
String time() {
return new java.text.DecimalFormat(" 0.000s").format(
(System.currentTimeMillis() - startTime) / 1000.0);
}
@BeforeEach void recordStartTime(TestInfo info) throws Exception {
System.out.println(info.getDisplayName() + " started at " + time());
Thread.sleep(500);// Sleep 0.5 s
}
@AfterEach void recordEndTime(TestInfo info) throws Exception {
Thread.sleep(500);// Sleep 0.5 s
System.out.println(info.getDisplayName() + " ended at " + time());
}
} I had the above executed with this suite-definition ... @Suite
@SelectClasses({A1Test.class,A2Test.class,B1Test.class,B2Test.class})
public class ParallelSuite {} ... to produce this output:
The timestamps reveal that there are at most one a-test and one b-test running in parallel at any moment. I.e. "tag A"-tests run in one sequence that executes in parallel with a sequence that carries all the "tag B"-tests! (But neither sequence uses any dedicated thread for executing its tests.) |
Beta Was this translation helpful? Give feedback.
I'm afraid that's currently not possible since the Suite engine does not support executing suite classes in parallel.