A comprehensive property-based testing library for Scala and Java applications that enables developers to specify program properties as testable assertions and automatically generates test cases to verify these properties.
npx @tessl/cli install tessl/maven-org-scalacheck--scalacheck-2-12@1.18.0ScalaCheck is a comprehensive property-based testing library for Scala and Java applications that enables developers to specify program properties as testable assertions and automatically generates test cases to verify these properties. The library provides sophisticated data generators for creating test inputs, property combinators for building complex test scenarios, and automatic shrinking capabilities that minimize failing test cases to their essential components.
libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.18.1" % Testimport org.scalacheck._
import org.scalacheck.Prop._
import org.scalacheck.Gen._For specific functionality:
import org.scalacheck.{Gen, Prop, Arbitrary, Test}
import org.scalacheck.Prop.forAllimport org.scalacheck._
import org.scalacheck.Prop._
// Define a simple property
val propReverseReverse = forAll { (l: List[Int]) =>
l.reverse.reverse == l
}
// Check the property
propReverseReverse.check()
// Define a property with explicit generator
val smallInts = Gen.choose(0, 100)
val propSmallInts = forAll(smallInts) { n =>
n >= 0 && n <= 100
}
// Create a property collection
object StringProps extends Properties("String") {
property("concatenation") = forAll { (s1: String, s2: String) =>
(s1 + s2).length >= s1.length && (s1 + s2).length >= s2.length
}
property("reverse") = forAll { (s: String) =>
s.reverse.reverse == s
}
}
// Run all properties
StringProps.check()ScalaCheck is built around several key components:
This modular design enables everything from simple property definitions to complex stateful testing scenarios, making ScalaCheck suitable for unit testing, integration testing, and specification-based development.
Comprehensive data generation system with primitive generators, collection builders, distribution support, and powerful combinators for creating custom test data.
abstract class Gen[+T] {
def map[U](f: T => U): Gen[U]
def flatMap[U](f: T => Gen[U]): Gen[U]
def filter(p: T => Boolean): Gen[T]
def sample: Option[T]
}
object Gen {
def const[T](x: T): Gen[T]
def choose[T](min: T, max: T)(implicit num: Choose[T]): Gen[T]
def oneOf[T](xs: T*): Gen[T]
def listOf[T](g: Gen[T]): Gen[List[T]]
def frequency[T](gs: (Int, Gen[T])*): Gen[T]
}Property definition and testing system with universal/existential quantifiers, logical combinators, and comprehensive assertion capabilities.
abstract class Prop {
def &&(p: => Prop): Prop
def ||(p: => Prop): Prop
def ==>(p: => Prop): Prop
def check(): Unit
def check(params: Test.Parameters): Unit
}
object Prop {
def forAll[T, P](f: T => P)(implicit a: Arbitrary[T], pp: P => Prop): Prop
def forAll[T, P](g: Gen[T])(f: T => P)(implicit pp: P => Prop): Prop
def exists[T, P](f: T => P)(implicit a: Arbitrary[T], pp: P => Prop): Prop
def ?=[T](x: T, y: T): Prop
}Configurable test execution with parallel processing, custom reporting, seed control, and comprehensive result analysis.
object Test {
sealed abstract class Parameters {
val minSuccessfulTests: Int
val workers: Int
val testCallback: TestCallback
def withMinSuccessfulTests(n: Int): Parameters
def withWorkers(w: Int): Parameters
}
def check(params: Parameters, prop: Prop): Result
def checkProperties(params: Parameters, props: Properties): Seq[(String, Result)]
}Type-directed generator instances for automatic property parameterization, supporting primitive types, collections, and custom data structures.
abstract class Arbitrary[T] {
def arbitrary: Gen[T]
}
object Arbitrary {
def apply[T](g: => Gen[T]): Arbitrary[T]
def arbitrary[T](implicit a: Arbitrary[T]): Gen[T]
implicit val arbInt: Arbitrary[Int]
implicit val arbString: Arbitrary[String]
implicit def arbList[T: Arbitrary]: Arbitrary[List[T]]
implicit def arbOption[T: Arbitrary]: Arbitrary[Option[T]]
}Automatic minimization of failing test cases to find the smallest counterexample, with configurable shrinking strategies for all data types.
abstract class Shrink[T] {
def shrink(x: T): Stream[T]
def suchThat(f: T => Boolean): Shrink[T]
}
object Shrink {
def apply[T](s: T => Stream[T]): Shrink[T]
def shrink[T](x: T)(implicit s: Shrink[T]): Stream[T]
implicit def shrinkContainer[C[_], T: Shrink]: Shrink[C[T]]
implicit val shrinkString: Shrink[String]
}Co-generator framework enabling the generation of functions for testing higher-order functions and functional programming patterns.
trait Cogen[T] {
def perturb(seed: Seed, t: T): Seed
def contramap[S](f: S => T): Cogen[S]
}
object Cogen {
def apply[T](f: T => Long): Cogen[T]
implicit val cogenInt: Cogen[Int]
implicit val cogenString: Cogen[String]
implicit def cogenList[T: Cogen]: Cogen[List[T]]
}Framework for organizing related properties into testable suites with command-line runners and batch execution support.
abstract class Properties(name: String) {
def check(params: Test.Parameters = Test.Parameters.default): Unit
def main(args: Array[String]): Unit
def include(ps: Properties): Unit
val property: PropertySpecifier
}
class PropertySpecifier {
def update(propName: String, p: => Prop): Unit
}Advanced testing framework for stateful systems using command sequences, state machines, and concurrent execution scenarios.
trait Commands {
type State
type Sut
def canCreateNewSut(newState: State, initSuts: Traversable[State], runningSuts: Traversable[Sut]): Boolean
def newSut(state: State): Sut
def genInitialState: Gen[State]
def genCommand(state: State): Gen[Command]
trait Command {
type Result
def run(sut: Sut): Result
def nextState(state: State): State
def preCondition(state: State): Boolean
def postCondition(state: State, result: Try[Result]): Prop
}
}// Generator parameters for controlling test data generation
sealed abstract class Gen.Parameters {
val size: Int
val initialSeed: Option[Seed]
val useLegacyShrinking: Boolean
}
// Property evaluation result
case class Prop.Result(
status: Prop.Status,
args: List[Prop.Arg[Any]],
collected: Set[Any],
labels: Set[String]
)
// Property status types
sealed trait Prop.Status
case object Prop.Proof extends Prop.Status
case object Prop.True extends Prop.Status
case object Prop.False extends Prop.Status
case object Prop.Undecided extends Prop.Status
case class Prop.Exception(e: Throwable) extends Prop.Status
// Test execution result
case class Test.Result(
status: Test.Status,
succeeded: Int,
discarded: Int,
freqMap: FreqMap[Set[Any]],
time: Long
)
// Random number generator seed
abstract class Seed {
def next: Seed
def reseed(n: Long): Seed
def toBase64: String
}