简体   繁体   中英

How do you unit test Scala in Eclipse?

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?

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.

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.

只是为了让答案保持最新:ScalaTest现在附带了一个eclipse插件 ,它应该能够处理从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

我无法从Eclipse运行单元测试(不可否认我没有太努力),但是如果你愿意看看IntelliJ ScalaTest在IDE中运行没有问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM