Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a scalaTest mechanism similar to TestNg dependsOnMethods annotation

Can I have dependencies between scalaTest specs such that if a test fails, all tests dependent on it are skipped?

like image 220
user44242 Avatar asked Aug 10 '11 10:08

user44242


2 Answers

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
  // ...
}
like image 67
Bill Venners Avatar answered Nov 15 '22 21:11

Bill Venners


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
    }
  }
}
like image 30
KajMagnus Avatar answered Nov 15 '22 21:11

KajMagnus