'How to turn off parallel execution of tests for multi-project builds?

I have a multi-project build with tests in sub-projects and in a parent project. The build is aggregated so that the parent project runs all tests in child projects.

I configured it so that there's no parallel execution of tests in both the sub-projects and the parent project, via

parallelExecution in Test := false

However, I have the nagging feeling that tests that span over multiple projects are ran in parallel. In the case of one of the sub-projects this is a problem because it mutates state in a test database concurrently, leading to the test to fail.

Any ideas as to how to globally switch of parallel execution of tests, between projects?



Solution 1:[1]

To restrict the number of concurrently executing tests in all projects, use:

concurrentRestrictions in Global += Tags.limit(Tags.Test, 1)

See sbt documentation

See discussion

Solution 2:[2]

This worked for me in 1.1.0:

Test / parallelExecution := false

Solution 3:[3]

See my answer here How to run subprojects tests (including setup methods) sequentially when testing

There is another way to prevent parallel execution. You can make the test tasks of the different projects depend on each other:

test in Project2 := (test in Project2).dependsOn(test in Project1).value
parallelExecution in Test in Project2 := false

Solution 4:[4]

Another possibility, based on https://stackoverflow.com/a/27068019/1922026, is to define a command alias in the root project:

.settings(addCommandAlias("test", ";s1/test;s2/test;s3/test"): _*)

where s1, s2 and s3 are the sub-projects. When you are in root project and run "test" the tests will be executed sequentially and in the order defined.

Solution 5:[5]

You can try also Global / parallelExecution := false

Solution 6:[6]

The "modern" (i.e. sbt 1.x) equivalent of disabling parallel execution in Test scope is to add the following to your build.sbt:

Global / concurrentRestrictions += Tags.limit(Tags.Test, 1)

For those not familiar with sbt syntax, in context you want to do something like:

lazy val main = project
  .in(file("."))
  .settings(
    name := "foo",
    // more settings
    // ...
    Global / concurrentRestrictions += Tags.limit(Tags.Test, 1)
  )

From the tags and rules section of sbt docs.

p.s. quite a useful setting for ScalaTest, particularly around setup/teardown logic during database testing. Fixes some quite puzzling non-deterministic errors that you'll ineviitably hit with parallel execution enabled.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lisak
Solution 2 xmar
Solution 3 Community
Solution 4 Community
Solution 5 Alex Elkin
Solution 6 virtualeyes