Set maxParallelForks to number of cores on the current machine

Is there a way to set maxParallelForks for tests execution to number of cores on the current machine? I.e. Gradle will check number of available cores and set the value accordingly?

You can use the normal Java means:

tasks.withType(Test) {
    maxParallelForks = Runtime.runtime.availableProcessors / 2
}

Here, ‘/2’ tries to account for virtual vs. physical cores, and has to be adapted depending on your hardware. Depending on the kind of tests (CPU vs. IO bound) and specification/load of the machine, a number lower or higher than the number of cores may work better. The best thing to do is to experiment.

should be maxParallelForks = Runtime.runtime.availableProcessors() / 2

with parentheses ‘()’

and due to annoying IntelliJ gradle integration issues, I’d recommend fully qualifying Test:

tasks.withType(org.gradle.api.tasks.testing.Test)