我可以运行 JUnit 4 从命令行测试 Scala 代码吗?
如果是这样,怎么办?我还没有找到合适的咒语。
如果没有,从命令行对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于编译后的 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.
您可能对 ScalaTest 感兴趣。
请参阅 Runner 文档了解如何从命令行运行测试。
You may be interested in ScalaTest.
See the Runner documentation for how to run tests from the command line.
ScalaTest 或任何其他特定于 Scala 的框架的建议非常好。不过,我想指出另一件事。
SBT。
SBT 是一种构建工具,如 Ant、Maven 或 Make。它的一个有趣的方面对我们来说很重要,那就是它是基于 Scala 的。我并不是说它具有处理 Scala 代码的特殊功能或者它是用 Scala 编写的,尽管这两件事都是如此。我的意思是它使用 Scala 代码,而不是像 Maven 和 Ant 这样的 XML 作为配置源。
这本身就很有趣。就在今天,我看到了一个将测试源与程序源分离的精彩示例,我将其发布在这里只是因为它太酷了。
但更有趣的是,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.
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.