使用SBT运行JUnit测试 [英] Run JUnit tests with SBT
问题描述
我有一个0.13.7 SBT项目,还有几个子项目.
I have a 0.13.7 SBT project, with several sub-projects.
其中一个称为webapp
,并且在webapp/src/test/java
中具有许多JUnit
测试.
One of them is called webapp
, and it has many JUnit
tests in webapp/src/test/java
.
运行时:
sbt webapp/test
仅运行ScalaTest
测试,但不运行JUnit测试.
only the ScalaTest
tests are run, but no JUnit tests.
我的build.sbt
文件的摘要:
libraryDependencies ++= Seq(
"com.novocode" % "junit-interface" % "0.11" % Test
)
lazy val webapp = project
settings(
Seq(
projectDependencies ++= Seq(
....
"org.scalatest" %% "scalatest" % "2.2.2" % Test,
"junit" % "junit" % "4.11" % Test,
"com.novocode" % "junit-interface" % "0.11" % Test
)
): _*
)
示例JUnit测试:
import org.junit.Test;
public class CodificadorBase64Test {
@Test
public void testPlain() {
byte b[] = {64, 127, 72, 36, 100, 1, 5, 9, 123};
assertEquals("QH9IJGQBBQl7", CodificadorBase64.encode(b));
}
}
更新(更多研究):
> webapp/testFrameworks
[info] List(TestFramework(WrappedArray(org.scalacheck.ScalaCheckFramework)), TestFramework(WrappedArray(org.specs2.runner.Specs2Framework, org.specs2.runner.SpecsFramework)), TestFramework(WrappedArray(org.specs.runner.SpecsFramework)), TestFramework(WrappedArray(org.scalatest.tools.Framework, org.scalatest.tools.ScalaTestFramework)), TestFramework(WrappedArray(com.novocode.junit.JUnitFramework))
show webapp/loadedTestFrameworks
[info] Map(TestFramework(WrappedArray(
org.scalatest.tools.Framework,
org.scalatest.tools.ScalaTestFramework)
) -> org.scalatest.tools.Framework@65767aeb)
因此SBT知道JUnit
支持,但未加载.
So JUnit
support is known by SBT but not loaded.
调试输出:
[debug] Framework implementation 'org.scalacheck.ScalaCheckFramework' not present.
[debug] Framework implementation 'org.scalacheck.ScalaCheckFramework' not present.
[debug] Framework implementation 'org.scalacheck.ScalaCheckFramework' not present.
[debug] Framework implementation 'org.scalacheck.ScalaCheckFramework' not present.
[debug] Framework implementation 'org.specs2.runner.Specs2Framework' not present.
[debug] Framework implementation 'org.specs2.runner.Specs2Framework' not present.
[debug] Framework implementation 'org.specs2.runner.Specs2Framework' not present.
[debug] Framework implementation 'org.specs2.runner.Specs2Framework' not present.
[debug] Framework implementation 'org.specs2.runner.SpecsFramework' not present.
[debug] Framework implementation 'org.specs2.runner.SpecsFramework' not present.
[debug] Framework implementation 'org.specs2.runner.SpecsFramework' not present.
[debug] Framework implementation 'org.specs2.runner.SpecsFramework' not present.
[debug] Framework implementation 'org.specs.runner.SpecsFramework' not present.
[debug] Framework implementation 'org.specs.runner.SpecsFramework' not present.
[debug] Framework implementation 'org.specs.runner.SpecsFramework' not present.
[debug] Framework implementation 'org.specs.runner.SpecsFramework' not present.
[debug] Framework implementation 'com.novocode.junit.JUnitFramework' not present.
[debug] Framework implementation 'com.novocode.junit.JUnitFramework' not present.
[debug] Framework implementation 'com.novocode.junit.JUnitFramework' not present.
[debug] Framework implementation 'com.novocode.junit.JUnitFramework' not present.
[debug] Subclass fingerprints: List((org.scalatest.Suite,false,org.scalatest.tools.Framework$$anon$1@3ad42aff))
[debug] Subclass fingerprints: List((org.scalatest.Suite,false,org.scalatest.tools.Framework$$anon$1@97f54b))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,false,org.scalatest.tools.Framework$$anon$2@6a589982))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,false,org.scalatest.tools.Framework$$anon$2@1b95d5e6))
[debug] Subclass fingerprints: List((org.scalatest.Suite,false,org.scalatest.tools.Framework$$anon$1@5c997dac))
[debug] Subclass fingerprints: List((org.scalatest.Suite,false,org.scalatest.tools.Framework$$anon$1@406c43ef))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,false,org.scalatest.tools.Framework$$anon$2@282ddefc))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,false,org.scalatest.tools.Framework$$anon$2@4400c80))
使用:
- SBT 0.13.9和
- JUnit 4.x.
相关信息:
- Why don't junit tests get executed with "sbt test"?
- SBT documentation
推荐答案
最后,我发现我必须将以下设置添加到子项目中:
Finally, I've discovered, that I have to add the following settings to the subproject:
lazy val webapp = project
settings(
Seq(
projectDependencies ++= Seq(
....
"org.scalatest" %% "scalatest" % "2.2.2" % Test,
"junit" % "junit" % "4.11" % Test,
crossPaths := false,
"com.novocode" % "junit-interface" % "0.11" % Test
)
): _*
)
在子项目中声明junit-interface
依赖性非常重要,此外,将crossPaths
设置设置为false.
It is important to declare the junit-interface
dependency in the subproject, and in addition, to set to false the crossPaths
setting.
此问题给出了线索.
如果主项目没有JUnit
测试,则不需要提供所需的测试设置.
If the main project doesn't have JUnit
tests, then the needed test settings, don't need to be provided.
此外,为了了解失败的方法和原因,我们需要以下设置:
In addition, for knowing the failing method and the cause, we need this setting:
testOptions in Test := Seq(Tests.Argument(TestFrameworks.JUnit, "-a"))
这篇关于使用SBT运行JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!