Zaphod
Zaphod

Reputation: 1417

Is there a way to chain two arbitrary specs2 tests (in Scala)?

Every now and then I run into a situation where I need to make absolutely sure that one test executes (successfully) before another one.

For example:

"The SecurityManager" should {
    "make sure an administrative user exists" in new WithApplication with GPAuthenticationTestUtility {
        checkPrerequisiteAccounts(prerequisiteAccounts)
    }

    "get an account id from a token" in new WithApplication with GPAuthenticationTestUtility {
        val token = authenticate(prerequisiteAccounts.head)

        token.isSuccess must beTrue

        myId = GPSecurityController.getAccountId(token.get)

        myId != None must beTrue
        myId.get.toInt > 0 must beTrue
    }

The first test will create the admin user if it doesn't exist. The second test uses that account to perform a test.

I am aware I can do a Before/After treatment in specs2 (though I've never done one). But I really don't want checkPrerequisiteAccounts to run before every test, just before that first test executes... sort of a "before you start doing anything at all, do this one thing..."

Anyone know if there is a way to tag a particular test as "do first" or "do before anything else?"

Upvotes: 0

Views: 161

Answers (2)

Biswanath
Biswanath

Reputation: 9185

You can also add sequential to your spec like, to sequential execution of tests.

class MySpec extends mutable.Specification {
  sequential

  // rest follows
  behaviour one 
  behaviour two 
}

Upvotes: 2

Eric
Eric

Reputation: 15557

You can just add a "Step" in between tests to enforce some sequentiality:

"make sure an administrative user exists" in ok

step("admin is created".pp)

"get an account id from a token" in ok

Upvotes: 3

Related Questions