我使用ScalaTest 2.1.4和SBT 0.13.5。我有一些长期运行的测试套件,如果单个测试失败可能需要很长时间才能完成(多JVM Akka测试)。如果其中任何一个失败,我希望整个套件中止,否则套件可能需要很长时间才能完成,特别是在我们的CI服务器上。如何配置ScalaTest以在测试失败时中止套件?
如何在套件中的任何测试失败时将ScalaTest配置为中止套件?
我使用ScalaTest 2.1.4和SBT 0.13.5。我有一些长期运行的测试套件,如果单个测试失败可能需要很长时间才能完成(多JVM Akka测试)。如果其中任何一个失败,我希望整个套件中止,否则套件可能需要很长时间才能完成,特别是在我们的CI服务器上。如何配置ScalaTest以在测试失败时中止套件?
如何在套件中的任何测试失败时将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!")
}
}
谢谢尤金;这里是我的改进:
trait TestBase extends FunSuite {
import TestBase._
override def withFixture(test: NoArgTest): Outcome = {
if (aborted) Canceled(s"Canceled because $explanation")
else super.withFixture(test)
}
def abort(text: String = "one of the tests failed"): Unit = {
aborted = true
explanation = text
}
}
object TestBase {
@volatile var aborted = false
@volatile var explanation = "nothing happened"
}
不知是否可以在不使用var
s内完成。
这太好了。现在,如果我在第一个套件中放置我的初始化资料(我既设置了数据库又测试了设置功能),我保证它始终会首先运行?即运行顺序与源文件中的顺序相同吗? – akauppi 2014-10-24 13:22:08