Ryan
Ryan

Reputation: 7257

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?

Upvotes: 6

Views: 3512

Answers (2)

Vlad Patryshev
Vlad Patryshev

Reputation: 1422

Thank you Eugene; here's my improvement:

trait TestBase extends FunSuite {
  import TestBase._

  override def withFixture(test: NoArgTest): Outcome = {
    if (aborted) Canceled(s"Canceled because $explanation")
    else super.withFixture(test)
  }

  def abort(text: String = "one of the tests failed"): Unit = {
    aborted = true
    explanation = text
  }
}

object TestBase {
  @volatile var aborted = false
  @volatile var explanation = "nothing happened"
}

I wonder if it can be done without using vars.

Upvotes: 0

Eugene Zhulenev
Eugene Zhulenev

Reputation: 9734

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

}

Upvotes: 6

Related Questions