Justin Lin
Justin Lin

Reputation: 761

multiple before/after blocks in Scala BeforeAndAfter

Can scalatest support mulitple before and after blocks?

If so what would be the order of execution?

trait A extends BeforeAndAfter {
val aa

before {
  // access aa
}

after {
  // access aa
}
}

class B with A {
val bb

before {
  // access bb
}

after {
  // access bb
}
}

EDIT: base on one of the answers, it's not possible. Wonder how to achieve this

Upvotes: 1

Views: 422

Answers (1)

francoisr
francoisr

Reputation: 4595

Why not just try the code for yourself? Running this snippet

trait A extends TestSuite with BeforeAndAfter {
  before {
    // First call
  }

  before {
    // Second call
  }
}

Results in

org.scalatest.exceptions.NotAllowedException: 
You are only allowed to call before once in each Suite that mixes in BeforeAndAfter.

Same goes for after. The answer is no, you are not allowed to call before or after more than once in any given Suite instance. Note that using A and B is irrelevant here.

Upvotes: 1

Related Questions