I created a simple example of running JUnit tests as parameterized AND parallel at the same time.
This would have been VERY helpful to me in the past and so I am sharing this to help out others.
With this method, of course, there is no need to fork processes with Gradle since JUnit will do it.
It works great, as far as multi-threading goes, creating a thread pool for each class and multiple threads per pool, but the Gradle report lacks some detail when providing report information: the Gradle report displays zero information about the class files involved in the test suite and zero info about the threads in the suite (aside from the debug log).
What are you trying to accomplish with this, instead of using Gradle’s parallel test execution? As far as I know, ParallelRunner has never left experimental stage, and no released version of JUnit is thread-safe. Besides, ‘JUnitCore’ and ‘ParallelComputer’ are only meant to be used by the party that’s in control of test execution (build tool, IDE). If you use it from a test, you essentially kick off your own inner test execution, and there is no way for the outer party to know about that. That’s why you don’t see anything in the Gradle report.
tasks.withType(Test) {
// experiment with different values to see what works best
maxParallelForks = 4
// can also compute dynamically
// maxParallelForks = Runtime.runtime.availableProcessors() / 2
}
Ok, I didn’t realize that setting greater than 1 actually triggers forking.
The doc doesn’t actually say that but it makes perfect sense now.
I tried it and the reports look at lot better. During the run the individual threads don’t identify themselves in the log (at the INFO level) and I was wondering if you know of a way that I can get them to do that?
Test execution always forks at least one JVM. That’s why it’s called ‘maxParallelForks’. Each test JVM runs a single test thread. Usually it doesn’t help to know in which JVM a test is running, but if you want to know, you can do something like ‘test.testLogging.events “started”, “passed”, “failed”, “skipped”; test.testLogging.displayGranularity = 0’. As always, see the DSL reference for details.