我可以运行 JUnit 4 从命令行测试 Scala 代码吗?

发布于 2024-08-07 02:36:25 字数 116 浏览 4 评论 0原文

如果是这样,怎么办?我还没有找到合适的咒语。

如果没有,从命令行对 Scala 代码进行单元测试的最佳方法是什么? (我是一个穴居人;必要时我会使用 IDE,但我更喜欢使用 Emacs 和命令行工具。)

If so, how? I haven't come across the proper incantation yet.

If not, what's the best approach to unit-testing Scala code from the command line? (I'm a troglodyte; I use IDEs when I have to, but I prefer to play around using Emacs and command-line tools.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小苏打饼 2024-08-14 02:36:25

由于编译后的 Scala 只是 Java 字节码(好的,类名中有更多 $ 字符),因此它与针对 Java 代码运行 JUnit 4 测试完全相同,即从命令行将测试类作为参数传递给 org.junit.runner.JUnitCore。由于开箱即用的 JUnit 4 仅具有命令行支持,因此您甚至不必担心抑制基于 GUI 的测试运行程序。

也就是说,特定的 Scala 测试框架(ScalaTest、ScalaCheck)确实提供了更惯用的集测试用这种更实用的语言编写的代码的方法。

Since compiled Scala is just Java bytecode (OK, with a lot more $ characters in class names), it would be exactly as for running JUnit 4 tests against Java code, i.e. from the command line by passing the test classes as arguments to org.junit.runner.JUnitCore. As JUnit 4 out of the box only has command line support, you don't even have to worry about suppressing GUI based test runners.

That said, the specific Scala test frameworks (ScalaTest, ScalaCheck) do provide a more idiomatic set approach to testing code written in this more functional language.

自演自醉 2024-08-14 02:36:25

您可能对 ScalaTest 感兴趣。

ScalaTest 是一个免费、开源的
Scala 和 Java 的测试工具
程序员。它是用 Scala 编写的,
并使您能够在中编写测试
Scala 用于测试 Scala 或 Java
代码。它是在 Apache 下发布的
2.0 开源许可证。
因为不同的开发商采取
不同的创作方法
软件,没有单一的方法
测试适合每个人。在
鉴于这一现实,ScalaTest 是
旨在方便不同
测试风格。

请参阅 Runner 文档了解如何从命令行运行测试。

You may be interested in ScalaTest.

ScalaTest is a free, open-source
testing tool for Scala and Java
programmers. It is written in Scala,
and enables you to write tests in
Scala to test either Scala or Java
code. It is released under the Apache
2.0 open source license.
Because different developers take
different approaches to creating
software, no single approach to
testing is a good fit for everyone. In
light of this reality, ScalaTest is
designed to facilitate different
styles of testing.

See the Runner documentation for how to run tests from the command line.

奶茶白久 2024-08-14 02:36:25

ScalaTest 或任何其他特定于 Scala 的框架的建议非常好。不过,我想指出另一件事。

SBT

SBT 是一种构建工具,如 Ant、Maven 或 Make。它的一个有趣的方面对我们来说很重要,那就是它是基于 Scala 的。我并不是说它具有处理 Scala 代码的特殊功能或者它是用 Scala 编写的,尽管这两件事都是如此。我的意思是它使用 Scala 代码,而不是像 Maven 和 Ant 这样的 XML 作为配置源。

这本身就很有趣。就在今天,我看到了一个将测试源与程序源分离的精彩示例,我将其发布在这里只是因为它太酷了。

// on this project we keep all sources, whether they be Scala or Java, and whether they be
// regular classes or test classes, in a single src tree.
override def mainScalaSourcePath = "src"
override def mainJavaSourcePath = "src"
override def testScalaSourcePath = "src"
override def testJavaSourcePath = "src"
override def mainResourcesPath = "resources"

// distinguish main sources from test sources
def testSourceFilter =
  "Test*.scala" | "Test*.java" |
  "AbstractTest*.scala" | "AbstractTest*.java" |
  "ScalaTestRunner.scala"
def mainSourceFilter = ("*.scala" | "*.java") - testSourceFilter
override def mainSources = descendents(mainSourceRoots, mainSourceFilter)
override def testSources = descendents(testSourceRoots, testSourceFilter)

但更有趣的是,SBT 的工作方式就像一个控制台。您运行“sbt”,然后进入类似控制台的界面,您可以在其中键入“test”等命令,然后运行测试。

The suggestion of ScalaTest -- or any of the other Scala-specific frameworks, for that matter, is very good. I'd like to point to something else, though.

SBT.

SBT is a build tool, like Ant, Maven or Make. One interesting aspect of it, which will matter to us, is that it is Scala-based. I don't mean it has special capabilities to handle Scala code or that it is written in Scala, though both these things are true. I mean it uses Scala code, instead of XML like Maven and Ant, as the configuration source.

That in itself is interesting. Just today I saw a wonderful example of separating test sources from program sources, which I post here just because its so cool.

// on this project we keep all sources, whether they be Scala or Java, and whether they be
// regular classes or test classes, in a single src tree.
override def mainScalaSourcePath = "src"
override def mainJavaSourcePath = "src"
override def testScalaSourcePath = "src"
override def testJavaSourcePath = "src"
override def mainResourcesPath = "resources"

// distinguish main sources from test sources
def testSourceFilter =
  "Test*.scala" | "Test*.java" |
  "AbstractTest*.scala" | "AbstractTest*.java" |
  "ScalaTestRunner.scala"
def mainSourceFilter = ("*.scala" | "*.java") - testSourceFilter
override def mainSources = descendents(mainSourceRoots, mainSourceFilter)
override def testSources = descendents(testSourceRoots, testSourceFilter)

But what makes it even more interesting is that SBT works like a console. You run "sbt", and you get dropped into a console-like interface, from which you can type commands like, for instance, "test", and have your tests run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文