Any way to run JUnit5 tests in parallel?
Asked Answered
H

1

6

Previously I was using Maven+Selenide+JUnit4 for my tests and it was fine, parallel running worked perfectly. Example:

<plugins>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven.surefire.plugin}</version>
    <configuration>
        <parallel>all</parallel>
        <perCoreThreadCount>true</perCoreThreadCount>
        <threadCount>4</threadCount>
        <perCoreThreadCount>false</perCoreThreadCount>
        <redirectTestOutputToFile>true</redirectTestOutputToFile>
    </configuration>
</plugin>

And in Jenkins job I was able to run tests (example below)

mvn -Dtest=TestClassName test

My tests were running in 4 browsers.

Before I switched to JUnit5, because I would like to use running tests by tags, for example

@Test
@Tag("smoke")
public void test1() {}

And run all tests which marked as 'smoke' by next command:

mvn -Dtag=smoke test

But I got next problem: parallel execution does not work and I still did not find solution. I found this bug https://github.com/junit-team/junit5/issues/1424

How can I run tests in parallel with JUnit5?

I have tried to use in pom.xml

<forkCount>2</forkCount>
<reuseForks>true</reuseForks>
<parallel>all</parallel>

It did not help, I have created a file junit-platform.properties and insert there

junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.config.strategy = fixed

But anyway I was not able to solve this problem.

Highams answered 12/7, 2018 at 14:24 Comment(9)
Which versions of the different JUnit JARs are you using?Galateah
Hi! I use <junit.jupiter.version>5.2.0</junit.jupiter.version>Highams
My pom.xml github.com/13Dima13/G/blob/master/pom.xml I already switched to latest 5.3.0-M1 version, but anyway I did not get how to run in parallel.Highams
Have you read the instructions in the User Guide? junit.org/junit5/docs/snapshot/user-guide/…Globoid
Of course I have read. Have you checked what I described before? I can not run in parallelHighams
Where does your junit-platform.properties file reside? And can you perhaps share a sample project demonstrating that the tests do not run in parallel?Globoid
Sure, my junit-platform.properties file github.com/13Dima13/G/blob/master/junit-platform.propertiesHighams
Any ideas what should I change to run parallel tests?Highams
Yes, move junit-platform.properties to src/test/resources.Galateah
H
7

Finally I found solution.

On Maven+JUnit5 parallel execution will work only by classes (not by methods as I get used to have in JUnit4)

How it can be implemented: Just put these 2 strings in your pom.xml:

<forkCount>4</forkCount>
<reuseForks>false</reuseForks>

Example:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
                <forkCount>4</forkCount>
                <reuseForks>false</reuseForks>
                <properties>
                    <includeTags>${tag}</includeTags>
                </properties>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>${junit.platform.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit.jupiter.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-logger-api</artifactId>
                    <version>${surefire-logger-api}</version>
                </dependency>
            </dependencies>
        </plugin>

For instance you have 3 classes with tests, so after running from console current tests will be created 3 instances of your browser (one for each class) and inside each class tests will be executed by consistently, but classes are executed parallel.

Highams answered 21/7, 2018 at 13:1 Comment(3)
Thanks for sorting it out! Maven-surefire-plugin 2.22.2 works fine, version 3.0.0-M3 is not working with Junit 5 in our project.Arborvitae
Hi Gordon Freeman, Can you please how to add fork setting in gralde project and what is the use of adding it . <forkCount>4</forkCount> <reuseForks>false</reuseForks> I am using below configuration in junit-platform.properties file, in two browser instants, but i still see some issues junit.jupiter.execution.parallel.enabled = true junit.jupiter.execution.parallel.mode.default = same_thread junit.jupiter.execution.parallel.mode.classes.default = concurrent junit.jupiter.execution.parallel.config.strategy=fixed junit.jupiter.execution.parallel.config.fixed.parallelism=2Dexter
This was helpful for running RestAssured in parallel since RestAssured relies on a static configuration which can not be easily shared between threads.Browder

© 2022 - 2024 — McMap. All rights reserved.