Jørgen Fogh
Jørgen Fogh

Reputation: 7656

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?

Upvotes: 15

Views: 9591

Answers (5)

nietaki
nietaki

Reputation: 9008

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.

Upvotes: 5

Eric
Eric

Reputation: 15557

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.

Upvotes: 7

gbrown
gbrown

Reputation: 51

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

Upvotes: 2

user127386
user127386

Reputation: 449

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.

Upvotes: 1

Abhinav Sarkar
Abhinav Sarkar

Reputation: 23782

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.

Upvotes: 6

Related Questions