我将 ScalaTest 2.1.4 与 SBT 0.13.5 一起使用。我有一些长时间运行的测试套件,如果单个测试失败(多 JVM Akka 测试),它们可能需要很长时间才能完成。如果其中任何一个失败,我希望整个套件中止,否则套件可能需要很长时间才能完成,尤其是在我们的 CI 服务器上。
如果套件中的任何测试失败,我如何配置 ScalaTest 以中止套件?
最佳答案
如果您只需要取消与失败测试相同的规范/套件/测试中的测试,您可以使用 scalatest 中的 CancelAfterFailure 混合。如果你想全局取消它们,这里是示例:
import org.scalatest._
object CancelGloballyAfterFailure {
@volatile var cancelRemaining = false
}
trait CancelGloballyAfterFailure extends SuiteMixin { this: Suite =>
import CancelGloballyAfterFailure._
abstract override def withFixture(test: NoArgTest): Outcome = {
if (cancelRemaining)
Canceled("Canceled by CancelGloballyAfterFailure because a test failed previously")
else
super.withFixture(test) match {
case failed: Failed =>
cancelRemaining = true
failed
case outcome => outcome
}
}
final def newInstance: Suite with OneInstancePerTest = throw new UnsupportedOperationException
}
class Suite1 extends FlatSpec with CancelGloballyAfterFailure {
"Suite1" should "fail in first test" in {
println("Suite1 First Test!")
assert(false)
}
it should "skip second test" in {
println("Suite1 Second Test!")
}
}
class Suite2 extends FlatSpec with CancelGloballyAfterFailure {
"Suite2" should "skip first test" in {
println("Suite2 First Test!")
}
it should "skip second test" in {
println("Suite2 Second Test!")
}
}
关于scala - 如果测试失败,如何配置 ScalaTest 以中止套件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26001795/