ScalaCheck: Property-based testing for Scala (original) (raw)
ScalaCheck is a library written in Scala and used for automated property-based testing of Scala or Java programs. ScalaCheck was originally inspired by the Haskell libraryQuickCheck, but has also ventured into its own.
ScalaCheck has no external dependencies other than the Scala runtime, andworks great with sbt, the Scala build tool. It is also fully integrated in the test frameworksScalaTest,specs2 andLambdaTest. You can also use ScalaCheck completely standalone, with its built-in test runner.
ScalaCheck is used by several prominent Scala projects, for example the Scala compiler and the Akkaconcurrency framework.
News
- 2025-09-06: ScalaCheck 1.19.0 is released. New
Gen.zipWithcombinator. Bug fixes forGen.pickandScalaCheckFramework. - 2024-09-15: ScalaCheck 1.18.1 is released. Maintenance release.
- 2024-04-17: ScalaCheck 1.18.0 is released. Support for Scala Native v0.5.
- 2024-04-16: ScalaCheck 1.17.1 is released. Several bug fixes. New
Gen.nonEmptyStringOfcombinator. - 2022-09-15: ScalaCheck 1.17.0 is released. Several bug fixes and improvements.
- 2022-04-07: ScalaCheck 1.16.0 is released. Support for
java.timetypes. Several bug fixes. - 2021-05-14: Scala 3.0 artifacts were published for ScalaCheck 1.15.4 and 1.15.3
- 2021-05-03: ScalaCheck 1.15.4 is released with a few small bug fixes
- 2021-02-16: ScalaCheck 1.15.3 adds support for Scala Native 0.4.0
- 2020-12-17: ScalaCheck 1.15.2 fixes some uninentional API breakage of methods
someOf,atLeastOneandpickinGen. - 2020-11-03: ScalaCheck 1.15.1is released. Fixes some uninentional API breakage of methods
someOf,atLeastOneandpickinGen. - 2020-10-30: ScalaCheck 1.15.0introduces a few enhancements and some performance improvements to string and character generation.
- 2019-12-13: ScalaCheck 1.14.3 fixed a defect with number generators. This also produces artifacts for Scala.js 1.0.0-RC2 and 0.6.31. See thechange log.
- 2019-09-25: ScalaCheck 1.14.2 fixed a major defect with Scala.js but no other changes from 1.14.1.
- 2019-09-18: ScalaCheck 1.14.1 released! This is the first release since the ScalaCheck repository was moved to the Typelevelorganisation. See the release notes.
- 2018-04-22: ScalaCheck 1.14.0! This release introduces support for deterministic testing. Seerelease notes andchange log.
- 2016-11-01: ScalaCheck 1.13.4 andScalaCheck 1.12.6 released! These releases fix a deadlock problemwith Scala 2.12.0. Also, as a problem was discovered with the previously released ScalaCheck 1.12.5 build, it is recommended that you upgrade to 1.12.6 or 1.13.4 even if you’re not using Scala 2.12.
- 2016-10-19: ScalaCheck 1.13.3 released! Seerelease notes.
- 2016-07-11: ScalaCheck 1.13.2 released! Seerelease notes.
- 2016-04-14: ScalaCheck 1.13.1 released! Seerelease notes.
- 2016-02-03: ScalaCheck 1.13.0 released! Seerelease notes.
- 2015-09-10: Snapshot builds are no longer published on this site. Instead Travisautomatically publishes all successful builds of the master branch. See documentation for more information.
Quick start
Specify some of the methods of java.lang.String like this:
import org.scalacheck.Properties
import org.scalacheck.Prop.forAll
object StringSpecification extends Properties("String") {
property("startsWith") = forAll { (a: String, b: String) =>
(a+b).startsWith(a)
}
property("concatenate") = forAll { (a: String, b: String) =>
(a+b).length > a.length && (a+b).length > b.length
}
property("substring") = forAll { (a: String, b: String, c: String) =>
(a+b+c).substring(a.length, a.length+b.length) == b
}
}If you use sbt add the following dependency to your build file:
libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.19.0" % "test"Put your ScalaCheck properties in src/test/scala, then use the test task to check them:
$ sbt test
+ String.startsWith: OK, passed 100 tests.
! String.concat: Falsified after 0 passed tests.
> ARG_0: ""
> ARG_1: ""
+ String.substring: OK, passed 100 tests.As you can see, the second property was not quite right. ScalaCheck discovers this and presents the arguments that make the property fail, two empty strings. The other two properties both pass 100 test rounds, each with a randomized set of input parameters.
You can also use ScalaCheck standalone, since it has a built-in command line test runner. Compile and run like this:
$ scalac -cp scalacheck_2.11-1.19.0.jar StringSpecification.scala
$ scala -cp .:scalacheck_2.11-1.19.0.jar StringSpecification