Search code examples
scalaspecs2

Forcing order of excecution with Specs2 Acceptance Style Testing


I'm trying to get my tests to execute sequentially with Specs2 acceptance style testing, but I'm not having any luck.

  override def is = {     
    "Template Project REST Specification" ^
      p ^
      "The server should" ^
      "Respond with greeting on root path" ! serverRunning ^
      p ^
      "For CLIENT json objects" ^
      "Return an empty list if there are no entities" ! getEmptyClientList ^
      "Create a new entity" ! createClient ^
      "Return a non-empty list if there some entities" ! getNonEmptyClientList ^
      "Read existing" ! todo ^
      "Update existing" ! todo ^
      "Delete existing" ! todo ^
      "Handle missing fields" ! todo ^
      "Handle invalid fields" ! todo ^
      "Return error if the entity does not exist" ! todo ^
      end
  }

When running the tests, the createClient test keeps creating a new client element before the getEmptyClientList test gets a chance to execute.

If I add a whole heap of getEmptyClientList tests before the createClient test, then all but the last one will execute before the call to createClient. But createClient will always beat the last getEmptyClientList call, which results in it failing.

How do I force it to execute sequentially? With Specs2 unit testing style, I just added the sequential keyword before the tests and all would be well.


Solution

  • In acceptance specifications, the sequential argument can be added at the beginning of the specification like this:

    def is = sequential ^
      "Template Project REST Specification" ^
       p ^
       "The server should" ^
       "Respond with greeting on root path" ! serverRunning ^
       p ^
       "For CLIENT json objects" ^
       "Return an empty list if there are no entities" ! getEmptyClientList ^
       "Create a new entity" ! createClient ^
       "Return a non-empty list if there some entities" ! getNonEmptyClientList ^
       "Read existing" ! todo ^
       "Update existing" ! todo ^
       "Delete existing" ! todo ^
       "Handle missing fields" ! todo ^
       "Handle invalid fields" ! todo ^
       "Return error if the entity does not exist" ! todo ^
       end
    

    Note that you don't need to override the is method and you also don't need curly braces.