Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run parallel test task using gradle

We use JUnit as a test framework. We have many projects. We use gradle (version 1.12) as a build tool. To run the unit tests in parallel using gradle we use the below script in every project under test task.

maxParallelForks = Runtime.runtime.availableProcessors() 

Ex:

test {       maxParallelForks = Runtime.runtime.availableProcessors()        } 

We also maintain the single gradle.properties file.

Is it possible to define test.maxParallelForks = Runtime.runtime.availableProcessors() in gradle.properties file rather than defining it in each build.gradle file under test task?

like image 480
Java-Seekar Avatar asked May 22 '14 11:05

Java-Seekar


People also ask

How do I run Gradle tasks in parallel?

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.

How do you run parallel test cases in TestNG Gradle?

single=TestClassName , expecting all the @Test methods inside that to be run in parallel. The relevant documentation: parallel="methods" : TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.


2 Answers

The accepted answer above works but the Gradle documentation here suggests you use

maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1 

I tried both and after testing both on a 2.3 GHz Intel Core i7 Mac Book Pro with 16GB RAM (4 cores with hyperthreading)

test {     maxParallelForks = Runtime.runtime.availableProcessors() } 

and

test {         maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1       } 

The approach suggested by Gradle documentation produced faster response times for our unit test suite: 7 minutes vs. 8 minutes (compared to the original 13 minutes). In addition my Mac CPU didn't get pegged and the fan didn't kick off.

I assume there is either contention on a shared resource - even if it is only the machine one which we are running the unit tests.

like image 181
kellyfj Avatar answered Sep 17 '22 17:09

kellyfj


$rootDir/build.gradle:

subprojects {     tasks.withType(Test) {         maxParallelForks = Runtime.runtime.availableProcessors()     } } 
like image 44
Peter Niederwieser Avatar answered Sep 19 '22 17:09

Peter Niederwieser