如何在 Eclipse 中对 Scala 进行单元测试?
我正在学习 Scala,想在 Eclipse 中设置集成单元测试。 据我通过谷歌搜索得知,ScalaTest 是可行的方法,可能与 JUnit 结合使用。
您在 Eclipse 中对 Scala 进行单元测试有哪些经验?我应该使用 JUnit 运行程序还是其他东西?
I am learning Scala and would like to set up integrated unit testing in Eclipse.
As far as I can tell from googling, ScalaTest is the way to go, possibly in combination with JUnit.
What are your experiences with unit testing Scala in Eclipse? Should I use the JUnit runner or something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只是为了使答案保持最新:ScalaTest 现在附带一个 eclipse 插件,它应该处理运行来自 Eclipse 的开箱即用测试。
Just to keep the answers up to date: ScalaTest now comes with an eclipse plugin, which should handle running tests from Eclipse out-of-the-box.
在过去的几天里,我一直在尝试为我的项目找到与 scala 一起使用的单元测试组合,这就是我所发现的。
我正在使用 Scala 2.8 的候选版本,因为它修复了一些在 2.7.2 中阻碍我的错误。我最初尝试让 Scalatest 与 2.8 一起工作,但无法找到稳定的解决方案,将来这可能是更好的方法,但目前 2.8 版本似乎有太多变化。
我正在工作的配置是在我的 scala 测试代码中使用 JUnit4 注释,如下所示:
然后我使用 java JUnit 测试套件一起运行所有测试,如下所示:
唯一的附加技巧是将输出目录添加为源目录在 Eclipse 中,以便 JUnit 运行程序可以找到您的类文件。这有点像黑客,但似乎有效。
项目 -> 属性 -> Java 构建路径 -> 源 -> 添加文件夹,然后单击类(或 bin,无论您将类文件编译到何处)。
通过右键单击 AllTests.java 并选择 RunAs->Junit,这可以在 Eclipse 中正常运行
I've spent the past few days trying to find a combination of unit tests that work with scala for my project and this is what I've found.
I am using the release candidates for Scala 2.8 because it fixes a number of bugs that were blocking me in 2.7.2. I initially tried to get Scalatest to work with 2.8, but was unable to find a stable solution, in the future that may be a better way to go, but currently there appears to be too much in flux around the 2.8 release.
The configuration I have working is to use JUnit4 annotations in my scala test code like so:
Then I am using a java JUnit test suite to run all my tests together like so:
The only additional trick is to add the output directory as a source directory in Eclipse so that the JUnit runner can find your class files. This is a bit of a hack, but it seems to work.
Project->Properties->Java Build Path->Source->Add Folder and then click on classes (or bin, wherever you are compiling your class files to).
This runs fine in Eclipse by right clicking on AllTests.java and selecting RunAs->Junit
我也无法从 Eclipse 运行单元测试(诚然,我并没有尝试太多),但如果您愿意看看 IntelliJ ScalaTest 在 IDE 中的工作原理,没有任何问题。
I couldn't get unit tests running from Eclipse either (admittedly I didn't try too hard), but if you're willing to take a look at IntelliJ ScalaTest works from within the IDE with no problems.
ScalaIDE 网站上有一个 wiki 页面,介绍如何在 Eclipse 中运行 Scala 单元测试< /a>.如果您在运行规范单元测试时遇到具体问题,我鼓励您在 specs-users< 上发布消息/a> 邮件列表。
埃里克.
There is a wiki page on the ScalaIDE website on how to run Scala unit tests in Eclipse. If you have specific issues with running specs unit tests, I encourage you to post messages on the specs-users mailing list.
Eric.
我无法在 Eclipse 中使用 JUnit 运行程序运行 ScalaTest 规范。我认为这是因为缺少
@Test
带注释的方法。但是,如果您的项目有 Maven 支持,您可以使用mvn test
从命令行运行您的规范。要让 Surefire 插件检测您的规格,请在 pom 文件中使用以下配置。查看这些示例参考。
I was unable to run the ScalaTest specs with JUnit runner inside eclipse. I think this is because of the absence of the
@Test
annotated methods. However if your project has Maven support, you can run your specs from command line usingmvn test
. For Surefire plugin to detect your specs, use the following configuration in your pom file.Check out these example for reference.