Search code examples
scalascalatestspecs2

Wrong order of execution of tests


I have the following test code:

import org.specs2.mutable.Specification
import org.specs2.specification.{BeforeExample, Before, Scope}

class TestSpec extends Specification with BeforeExample {

  def before = println("before!!!!")

  "Test" should {
    "run before 1" in {
      println("test 1")
      success
    }
    "run before 2" in {
      println("test 2")
      success
    }
    "run before 3" in {
      println("test 3")
      success
    }
  }

}

I expect something like:

before!!!!
test 1
before!!!!
test 2
before!!!!
test 3
...

But get the output:

before!!!!
before!!!!
before!!!!
test 3
test 2
test 1Test should
run before 1
run before 2
run before 3

Why such a strange order? Tests are run in parallel mode?

In this synthetic example it does not matter. But if before make a database cleanup or something else this execution order break testing.


Solution

  • specs2 executes tests concurrently by default (see the docs here). Add the sequential keyword in order to execute the tests sequentially:

    class TestSpec extends Specification with BeforeExample {
     sequential
     // your tests
    }