如何在以sbt运行的junit4中打开断言语句? [英] How to turn on assert statements in junit4 runned with sbt?
问题描述
我在Java项目中将sbt用于开发例程,并在运行测试时使用 junit-interface . 但是默认情况下,它会忽略assert语句(断言以检查基础代码中的某些不变性).例如,该测试通过:
I use sbt for development routines in my java project and junit-interface for running tests. But by default it ignores assert statements(assert for checking some invariant in underlying code). For example this test passes:
@Test
public void test() {
// this is example; In reality assertion somewhere in code under test
assert false; // not assertTrue(or whatever) from junit
}
我的build.sbt文件非常简单:
My build.sbt file is fairly simple:
organization := "Kharandziuk"
name := "Some alg project"
version := "1.0-SNAPSHOT"
libraryDependencies ++= Seq(
"junit" % "junit" % "4.8.1" % "test",
"com.novocode" % "junit-interface" % "0.10" % "test"
)
autoScalaLibrary := false
testOptions += Tests.Argument(TestFrameworks.JUnit, "-a", "-q", "-v")
fork in run := true
javaOptions in run += "-ea"
如何在测试中启用断言语句?
How I can enable assert statements in tests?
推荐答案
您可以将其设置为单独的任务,例如. test
s:
You can set it for separate tasks, eg. test
s:
javaOptions in test += "-ea"
或简单地将其设置在任何地方:
or simply set it everywhere:
javaOptions += "-ea"
无论如何,@ MPirious是正确的,因为您通常不希望通过单元测试导致断言失败.单元测试应该沙盒您的代码逻辑
Regardless, @MPirious is right in that you typically don't want to cause assertion failures through unit testing. A unit test is supposed to sandbox your code's logic
这篇关于如何在以sbt运行的junit4中打开断言语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!