How to run ScalaTest tests in the SAME class in PARALLEL

Viewed 1233

On an environment with

  • scala 2.12
  • sbt 1.1.4
  • scalatest 3.0.5

I'm not able to run tests in the same class in a parallel because it looks like SBT will run in a parallel way every class, not the test in the same class.

e.g.

  • I have a class called TestA in qa.parallelism package. This class contains two tests called test1 and test2.
  • I have another class called TestB in qa.parallelism package that contains a test called test1.

if I run

testOnly qa.parallelism.*

by log I understand that TestA.test1 and TestB.test1 was executed simultaneously,

but if I run

testOnly qa.parallelism.TestA

that contains two tests (test1 and test2), I understand that test2 will be executed at the end of test1.

Is there a way to run simultaneously every test of a single class or I should create a class for every single test?

Thanks.

1 Answers

ParallelTestExecution docs state default ScalaTest behaviour is to:

...run different suites in parallel, but the tests of any one suite sequentially.

However, mixing in ParallelTestExecution trait enables tests within the same class to be run in parallel. For example,

import org.scalatest.{FlatSpec, Matchers, ParallelTestExecution}

class HelloSpec extends FlatSpec with Matchers with ParallelTestExecution {
  "The Hello object" should "say hello 1" in {
    println("1")
    Hello.greeting should be ("hello")
  }

  it should "say hello 2" in {
    println("2")
    Hello.greeting should be ("hello")
  }

  it should "say hello 3" in {
    println("3")
    Hello.greeting should be ("hello")
  }
}

outputs different orderings of printlns on different sbt test executions.

Related