How to run JUnit 5 Tests in Parallel? Yes, in this post, I will explain how to use the JUnit 5 Parallel Test Execution feature. This experimental feature of JUnit 5 will come after JUnit 5.3. To enable parallel execution, set the junit.jupiter.execution.parallel.enabled configuration parameter to true in junit-platform.properties file.
Once the parallel test execution property is enabled, the JUnit Jupiter engine will execute tests in parallel according to the provided configuration with declared synchronization mechanisms. Please note that the Capturing Standard Output/Error feature needs to be enabled separately. For configuration and synchronization details, you can go and read JUnit 5 official documentation.
Let’s start to write some code and experiment with this new functionality.
First, you need to add the below dependencies in your pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>junit5-parallel</groupId> <artifactId>junit5-parallel</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.0.0-beta-3</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.8.0-M1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.8.0-M1</version> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.8.0-M1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <reportFormat>plain</reportFormat> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> </plugins> </build> </project>
Then, you need to create a “junit-platform.properties” file and locate it under the test resources folder.
The content of the junit-platform.properties file is as follows:
junit.jupiter.execution.parallel.enabled=true junit.jupiter.execution.parallel.config.strategy=dynamic
You can set your parallel test execution strategy as fixed as well. It depends on you. You can check them here.
Then, you need to add at the top of your test classes this annotation: @Execution(ExecutionMode.CONCURRENT)
Also, you can add this annotation on top of the BaseTest class. This way, you do not need to add this annotation at the top of each test class.
A sample test class is shown below:
package tests; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; import pages.HomePage; @Execution(ExecutionMode.CONCURRENT) public class LoginTest extends BaseTest { @Test public void invalidLoginTest_InvalidUserNameInvalidPassword() { page.getPage(HomePage.class) .givenIAmAtHomePage() .whenIGoToLoginPage() .andILoginToN11("onur@", "11223344") .thenIVerifyLogEntryFailMessage(); } @Test public void invalidLoginTest_EmptyUserEmptyPassword() { page.getPage(HomePage.class) .givenIAmAtHomePage() .whenIGoToLoginPage() .andILoginToN11("", "") .thenIVerifyLoginUserNameErrorMessage("Lütfen e-posta adresinizi girin.") .thenIVerifyPasswordErrorMessage("Bu alanın doldurulması zorunludur."); } }
After these settings, when you run your tests, they will run in parallel, as shown below.
If you want to run these tests in parallel with the maven command, you can try with the below command.
mvn clean -Dtest=tests.** test
GitHub Project
https://github.com/swtestacademy/junit5-parallel-testing
YouTube Demo Video
Thanks.
OnurBaskirt

Onur Baskirt is a Software Engineering Leader with international experience in world-class companies. Now, he is a Software Engineering Lead at Emirates Airlines in Dubai.