Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute JUnit tests in parallel for a single test class in Gradle

We have a lot of integration tests that are using Spring. We'd like not to create separate JVM processes per tests (maxParallelForks option) or only run parallel builds in the multimodule project (--parallel).

We would like for a single test class execute tests in parallel like in Maven with http://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html and the parallel option

The important thing to remember with the parallel option is: the concurrency happens within the same JVM process.

Is it possible to be achieved in Gradle?

like image 789
Marcin Grzejszczak Avatar asked Feb 05 '15 13:02

Marcin Grzejszczak


People also ask

How do I run Gradle tasks in parallel?

Parallel execution Yet Gradle will only run one task at a time by default, regardless of the project structure (this will be improved soon). By using the --parallel switch, you can force Gradle to execute tasks in parallel as long as those tasks are in different projects.

Is parallel execution possible in JUnit?

Yes, in this post, I will explain to you how to use JUnit 5 Parallel Test Execution feature. This is an experimental feature of JUnit 5 and it will come from after JUnit 5.3. To enable parallel execution, simply set the junit. jupiter.


2 Answers

@MariuszS answer was the best

Gradle is ant without xml, maybe this can help you ant.apache.org/manual/Tasks/parallel.html :)

The closest way to achieve it is described here -https://discuss.gradle.org/t/how-to-use-ants-parallel-target/6720/5

task runAllTestsByGroups << {
      ant.parallel(threadsPerProcessor: 1) {
                    ant.junit (...) {
            // run test group 1
            ...
        }
          ant.junit(...) {
            // run test group 2
            ...
       }
         // run group 3, 4, 5 ...
    }
  }
like image 94
Marcin Grzejszczak Avatar answered Nov 06 '22 11:11

Marcin Grzejszczak


Spring 5: New kid on the block

Since Spring 5, you can run Junit tests (using Spring TestContext Framework) in parallel, which I think is what you are looking for: simultaneous execution within the same JVM.

See Baeldung's blog for more information.


Original answer:

Here is Gradle doc explaining Java plugin's tasks in Gradle, and here is API doc explaining parameters to said plugin.

Please look closer at forkEvery and maxParallelForks parameters.

Setting these should give you enough control to achieve parallel test execution.

Here is the SO answer pointing at what value maxParallelForks should be set to, in order to maximize the speedup.

like image 27
diginoise Avatar answered Nov 06 '22 10:11

diginoise