运行 使用 SBT 进行 JUnit 测试

Run JUnit tests with SBT

我有一个 0.13.7 SBT 项目,有几个子项目。

其中一个叫做 webapp,它在 webapp/src/test/java.

中有很多 JUnit 个测试

当运行宁:

sbt webapp/test

只有 ScalaTest 测试是 运行,但没有 JUnit 测试。

我的 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)

因此 JUnit SBT 知道支持但未加载。

调试输出:

[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@3ad42aff))
[debug] Subclass fingerprints: List((org.scalatest.Suite,false,org.scalatest.tools.Framework$$anon@97f54b))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,false,org.scalatest.tools.Framework$$anon@6a589982))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,false,org.scalatest.tools.Framework$$anon@1b95d5e6))
[debug] Subclass fingerprints: List((org.scalatest.Suite,false,org.scalatest.tools.Framework$$anon@5c997dac))
[debug] Subclass fingerprints: List((org.scalatest.Suite,false,org.scalatest.tools.Framework$$anon@406c43ef))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,false,org.scalatest.tools.Framework$$anon@282ddefc))
[debug] Annotation fingerprints: List((org.scalatest.WrapWith,false,org.scalatest.tools.Framework$$anon@4400c80))

合作对象:

相关信息:

最后,我发现,我必须将以下设置添加到子项目中:

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。

线索已由this issue给出。

如果主项目没有JUnit测试,则不需要提供所需的测试设置。

另外,为了了解失败的方法和原因,我们需要这样设置:

testOptions in Test := Seq(Tests.Argument(TestFrameworks.JUnit, "-a"))

这个关于在 SBT 中支持 JUnit 的问题已被多次询问,但框架略有不同。

多次点击让我很难找到最简单和最新的答案。

This answer by @david.perez seems clear and works with current (2018) SBT 1.1.4.

(那个特定的问题是关于冲突的 JUnit 版本。exclude("junit", "junit-dep") 可能不是必需的。)

我还会将代码复制粘贴到此处以便快速访问:

libraryDependencies ++= Seq(
  "junit" % "junit" % "4.12" % Test,
  "com.novocode" % "junit-interface" % "0.11" % Test exclude("junit", "junit-dep")
)