Always run some tests sequentially even when other tests are executed in parallel

100 Views Asked by At

I have a bunch of scalatest classes that are all based on FlatSpec. I run all my tests in parallel using the -P command line parameter of scalatest. This works fine except for a few of the test classes which depend on some global variable. To make these tests reliable, I have to avoid running them in parallel.

Is there a way to flag a given test as "please don't ever try to run these tests in parallel to any other tests"?

So, just to make it clear:

  • given I have 10 test classes where 2 of them are not parallel-frendly
  • I want to run the 2 tests classes sequentially
  • all tests from the other 8 classes should be executed in parallel
  • all these 10 tests must be executed through a single invocation of the scalatest runner so that I can run all my tests using a single run configuration in IntelliJ.

I am therefore looking for something like the [CollectionDefinition(DisableParallelization = true)] attribute that xunit offers.

1

There are 1 best solutions below

3
Gastón Schabas On

You can have Custom test configuration. That will let you have different setup for each test configuration. One for the test that can be executed in parallel and another one for the ones that you need to run sequentially. Remember that sbt run all task in parallel by default. So, for each config you can have different setups.

having a directory structure like

- src
  |- main
  |---- scala
  |- test
  |---- scala
  |- serial
  |---- scala

and your build.sbt will be something like

lazy val Serial = config("serial") extend(Test)

lazy val root = (project in file("."))
  .configs(Serial)
  .settings(
    inConfig(Serial)(Defaults.testSettings),
    libraryDependencies ++= Seq(
      "org.scalatest" %% "scalatest"       % "3.2.15" % Seq(Test, Serial)
    ),
    Serial / parallelExecution := false
  )