ScalaCheck is a powerful tool for automatic unit testing of Scala and Java programs. It features automatic test case generation and minimization of failing test cases. ScalaCheck started out as a Scala port of the Haskell library QuickCheck, and has since evolved and been extended with features not found in Haskell QuickCheck.
Author: Rickard Nilsson
News
- 2010-02-03 - ScalaCheck built for Scala 2.8.0 Beta 1
- 2009-11-08 - New ScalaCheck builds
- 2009-10-14 - New ScalaCheck builds available
- 2009-10-07 - ScalaCheck 1.6 released
Introduction
See the UserGuide for detailed information on ScalaCheck usage, and ScalaCheck API documentation for reference information. ScalaCheckArticles provides links to some articles and blog posts about ScalaCheck. There is also a mailing list for users of ScalaCheck, http://groups.google.com/group/scalacheck. ScalaCheck is released under a BSD-like license.
Quick Start
Specify some methods of the java.util.String class with ScalaCheck:
import org.scalacheck._
object StringSpecification extends Properties("String") {
property("startsWith") = Prop.forAll((a: String, b: String) => (a+b).startsWith(a))
property("endsWith") = Prop.forAll((a: String, b: String) => (a+b).endsWith(b))
// Is this really always true?
property("concat") = Prop.forAll((a: String, b: String) =>
(a+b).length > a.length && (a+b).length > b.length
)
property("substring") = Prop.forAll((a: String, b: String) =>
(a+b).substring(a.length) == b
)
property("substring") = Prop.forAll((a: String, b: String, c: String) =>
(a+b+c).substring(a.length, a.length+b.length) == b
)
}Compile and run the tests, and see the results printed on the console:
scalac -cp scalacheck-1.6.jar StringSpecification.scala scala -cp .:scalacheck-1.6.jar StringSpecification + String.startsWith: OK, passed 100 tests. + String.endsWith: OK, passed 100 tests. ! String.concat: Falsified after 0 passed tests. > ARG_0: > ARG_1: + String.substring: OK, passed 100 tests. + String.substring: OK, passed 100 tests.
As we suspected, the third property wasn't really correct, and ScalaCheck gives us the input parameters for which is was falsified - two empty strings.
Retrieving ScalaCheck
The current release is 1.6, and it can be used with Scala 2.7.2, 2.7.3, 2.7.4, 2.7.5, 2.7.6 and 2.7.7. You can get it from the downloads page or install it directly with Scala Bazaar:
sbaz update sbaz install scalacheck
The ScalaCheck distribution that is available in sbaz is compiled with Scala 2.7.6.
ScalaCheck is also available in the http://scala-tools.org Maven repository. Add the following to your pom.xml:
<repositories>
<repository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</repository>
</repositories>
<dependency>
<groupId>org.scala-tools.testing</groupId>
<artifactId>scalacheck_2.7.7</artifactId>
<version>1.6</version>
</dependency>Use the artifact ids scalacheck_2.7.5, scalacheck_2.7.4 etc for variants compiled with different versions of Scala.
There is also an unreleased snapshot build of ScalaCheck 1.7 available on the scala-tools.org Maven snapshot repository. This build is compiled with Scala 2.8.0.Beta1-RC1 and can be used in the following way:
<repositories>
<repository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-snapshots</url>
</repository>
</repositories>
<dependency>
<groupId>org.scala-tools.testing</groupId>
<artifactId>scalacheck_2.8.0.Beta1-RC1</artifactId>
<version>1.7-SNAPSHOT</version>
</dependency>This snapshot build is also available from the downloads page.