How can I get the return value from ScalaTest indicating the test suite error?

I run the ScalaTest ( FlatSpec) package programmatically, for example:

new MyAwesomeSpec().execute()

Is there any way to find out if all the tests have passed? Suite#execute()returns Unithere, therefore does not help. Ideally, I would like to run the entire package, and then get a return value indicating whether any tests were unsuccessful; An alternative would be a refusal / refund immediately on any failed test.

I may have achieved this by writing a new subclass FlatSpecthat overrides the Scalatest method Suite#execute()to return the value, but is there a better way to do what I want here?

+4
source share
1 answer

org.scalatest.Suite run, .

. , Reporter. ad-hoc:

val reporter = new Reporter() {
  override def apply(e: Event) = {}
}

, :

import org.scalatest.events.Event
import org.scalatest.{Args, Reporter}

val testSuite = new MyAwesomeSpec()
val testNames = testSuite.testNames

testNames.foreach(test => {
  val result = testSuite.run(Some(test), Args(reporter))
  val status = if (result.succeeds()) "OK" else "FAILURE!"
  println(s"Test: '$test'\n\tStatus=$status")
})

, :

Test: 'This test should pass'
    Status=OK
Test: 'Another test should fail'
    Status=FAILURE!

, .

+3

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


All Articles