# Parallel tests in subprojects

**URL:** https://discuss.gradle.org/t/parallel-tests-in-subprojects/34978
**Category:** Help/Discuss
**Created:** [February 20, 2020, 10:52am UTC](https://discuss.gradle.org/t/parallel-tests-in-subprojects/34978 "2020-02-20T10:52:45Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![oyvind.s](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/oyvind.s/32/8323_2.png) [@oyvind.s](https://discuss.gradle.org/u/oyvind.s)
#### Post date: [February 20, 2020, 10:52am UTC](https://discuss.gradle.org/t/parallel-tests-in-subprojects/34978/1 "2020-02-20T10:52:45Z")

</div>

We moved to JUnit 5 and want to make use of concurrent running tests. We are using sub projects, and some projects have tests which can be run concurrently (both on class and test level), while others cannot

Which is the best way to enable only some projects to run concurrent tests? I’ve played around with system properties, but I haven’t found the solution yet.

---

<div class="post-metadata">

### Author: ![Learner](https://avatars.discourse-cdn.com/v4/letter/l/b782af/32.png) [@Learner](https://discuss.gradle.org/u/Learner)
#### Post date: [February 20, 2020, 2:19pm UTC](https://discuss.gradle.org/t/parallel-tests-in-subprojects/34978/2 "2020-02-20T14:19:52Z")

</div>

We found that we can disable parallel execution of tests on specific projects by setting maxParallelForks. It is project specific, in fact task specific, and won’t affect others:

```
test {
    maxParallelForks = 1; // Will not do parallel execution
}
```

---

<div class="post-metadata">

### Author: ![oyvind.s](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/oyvind.s/32/8323_2.png) [@oyvind.s](https://discuss.gradle.org/u/oyvind.s)
#### Post date: [February 20, 2020, 3:16pm UTC](https://discuss.gradle.org/t/parallel-tests-in-subprojects/34978/3 "2020-02-20T15:16:11Z")

</div>

OK - I did try the opposite, by setting maxParallelForks = 1 in the root project, and maxParallelForks \> 1 in those projects I wanted parallel execution, but I could not make it work. Will try some more then.

---

<div class="post-metadata">

### Author: ![oyvind.s](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/oyvind.s/32/8323_2.png) [@oyvind.s](https://discuss.gradle.org/u/oyvind.s)
#### Post date: [February 20, 2020, 3:40pm UTC](https://discuss.gradle.org/t/parallel-tests-in-subprojects/34978/4 "2020-02-20T15:40:34Z")

</div>

How are you enabling parallel execution in other projects?

---

<div class="post-metadata">

### Author: ![Learner](https://avatars.discourse-cdn.com/v4/letter/l/b782af/32.png) [@Learner](https://discuss.gradle.org/u/Learner)
#### Post date: [February 20, 2020, 3:45pm UTC](https://discuss.gradle.org/t/parallel-tests-in-subprojects/34978/5 "2020-02-20T15:45:23Z")

</div>

We leave maxParallelForks as is or set it to more than 1. You may need to invoke Gradle with --parallel and --max-workers

See

- [https://docs.gradle.org/current/userguide/command\_line\_interface.html](https://docs.gradle.org/current/userguide/command_line_interface.html)
- [https://guides.gradle.org/performance/](https://guides.gradle.org/performance/)
- [https://docs.gradle.org/2.4-rc-1/release-notes.html#new-“max-workers”-build-setting](https://docs.gradle.org/2.4-rc-1/release-notes.html#new-%E2%80%9Cmax-workers%E2%80%9D-build-setting)

---

<div class="post-metadata">

### Author: ![oyvind.s](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/oyvind.s/32/8323_2.png) [@oyvind.s](https://discuss.gradle.org/u/oyvind.s)
#### Post date: [February 20, 2020, 4:22pm UTC](https://discuss.gradle.org/t/parallel-tests-in-subprojects/34978/6 "2020-02-20T16:22:25Z")

</div>

Gradle (we’re on 5.6.2) is set up with parallel execution through gradle.properties, and max workers defaults to # processors as I understand. You don’t activate concurrency through @Execution, or either properties file or system properties? My tests don’t seem to run in parallel unless I do something like this

```
tasks.test{
     // this runs in parallel
     useJunitPlatform()
     systemProperties['junit.jupiter.execution.parallel.enabled'] = true
     systemProperties['junit.jupiter.execution.parallel.mode.default'] = 'concurrent'
     systemProperties['junit.jupiter.execution.parallel.mode.default.classes'] = 'concurrent'
}

```

Config below runs test sequentially

```
tasks.test{
     useJunitPlatform()
     maxParallelForks = 24
}

```

Config below runs test in parallel even with maxParallelForks = 1

```
tasks.test{
     useJunitPlatform()
     systemProperties['junit.jupiter.execution.parallel.enabled'] = true
     systemProperties['junit.jupiter.execution.parallel.mode.default'] = 'concurrent'
     systemProperties['junit.jupiter.execution.parallel.mode.default.classes'] = 'concurrent'
     maxParallelForks = 1
}
```

---

<div class="post-metadata">

### Author: ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)
#### Post date: [February 20, 2020, 7:31pm UTC](https://discuss.gradle.org/t/parallel-tests-in-subprojects/34978/7 "2020-02-20T19:31:26Z")

</div>

You may need to set the [forkEvery](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html#org.gradle.api.tasks.testing.Test:forkEvery) property

See [Relationship between forkEvery, maxParallelFork and --parallel](https://discuss.gradle.org/t/relationship-between-forkevery-maxparallelfork-and-parallel/25126/2)

---

<div class="post-metadata">

### Author: ![oyvind.s](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/oyvind.s/32/8323_2.png) [@oyvind.s](https://discuss.gradle.org/u/oyvind.s)
#### Post date: [February 20, 2020, 9:29pm UTC](https://discuss.gradle.org/t/parallel-tests-in-subprojects/34978/8 "2020-02-20T21:29:33Z")

</div>

The overall goal here is to have have gradle build my project as fast as possible. The project has a lot of sub projects with a lot of tests. There are so many sub projects that I don’t want to define the test task in each. I either want to enable parallel execution in general, and disable parallelity in projects which fail (preferred solution), or the other way around.

So if I define the default test tasks as

```
maxParallelForks = 1
forkEvery = 1 // assuming this is what you suggest

```

What is the effect for all my tests that run with this config compared to just letting them run with sequentially? The effect of starting a new process vs reuse which I assume is the case for sequential execution?

I’m finding the test setup hard to understand, at least how these system properties I’m playing with affect the use of for instance maxParallelForks. Is my use of system properties the way to go, or am I doing it all wrong?

---

<div class="post-metadata">

### Author: ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)
#### Post date: [February 20, 2020, 10:50pm UTC](https://discuss.gradle.org/t/parallel-tests-in-subprojects/34978/9 "2020-02-20T22:50:39Z")

</div>

The `Test` task can launch multiple junit (or testng) processes in series or parallel. The properties on the `Test` task manage this behavior.

I’m unfamiliar with your system properties but I believe these will affect the junit process once it is launched by gradle (which could already be multiple/parallel)

> Is my use of system properties the way to go

You can achieve parallel test execution without the jupiter specific system properties. I’d suggest that you either configure the parallelism in Gradle or via jupiter system properties, but not both (please note my earlier comment that I’m unfamiliar with the jupiter config)

> There are so many sub projects that I don’t want to define the test task in each

Since Gradle scripts are groovy (or kotlin) you can configure multiple projects programmatically. Eg:

```gradle
[':projectA', ':projectB'].each {
   project(it) {
      tasks.withType(Test) {
         maxParallelForks = 1
      } 
   } 
} 

```

---

<div class="post-metadata">

### Author: ![oyvind.s](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/oyvind.s/32/8323_2.png) [@oyvind.s](https://discuss.gradle.org/u/oyvind.s)
#### Post date: [February 20, 2020, 11:18pm UTC](https://discuss.gradle.org/t/parallel-tests-in-subprojects/34978/10 "2020-02-20T23:18:58Z")

</div>

> [@Lance](#):
>
> I’d suggest that you either configure the parallelism in Gradle or via jupiter system properties, but not both

I agree, it felt messy from the start. Thank you for your input, I will try to make it work with gradle.
