1

Currently I am writing some dynamic tests in Junit5 and I have questions regarding parallelism.

Is it possible to run dynamic tests in parallel, while factory methods are being executed sequentially? And opposite question: is it possible to run dynamic tests sequentially while factory methods are being executed in parallel?

1st question work-Flow:

  1. "1" factory method is executed -> dynamic tests of "1" factory method is run in parallel.
  2. "2" factory method is executed once "1"factory dynamic tests are completed. -> Dynamic tests inside "2" factory method is executed in parallel.
  3. ...

2nd question work-Flow:

  1. All 3 factory methods (factory1, factory2, factory3) are launch in parallel -> Dynamic tests inside factory methods are executed sequentially.

Code example:

@TestFactory
Stream<DynamicNode> factory1() {
    return Stream.of(10, 30, 50)
            .map(input -> dynamicContainer("Container ("+input+")", Stream.of(
                    dynamicTest("Test 1", () ->
                                assertTrue(input > 20))
                            ,
                            dynamicTest("Test 2", () ->
                                assertTrue(input > 100))
            )));
}

@TestFactory
Stream<DynamicNode> factory2() {
    return Stream.of(50, 80, 100)
            .map(input -> dynamicContainer("Container ("+input+")", Stream.of(
                    dynamicTest("Test 1", () ->
                            assertTrue(input < 20))
                    ,
                    dynamicTest("Test 2", () ->
                            assertTrue(input < 100))
            )));
}

@TestFactory
Stream<DynamicNode> factory3() {
    return Stream.of(50, 80, 100)
            .map(input -> dynamicContainer("Container ("+input+")", Stream.of(
                    dynamicTest("Test 1", () ->
                            assertTrue(input = 20))
                    ,
                    dynamicTest("Test 2", () ->
                            assertTrue(input = 100))
            )));
}

If I put annotation @Execution(ExecutionMode.CONCURRENT)/@Execution(ExecutionMode.SAME_THREAD), it affects both factory method itself and stream inside.

Thank you very much!

Ign
  • 31
  • 2

0 Answers0