Running junit tests in parallel in a Maven build?
From junit 4.7 it's now possible to run tests in parallel without using TestNG. Actually it has been possible since 4.6, but there are a number of fixes being made in 4.7 that will make it a viable option. You may also run parallel tests with spring, which you can read about here
How to Run Unit Test in Parallel Across All Modules Using Maven?
In the last module to be built excluding Root
, I added the following to the pom.xml
.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<classpathScope>test</classpathScope>
<mainClass>org.testng.TestNG</mainClass>
</configuration>
</plugin>
Then to run the tests, I use the following command:
mvn -DskipTests=true "-Dexec.args=testng.xml" test
The -DskipTests=true
tells Maven to not execute the tests but the test
phase tells Maven to build everything for the tests and run the above plugin
. The "-Dexec.args=testng.xml"
is an argument to TestNG in the above plugin
configuration. Here's the contents of testng.xml
.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="All" verbose="1" parallel="methods" thread-count="100">
<test name="All">
<packages>
<package name=".*"/>
</packages>
</test>
</suite>
The parallel="methods"
attribute allows TestNG to run in parallel each test method. The thread-count="100"
attribute limits TestNG to running 100 threads. The package name=".*"
attribute tells TestNG to run all tests in all packages.
JUnit 5 tests are executed in parallel inside IntelliJ and locally with Maven, but not inside Maven Docker container on Google Cloud Build
Properties such as the desired parallelism and the maximum pool size can be configured using a
ParallelExecutionConfigurationStrategy
. The JUnit Platform provides two implementations out of the box:dynamic
andfixed
. Alternatively, you may implement a custom strategy.
Keep in mind that the ParallelExecutionConfigurationStrategy class is still in the "EXPERIMENTAL" state and it is not yet stable. Unintended behaviour may occur.
As you don't set a specific configuration strategy, the following section applies:
If no configuration strategy is set, JUnit Jupiter uses the
dynamic
configuration strategy with a factor of 1. Consequently, the desired parallelism will be equal to the number of available processors/cores.
Find more details at https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution-config
Can junit tests be run in parallel?
Yes, You can.
If you are using maven. You can take help of
maven-surefire-plugin
In Spring,
You can check this Link
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.1</version>
<configuration>
<parallel>classes</parallel>
<threadCount>5</threadCount>
</configuration>
</plugin>
</plugins>
</build>
Solution 2: Junit4 provides parallel feature using ParallelComputer
Related Topics
Returning from a Finally Block in Java
How to Override Class Variables in Java
Get a Resource Using Getresource()
How to Check If the User Is Pressing a Key
Regex Doesn't Work in String.Matches()
Generate/Get Xpath from Xml Node Java
Why Does Writeobject Throw Java.Io.Notserializableexception and How to Fix It
What Is an Outofmemoryerror and How to Debug and Fix It
Java: How to Start a Standalone Application from the Current One When Both Are in the Same Package
How to Create a Rest Client for Java
How to Get the File Extension of a File in Java
Which Is the Best Library for Xml Parsing in Java
Remove Diacritical Marks (Ń Ǹ Ň ñ Ṅ Ņ Ṇ Ṋ Ṉ ̈ Ɲ Ƞ ᶇ ɳ ȵ) from Unicode Chars
How to Supply Value to an Annotation from a Constant Java
Is There a "Word Wrap" Property for Jlabel
Setting User Agent of a Java Urlconnection