Ryan Ryan - 2 months ago 12x
Scala Question

How to configure ScalaTest to abort a suite if a test fails?

I'm using ScalaTest 2.1.4 with SBT 0.13.5. I have some long-running test suites that can take a long time to finish if a single test fails (multi-JVM Akka tests). I would like the entire suite to be aborted if any of these fails, otherwise the suite can take a very long time to finish, especially on our CI server.

How can I configure ScalaTest to abort the suite if any test in the suite fails?


If you need to cancel only tests from same spec/suite/test as failed test, you can use CancelAfterFailure mixing from scalatest. If you want to cancel them globally here is 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")
      super.withFixture(test) match {
        case failed: Failed =>
          cancelRemaining = true
        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!")

  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!")