tl;dr 将testResultLogger 与不会抛出TestsFailedException 的自定义测试结果记录器一起使用,而TestsFailedException 又会设置非0 退出代码。
刚刚注意到我错过了该要求“以避免更改build.sbt。您可以使用任何其他*.sbt 文件,例如exitcodezero.sbt 或~/.sbt/0.13/default.sbt 与自定义testResultLogger。
事实证明,由于 sbt 0.13.5 有一种方法可以产生这种行为 - 请参阅 Added setting 'testResultLogger' which allows customisation of test reporting testResultLogger 的诞生地。
> help testResultLogger
Logs results after a test task completes.
因为它可能已经在the implementation of TestResultLogger.SilentWhenNoTests 中读取,这是testResultLogger 的默认值:
results.overall match {
case TestResult.Error | TestResult.Failed => throw new TestsFailedException
case TestResult.Passed =>
}
这意味着当执行测试出现问题时,会抛出TestsFailedException异常,然后捕获并报告如下:
[error] Failed: Total 3, Failed 1, Errors 0, Passed 2
[error] Failed tests:
[error] HelloWorldSpec
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
我的想法是不管执行测试的结果如何都禁止抛出异常。将以下内容添加到build.sbt 并始终使用退出代码0:
testResultLogger in (Test, test) := new TestResultLogger {
import sbt.Tests._
def run(log: Logger, results: Output, taskName: String): Unit = {
println("Exit code always 0...as you wish")
// uncomment to have the default behaviour back
// TestResultLogger.SilentWhenNoTests.run(log, results, taskName)
}
}
取消注释 TestResultLogger.SilentWhenNoTests.run 以恢复默认行为。
➜ failing-tests-dont-break-build xsbt test; echo $?
JAVA_HOME=/Library/Java/JavaVirtualMachines/java8/Contents/Home
SBT_OPTS= -Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -Dfile.encoding=UTF-8
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Set current project to failing-tests-dont-break-build (in build file:/Users/jacek/sandbox/failing-tests-dont-break-build/)
[info] HelloWorldSpec
[info]
[info] The 'Hello world' string should
[info] x contain 11 characters
[error] 'Hello world' doesn't have size 12 but size 11 (HelloWorldSpec.scala:7)
[info]
[info] + start with 'Hello'
[info] + end with 'world'
[info]
[info] Total for specification HelloWorldSpec
[info] Finished in 15 ms
[info] 3 examples, 1 failure, 0 error
Exit code always 0...as you wish
[success] Total time: 1 s, completed Sep 19, 2014 9:58:09 PM
0