如何在 Eclipse 中对 Scala 进行单元测试?

发布于 2024-09-05 00:38:40 字数 148 浏览 5 评论 0原文

我正在学习 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 技术交流群。

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

发布评论

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

评论(5

梦里的微风 2024-09-12 00:38:41

只是为了使答案保持最新: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.

怕倦 2024-09-12 00:38:41

在过去的几天里,我一直在尝试为我的项目找到与 scala 一起使用的单元测试组合,这就是我所发现的。

我正在使用 Scala 2.8 的候选版本,因为它修复了一些在 2.7.2 中阻碍我的错误。我最初尝试让 Scalatest 与 2.8 一起工作,但无法找到稳定的解决方案,将来这可能是更好的方法,但目前 2.8 版本似乎有太多变化。

我正在工作的配置是在我的 scala 测试代码中使用 JUnit4 注释,如下所示:

import org.junit._

import Assert._

class TestSuite{

  @Test def testSimple(){ 
    asserEquals("Equal",1,1)
  }
}

然后我使用 java JUnit 测试套件一起运行所有测试,如下所示:

import junit.framework.Test;
import junit.framework.TestSuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses( { 
  TestSuite.class
  })
public class AllTests {

  public static Test suite() {
    TestSuite suite = new TestSuite("Test Suite");
    return suite;
  }

}

唯一的附加技巧是将输出目录添加为源目录在 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:

import org.junit._

import Assert._

class TestSuite{

  @Test def testSimple(){ 
    asserEquals("Equal",1,1)
  }
}

Then I am using a java JUnit test suite to run all my tests together like so:

import junit.framework.Test;
import junit.framework.TestSuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses( { 
  TestSuite.class
  })
public class AllTests {

  public static Test suite() {
    TestSuite suite = new TestSuite("Test Suite");
    return suite;
  }

}

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

终止放荡 2024-09-12 00:38:41

我也无法从 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.

哑剧 2024-09-12 00:38:40

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.

临风闻羌笛 2024-09-12 00:38:40

我无法在 Eclipse 中使用 JUnit 运行程序运行 ScalaTest 规范。我认为这是因为缺少 @Test 带注释的方法。但是,如果您的项目有 Maven 支持,您可以使用 mvn test 从命令行运行您的规范。要让 Surefire 插件检测您的规格,请在 pom 文件中使用以下配置。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.5</version>
  <configuration>
   <includes>
    <include>**/*Spec.class</include>
   </includes>
  </configuration>
</plugin>

查看这些示例参考。

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 using mvn test. For Surefire plugin to detect your specs, use the following configuration in your pom file.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.5</version>
  <configuration>
   <includes>
    <include>**/*Spec.class</include>
   </includes>
  </configuration>
</plugin>

Check out these example for reference.

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