If you need to cancel only those tests from the same spec / suite / test test as with an error, you can use the CancelAfterFailure offset from scalatest. If you want to cancel them all over the world, here is an example:
import org.scalatest._
object CancelGloballyAfterFailure {
@volatile var cancelRemaining = false
}
trait CancelGloballyAfterFailure extends SuiteMixin { this: Suite =>
import CancelGloballyAfterFailure._
abstract override def withFixture(test: NoArgTest): Outcome = {
if (cancelRemaining)
Canceled("Canceled by CancelGloballyAfterFailure because a test failed previously")
else
super.withFixture(test) match {
case failed: Failed =>
cancelRemaining = true
failed
case outcome => outcome
}
}
final def newInstance: Suite with OneInstancePerTest = throw new UnsupportedOperationException
}
class Suite1 extends FlatSpec with CancelGloballyAfterFailure {
"Suite1" should "fail in first test" in {
println("Suite1 First Test!")
assert(false)
}
it should "skip second test" in {
println("Suite1 Second Test!")
}
}
class Suite2 extends FlatSpec with CancelGloballyAfterFailure {
"Suite2" should "skip first test" in {
println("Suite2 First Test!")
}
it should "skip second test" in {
println("Suite2 Second Test!")
}
}
source
share