My favorites | Sign in
Project Home Downloads Wiki
Project Information
Members
Featured
Wiki pages
Links

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

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.9.jar StringSpecification.scala

scala -cp .:scalacheck-1.9.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.9, and it can be used with Scala 2.9.0. You can get it from the downloads page or get it from 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.9.0</artifactId>
    <version>1.9</version>
  </dependency>

The source code of ScalaCheck is hosted at GitHub. For information on how to retrieve the source code and build it see ScalaCheck's GitHub page.

Bugs and feature requests

Please feel welcome to report any bugs or feature requests on ScalaCheck's GitHub page.

Powered by Google Project Hosting