views:

328

answers:

4

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?

+5  A: 

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.

abhin4v
+1  A: 

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.

It turned out that the JUnit runner worked for me out of the box in Eclipse. I just had to import the right package and write @test before the test methods, just like in Java. Haven't tried ScalaTest yet though.
Jørgen Fogh
A: 

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

gbrown
I found out that I could just right-click on a scala file containing annotated test code and pick RunAs->Junit, provided that I had added the library reference for JUnit.
Jørgen Fogh
Yeah, that seems to work for me too, though for my project running a full suite of tests is more useful. It did not work when I tried to create a .scala file with the JUnit4 annotations to create a suite of my tests.
gbrown
+2  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.

Eric