11

Is it possible to execute a test case parallel with JUnit 5?

I'm looking for something like threadPoolSize and invocationCount from TestNG:

@Test(threadPoolSize = 3, invocationCount = 10,  timeOut = 10000)
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
deamon
  • 89,107
  • 111
  • 320
  • 448

3 Answers3

22

You can write parallel tests with JUnit 5.3. https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution

@Execution(ExecutionMode.CONCURRENT)
class MyTest {

    @Test
    void test1() throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("Test1 " + Thread.currentThread().getName());
    }

    @Test
    void test2() throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("Test 2! " + Thread.currentThread().getName());
    }
}

// should run simultaneously 

Remember to add the junit.jupiter.execution.parallel.enabled=true to your JUnit configuration

https://junit.org/junit5/docs/current/user-guide/#running-tests-config-params

Add this to your JUnit configuration if you need a fixed thread pool:

junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=4
Mr.Turtle
  • 2,950
  • 6
  • 28
  • 46
2

As of writing, Parallel test execution is an experimental feature in Junit5:

https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-parallel-execution

Update the junit-platform.properties file:

Configuration parameters to execute top-level classes in parallel but methods in same thread:

junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = same_thread
junit.jupiter.execution.parallel.mode.classes.default = concurrent

Configuration parameters to execute top-level classes sequentially but their methods in parallel:

junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.mode.classes.default = same_thread

The default execution mode is applied to all nodes of the test tree with a few notable exceptions, namely test classes that use the Lifecycle.PER_CLASS mode or a MethodOrderer (except for Random). In the former case, test authors have to ensure that the test class is thread-safe; in the latter, concurrent execution might conflict with the configured execution order. Thus, in both cases, test methods in such test classes are only executed concurrently if the @Execution(CONCURRENT) annotation is present on the test class or method.

Saikat
  • 14,222
  • 20
  • 104
  • 125
1

For Maven:

// JUnit 5
@Execution(ExecutionMode.CONCURRENT)
abstract class BaseTest {
}


// pom.xml
<properties>
    <!-- CLI parameters -->
    <ignore-failure />
    <include-tags />
    <exclude-tags />
    <parallel-enabled />
    <thread-count />
</properties>

<build>
    <plugins>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M8</version>
            <configuration>
                <testFailureIgnore>${ignore-failure}</testFailureIgnore>
                <groups>${include-tags}</groups>
                <excludedGroups>${exclude-tags}</excludedGroups>
                <properties>
                    <configurationParameters>
                        junit.jupiter.execution.parallel.enabled=${parallel-enabled}
                        junit.jupiter.execution.parallel.config.strategy=fixed
                        junit.jupiter.execution.parallel.config.fixed.parallelism=${thread-count}
                        junit.jupiter.execution.parallel.config.fixed.max-pool-size=${thread-count}
                    </configurationParameters>
                </properties>
            </configuration>
        </plugin>
    </plugins>
</build>
k_rollo
  • 5,304
  • 16
  • 63
  • 95