Scalatest - Suite in parallel

Viewed 354

I am using a Suite (Scalatest-version: 3.2.2) which has several TestSuites inside:

class SuiteMixedSequentialParallel
    extends Stepwise(
      new TestInParallel,
      new TestSequentially
    )

Now, I want, that all tests in TestInParallel should be executed in parallel, all tests in TestSequentially should be executed sequentially.

Therefore I extended TestInParallel from ParallelTestExecution

class TestInParallel extends AnyFunSuite with ParallelTestExecution {
  (0 to 10).foreach(i =>
    test(s"$i") {
      Thread.sleep(500)
      println(s"TestInParallel $i")
    }
  )
}
class TestSequentially extends AnyFunSuite {
  (0 to 10).foreach(i =>
    test(s"$i") {
      Thread.sleep(200)
      println(s"TestSequentially $i")
    }
  )
}

When I run sbt testOnly TestInParallel, all tests are executed in parallel. But: When I run sbt testOnly SuiteMixedSequentialParallel, all tests are executed sequentially.

Does anybody have a hint, how to achieve, that my tests in TestInParallel will run in parallel, when I run SuiteMixedSequentialParallel?

The suites TestInParallel and TestSequentially should still run sequentially. Just the tests in TestInParallel should run in parallel.

1 Answers

From Stepwise documentation:

When StepsSuite is executed, regardless of whether a Distributor is passed, it will execute its nested suites sequentially in the passed order: Step1Suite, Step2Suite, Step3Suite, Step4Suite, and Step5Suite.

From ParallelTestExecution documentation:

ScalaTest's normal approach for running suites of tests in parallel is to run different suites in parallel, but the tests of any one suite sequentially.

Therefore, when you combine the two above, you block scalatest from running in parallel, such that even TestInParallel is running sequentially.

In order to resolve that, you need to mixin the ParallelTestExecution trait into SuiteMixedSequentialParallel, meaning:

class SuiteMixedSequentialParallel
  extends Stepwise(
    new TestInParallel,
    new TestSequentially
) with ParallelTestExecution

This will run both suites in parallel, the tests in TestInParallel in parallel, and the tests from TestSequentially, sequentially. As desired.

In order to verify that, I changed the println from Hallo Welt into TestInParallel, and TestSequentially respectively.

The output I got is:

TestInParallel 8
TestInParallel 4
TestInParallel 0
TestInParallel 7
TestInParallel 5
TestInParallel 3
TestSequentially 0
TestInParallel 2
TestInParallel 1
TestInParallel 9
TestInParallel 6
TestSequentially 1
TestSequentially 2
TestSequentially 3
TestSequentially 4
TestSequentially 5
TestSequentially 6
TestSequentially 7
TestSequentially 8
TestSequentially 9
Related