16
votes

My company is beginning to write some code using Scala. I've been moved onto this project, and am a big fan of TDD, so I would like to get a unit-testing framework in place. However, the build system we're using for this project is Maven, and that's not going to change, for a variety of reasons.

I've looked at both ScalaTest and ScalaCheck, and both seem to have issues with Maven integration. Various forum posts I've seen in Google searches indicate that some of the Suites included with ScalaTest won't run under the maven-scala-plugin, and I haven't been able to find any information at all on running ScalaCheck in Maven's test goal.

Can anyone either point me at a way to get ScalaTest/ScalaCheck running reliably with Maven, or suggest an alternative?

5

5 Answers

14
votes

ScalaTest 1.0 has:

org.scalatest.junit.JUnitRunner

You can use it with JUnit's RunWith annotation. Maven likes that. There's also a Maven plugin now for ScalaTest, written by Jon-Anders Teigen. Right now you'll need to grab it from Jon-Anders github page:

http://github.com/teigen/maven-scalatest-plugin

Soon we plan to deploy it "officially" in a Maven repository.

Plugin officially deployed into Maven Central.

14
votes

I realize this has been answered but the most complete answer I found for using Scalatest with Maven, specifically via the command line mvn test, is http://jpz-log.info/archives/2009/09/29/scalatest-in-maven/

I add this information here because when googling for Scalatest and Maven setups this questions came in pretty high on the results but didn't provide a satisfactory (for my case) answer.

Basically:

  • add scala to your pom as usual as well as scalatest (version > 1.0) and junit
  • add @RunWith(classOf[JUnitRunner]) as a class annotation on your specs
  • tell maven-surefire-plugin to include **/*Spec.class

In your Spec.scala files:

import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
import org.scalatest.Spec
import org.scalatest.matchers.ShouldMatchers

@RunWith(classOf[JUnitRunner])
class AutomatonSpec extends Spec with ShouldMatchers {
    (...)
}

In your pom.xml:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <includes>
          <include>**/*Spec.class</include>
        </includes>
      </configuration>
    </plugin>
  </plugins>
</build>
11
votes

You can also use specs to declare specifications and execute them as JUnit tests with Maven. This works fine, as described here: http://code.google.com/p/specs/wiki/RunningSpecs#Run_your_specification_with_JUnit4

If you have any issue with that, please post a mail on the users mailing list.

Thanks,

Eric.

PS: you can also use ScalaTest and ScalaCheck from specs if you want to access some of these libraries features.

3
votes

To answer my own question: It appears that the JUnit4Runner package located here provides the necessary glue to integrate ScalaTest's full functionality with Maven. Also, rumor has it that the next version of ScalaTest will include something like this.

1
votes

There are some useful unit test suit for Scala: ScalaCheck ScalaCheck is a Scala implementation of the QuickCheck library for Haskell. It tries to keep the look and feel of QuickCheck as much as possible, while being open for improvements and extensions that can ease the task of property-based, randomized testing of Scala programs. Team: Rickard Nilsson. Requires: Scala 2.6.0 or newer. Documentation: User's Guide.

Rehersal Rehersal is a testing framework for Scala, intended as an easier-to-use replacement for SUnit in the standard library. Team: Raphael Cohn.

ScalaTest ScalaTest is a free, open-source testing tool for Scala and Java programmers. It is written in Scala, and enables you to write tests in Scala to test either Scala or Java code. It is released under the Apache 2.0 open source license. Team: Bill Venners.

Scala Specs Specs is a Behaviour-Driven-Design framework that provides: a simple and typed language to create specifications; matchers to specify code properties; integration with JUnit, Scalacheck, and jMock2; a pattern-like matcher; possibilities to structure and compose specifications, and to reuse examples across specs; data tables to group several data examples at once. Team: Eric Torreborre, David Bernard, Jorge Ortiz.

From: http://www.scala-lang.org/node/1209#program_testing