Is there a scalaTest mechanism similar to TestNg dependOnMethods test

Can I have dependencies between scalaTest specifications, so if a test fails, all tests that depend on it are skipped?

+6
source share
4 answers

I did not add this feature to TestNG because at that time I had no convincing use cases to justify it. Since then, I have gathered some use cases, and will add a function to the next version of ScalaTest to refer to it. But these will not be dependent tests, but simply a way to β€œcancel” a test based on an unsatisfied prerequisite.

In the meantime, you can simply use the Scala if commands only to register tests if the condition is met, or register them as ignored if you prefer to see their output. If you use Spec, it looks something like this:

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 is met exactly at the time the test is built. Perhaps if, for example, it is assumed that the database is started by the beforeAll method, you will need to perform a check inside each test. And in this case, you could say that it is under consideration. Sort of:

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

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

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

I use it for my automated browser tests. If something fails, then usually it makes no sense to continue interacting with the graphical interface, since it is in a "mixed up" state.

License: The public domain (Creative Common CC0) or (optionally) 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 } } } 
+1
source

I do not know about a ready-made solution. But you can easily write your own fixtures.

In the "javadoc" "Great feature", see "Composing String Attributes of the Binding"

Such a device can, for example, replace all test runs after the first call with pending

0
source

You can use trait org.scalatest.CancelAfterFailure to undo the remaining tests after the 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
source

Source: https://habr.com/ru/post/894709/


All Articles