Gradle equivalent for Maven Surefire surefire.fork

2019-08-23 05:53发布

问题:

I'm migrating a codebase from Maven to Gradle.
In our integration test we use FailSafe ability to run the tests in parallel in multiple threads.
FailSafe is setting a system property name surefire.forkNumber with the number of the specific fork the current thread runs.

Is there equivalent parameter when running JUnit with Gradle?

回答1:

The test task in Gradle has the property maxParallelForks to instruct the tests to run in parallel. To pass a property from the command-line to set the value we can define the following test configuration in our build file:

// File: build.gradle
...
// Set maxParallelForks using project property
// testMaxParallelForks if set otherwise use 1.
test.maxParallelForks = project.findProperty('testMaxParallelForks') ?: 1
...

Then from the command-line we can run the test task: $ gradle test -PtestMaxParallelForks=8

Gradle sets the Java System property org.gradle.test.worker with the index number of the thread running the test. This property can be read and used within the tests that run on the parallel thread.



标签: gradle junit4