is there a scalaTest mechanism similar to TestNg dependsOnMethods annotation

我可以在scalaTest规范之间有依赖关系吗?如果测试失败,所有依赖它的测试都会被跳过?


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

链接地址: http://www.djcxy.com/p/54204.html

上一篇: 如何在Windows上每毫秒或更多时间做某些事情

下一篇: 有没有类似于TestNg dependsOnMethods注解的scalaTest机制