使用SBT构建Scala程序,我想在构建的JAR文件中禁用断言,但我想在测试期间启用断言。这包括主要类别中声明的断言。 显而易见的SBT键是:
scalacOptions in Compile ++= Seq(
"-optimise",
"-Xelide-below", "3000",
"-Xdisable-assertions")
scalacOptions in Test ++= Seq("-optimise")
不幸的是,这不起作用:JAR文件确实禁用了断言,但测试也是如此。例如,ScalaTests
it("should have assertions enabled in test classes") {
intercept[AssertionError] {
Predef.assert(false, "Assertions are enabled in test classes")
}
}
it("should have assertions enabled in main classes") {
intercept[AssertionError] {
MainClass.assertFalse
}
}
都失败并显示错误:Expected exception java.lang.AssertionError to be thrown, but no exception was thrown.
答案 0 :(得分:0)
Test中的选项附加到Compile中的选项。要查看其值,请运行sbt "show test:scalac-options"
要解决此问题,只需将构建更改为:
scalacOptions in Test := Seq("-optimise")