14

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?

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32
  • 1
    Gradle is `ant` without `xml`, maybe this can help you https://ant.apache.org/manual/Tasks/parallel.html :) – MariuszS Feb 19 '15 at 21:50
  • 1
    Actually I really considered that! That's a good hint - there are a lot of articles on how to do the above with JUnit and ant. – Marcin Grzejszczak Feb 26 '15 at 12:16
  • I opened a discussion on gradle about this topic https://discuss.gradle.org/t/how-to-execute-junit-tests-in-parallel – mendlik Sep 06 '16 at 10:35

2 Answers2

4

@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 ...
    }
  }
Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32
  • 1
    I have no idea how the other answer can have more +1 since it doesn't answer my problem. This one did, cause I actually used sth like an ant task to do that. Don't really remember what that was. – Marcin Grzejszczak Jun 02 '17 at 12:19
  • You can always down-vote if you feel strongly about the answer below. As is Gradle does not support it, so you are resolving to run tests using ant's `parallel` task which is not guaranteed to be thread safe as per their docs. Or post the answer which got you to where you wanted to be so that we all learn something. – diginoise Feb 28 '18 at 10:01
  • 1
    I've updated the answer. Hopefully people will stop downvoting this. – Marcin Grzejszczak Feb 28 '18 at 10:03
2

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.

diginoise
  • 7,352
  • 2
  • 31
  • 39
  • 5
    Thank you of course I know about those two - what I was looking for is actually not spawning JVMs. I talked to the Gradle developers and they said that the feature is not present in Gradle at the moment. – Marcin Grzejszczak Feb 26 '15 at 12:16