Reputation: 53
i am using the Discovering Tests feature of Junit and i also have a class that introduces a few @TestFactory
methods each of which produces a great amount of dynamic tests. My first thought was "It would be great to be able to run these concurrently.", so i added
.configurationParameter(
"junit.jupiter.execution.parallel.enabled", "true"
)
to my LauncherDiscoveryRequestBuilder
and i annotated my test class holding the factories with @Execution(ExecutionMode.CONCURRENT)
. So far so good, but i ended up with each @TestFactory
method being run on a different thread but all the dynamic tests produced by a single @TestFactory
share the same thread (the thread the @TestFactory
that produced these tests runs on).
My question is - can i run the dynamic tests produced by a @TestFactory in parallel ? Also tried this
@TestFactory
@Execution(ExecutionMode.CONCURRENT)
but the result was the same.
Upvotes: 5
Views: 2161
Reputation: 31247
Yes, you can execute dynamic tests concurrently.
Here's a modified version of the dynamicNodeSingleContainer()
test factory from the JUnit 5 User Guide
@TestFactory
@Execution(ExecutionMode.CONCURRENT)
DynamicNode dynamicNodeSingleContainer() {
return dynamicContainer("palindromes",
Stream.of("racecar", "radar", "mom", "dad")
.map(text -> dynamicTest(text, () -> {
assertTrue(isPalindrome(text));
System.err.println(Thread.currentThread().getName());
})
));
}
When I execute it, I see the following output which demonstrates that the 4 dynamic tests executed concurrently in different threads.
ForkJoinPool-1-worker-5
ForkJoinPool-1-worker-11
ForkJoinPool-1-worker-7
ForkJoinPool-1-worker-9
Upvotes: 8