I do know that there is the BeforeAndAfter
trait which allows us to perform setup and tear-down operations before and after every test of a suite.
Is there an alternative that either runs only before and after a suite or runs for selected tests?
Regarding before and after particular test consider loan pattern
import org.scalactic.Equality
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class LoanPatternExampleSpec extends AnyFlatSpec with Matchers {
def life(testFun: Int => Any): Unit = {
println("Doing stuff before the test")
val meaningOfLife = 42
testFun(meaningOfLife)
println("Doing stuff after the test")
}
"User" should "do things" in life { i =>
println(s"My fixture says meaning of life is $i")
assert(true)
}
}
which outputs
sbt:scalatest-seed> testOnly example.LoanPatternExampleSpec
Doing stuff before the test
My fixture says meaning of life is 42
Doing stuff after the test
[info] LoanPatternExampleSpec:
[info] User
[info] - should do things
[info] Run completed in 314 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
BeforeAndAfterAll
has overrides for methods to be executed once before and after the suite.