简体   繁体   中英

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?

@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 ...
    }
  }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM