开发者

is there a scalaTest mechanism similar to TestNg dependsOnMethods annotation

Can I have dependencies between scalaTest specs such that i开发者_StackOverflow社区f a test fails, all tests dependent on it are skipped?


I didn't add that feature of TestNG because I didn't at the time have any compelling use cases to justify it. I have since collected some use cases, and am adding a feature to the next version of ScalaTest to address it. But it won't be dependent tests, just a way to "cancel" a test based on an unmet precondition.

In the meantime what you can do is simply use Scala if statements to only register tests if the condition is met, or to register them as ignored if you prefer to see it output. If you are using Spec, it would look something like:

if (databaseIsAvailable) {
  it("should do something that requires the database") {
     // ...
  }
  it ("should do something else that requires the database") {
  }
 }

This will only work if the condition will be met for sure at test construction time. If the database for example is supposed to be started up by a beforeAll method, perhaps, then you'd need to do the check inside each test. And in that case you could say it is pending. Something like:

it("should do something that requires the database") {
  if (!databaseIsAvailable) pending
  // ...
}
it("should do something else that requires the database") {
  if (!databaseIsAvailable) pending
  // ...
}


Here is a Scala trait that makes all test in the test suite fail, if any test fails.
(Thanks for the suggestion, Jens Schauder (who posted another answer to this question).)

Pros: Simple-to-understand test dependencies.
Cons: Not very customizable.

I use it for my automatic browser tests. If something fails, then usually there's no point in continuing interacting with the GUI since it's in a "messed up" state.

License: Public domain (Creative Common's CC0), or (at your option) the MIT license.

import org.scalatest.{Suite, SuiteMixin}
import scala.util.control.NonFatal


/**
 * If one test fails, then this traits cancels all remaining tests.
 */
trait CancelAllOnFirstFailure extends SuiteMixin {
  self: Suite =>

  private var anyFailure = false

  abstract override def withFixture(test: NoArgTest) {
    if (anyFailure) {
      cancel
    }
    else try {
      super.withFixture(test)
    }
    catch {
      case ex: TestPendingException =>
        throw ex
      case NonFatal(t: Throwable) =>
        anyFailure = true
        throw t
    }
  }
}


I don't know about a ready made solution. But you can fairly easily write your own Fixtures.

See "Composing stackable fixture traits" in the javadoc of the Suite trait

Such a fixture could for example replace all test executions after the first one with calls to pending


You can use trait org.scalatest.CancelAfterFailure to cancel remaining tests after first failure:

import org.scalatest._

class MySpec extends FunSuite with CancelAfterFailure {
  test("successfull test") {
    succeed
  }

  test("failed test") {
    assert(1 == 0)
  }

  test("this test and all others will be cancelled") {
    // ...
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜