Detect if build is parallel and how many runners

Hello everyone. I’d like to run my tests in parallel if and only if gradle itself is also running parallel. However I’m not sure how I could find that out. Also how I can get the max number of runners ideally I’d like something like this:

test {
  if(gradle.runsParallel) {
    maxParallelForks = gradle.runners
  }

  forkEvery = 1
}

Someone on IRC showed me the solution:

test {
  if(gradle.startParameter.isParallelProjectExecutionEnabled()) {
    maxParallelForks = gradle.startParameter.maxWorkerCount
  }

  forkEvery = 1
}

This will fork a new test process for every single test case, making your tests very slow.

I’m aware that this will for for every test class. It is intended behavior, as I need a fresh JVM for quite a few classes.