使用 SBT 运行 JUnit 测试

编程入门 行业动态 更新时间:2024-10-28 06:24:15
本文介绍了使用 SBT 运行 JUnit 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 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 测试示例:

Example JUnit test:

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 知道但没有加载.

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.

相关信息:

  • 为什么junit测试不使用"sbt 测试"?
  • SBT 文档
推荐答案

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

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 测试

本文发布于:2023-07-14 22:19:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107340.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   SBT   JUnit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!