Reputation: 1770
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?
Upvotes: 64
Views: 92065
Reputation: 2719
kotlin gradle dsl
tasks.withType<Test> {
maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1).also {
println("Setting maxParallelForks to $it")
}
}
Upvotes: 3
Reputation: 7201
For those of us using Gradle Kotlin DSL in a build.gralde.kts who find themselves here after a google search, I was able to get it to work this way:
tasks.test {
maxParallelForks = Runtime.getRuntime().availableProcessors()
}
or
tasks.withType<Test> {
maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).takeIf { it > 0 } ?: 1
}
as I found here
edit: I ran into some issue as described here. This is (hopefully) my final form:
tasks.withType<Test> {
systemProperties["junit.jupiter.execution.parallel.enabled"] = true
systemProperties["junit.jupiter.execution.parallel.mode.default"] = "concurrent"
maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).takeIf { it > 0 } ?: 1
}
Upvotes: 19
Reputation: 6943
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.
Upvotes: 75
Reputation: 123910
$rootDir/build.gradle
:
subprojects {
tasks.withType(Test) {
maxParallelForks = Runtime.runtime.availableProcessors()
}
}
Upvotes: 62