Manuel Bernhardt
Manuel Bernhardt

Reputation: 3140

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?

Upvotes: 38

Views: 23841

Answers (7)

virtualeyes
virtualeyes

Reputation: 11237

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.

Upvotes: 2

Alex Elkin
Alex Elkin

Reputation: 624

You can try also Global / parallelExecution := false

Upvotes: 1

xmar
xmar

Reputation: 1809

This worked for me in 1.1.0:

Test / parallelExecution := false

Upvotes: 10

brunovianarezende
brunovianarezende

Reputation: 866

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.

Upvotes: 1

lisak
lisak

Reputation: 21971

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

Upvotes: 23

Boris Stumm
Boris Stumm

Reputation: 128

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

Upvotes: 4

0__
0__

Reputation: 67280

I think you can apply a setting across projects using scope ThisBuild, like

parallelExecution in ThisBuild := false

I don't know if you can combine that with scope Test, but it might not be necessary.

Upvotes: 36

Related Questions