Skip to content

Property-Based Testing ​

An example-based test checks inputs you choose. A property-based test states a rule that holds for every input, such as "reversing a list twice gives it back", and UniTest generates the inputs to check it. New to the idea? Read the introduction in the Quick Start first.

UniTest ships a self-contained property-based testing toolkit — no external dependency — and it's part of the base UniTest trait. Any test class gets forAll automatically. Call it with a body that holds for every randomly generated input; a failing run is automatically shrunk to a minimal counter-example, and the seed is reported so the run can be replayed.

scala
import wvlet.uni.test.UniTest

class ListLawsTest extends UniTest:
  test("addition is commutative") {
    forAll { (a: Int, b: Int) =>
      (a + b) shouldBe (b + a)
    }
  }

  test("reverse is an involution") {
    forAll { (xs: List[Int]) =>
      xs.reverse.reverse shouldBe xs
    }
  }

Generators ​

Generators live in wvlet.uni.test.check.Gen. Every forAll body receives randomly generated values — by default drawn from the type's implicit Arbitrary instance, or from a Gen you supply explicitly.

scala
import wvlet.uni.test.check.Gen

test("positive integers") {
  forAll(Gen.posNum[Int]) { n =>
    (n > 0) shouldBe true
  }
}

test("bounded integers") {
  forAll(Gen.chooseNum(1, 100)) { n =>
    (n >= 1 && n <= 100) shouldBe true
  }
}

Common combinators:

FactoryProduces
Gen.const(x)Always x
Gen.chooseNum[T](min, max)Uniform integer in [min, max]
Gen.posNum[T]Positive, non-zero value
Gen.oneOf(a, b, c)Uniform choice from values
Gen.oneOfGen(g1, g2)Uniform choice from generators
Gen.frequency(w1 -> g1, w2 -> g2)Weighted choice from generators
Gen.listOf(g) / Gen.nonEmptyListOf(g)List of g, size-aware
Gen.listOfN(n, g)List of exactly n elements
Gen.setOf(g) / Gen.mapOf(gK, gV)Set / Map, size-aware
Gen.option(g)Some(g) or None
Gen.alphaChar / .numChar / .alphaNumChar / .asciiCharSingle character
Gen.alphaStr / .numStr / .alphaNumStr / .asciiStrSize-aware string
Gen.identifier[A-Za-z][A-Za-z0-9]*
Gen.sized(n => g) / Gen.resize(n, g)Read or override the current size
Gen.recursive(leaf)(child => branch)Finite trees and ASTs (see below)
Gen.lzy(g)Defers g, for generators that refer to each other

Generators compose like Options: g.map, g.flatMap, g.filter, g.zip, plus for-comprehensions.

scala
val evenPositive: Gen[Int] = Gen.posNum[Int].map(_ * 2)

val labeledInt: Gen[(String, Int)] =
  for
    label <- Gen.alphaStr
    value <- Gen.chooseNum(0, 100)
  yield (label, value)

Recursive data such as trees and ASTs needs a generator that stops. Gen.recursive takes a leaf generator and builds a branch from a generator of its children:

scala
enum Expr:
  case Num(v: Int)
  case Neg(e: Expr)
  case Add(args: List[Expr])

val expr: Gen[Expr] = Gen.recursive(Gen.chooseNum(0, 9).map(Expr.Num(_))) { child =>
  Gen.oneOfGen(child.map(Expr.Neg(_)), Gen.listOf(child).map(Expr.Add(_)))
}

A value has at most size branch nodes and is at most maxDepth (default 8) branches deep, and children get half their parent's size, so lists of subtrees stay small. Leaves are the first choice, so with fuzzGen a failing tree reduces toward leaves: a planted bug in negating a sum reduces to Neg(Add(List(Num(0)))).

Arbitrary and derivation ​

Implicit Arbitrary[T] instances drive the argument-less forAll overload. Defaults ship for primitives, String, Array[Byte], List, Vector, Option, and small tuples. For your own types, use Scala 3's derives:

scala
import wvlet.uni.test.check.Arbitrary

case class Point(x: Int, y: Int) derives Arbitrary

sealed trait Shape derives Arbitrary
case class Circle(r: Int)                   extends Shape
case class Rect(w: Int, h: Int)             extends Shape
case class Triangle(a: Int, b: Int, c: Int) extends Shape

test("shapes round-trip through the renderer") {
  forAll { (s: Shape) =>
    Renderer.parse(Renderer.show(s)) shouldBe s
  }
}

Derivation is recursive: a derives Arbitrary on a sealed trait picks up each case class automatically, and fields of user types that themselves have an Arbitrary given (or a Mirror) are threaded through.

To summon a default generator explicitly, use Arbitrary.arbitrary[T]:

scala
forAll(Arbitrary.arbitrary[String]) { s =>
  s.length >= 0 shouldBe true
}

Shrinking ​

When a property fails, the runner shrinks the counter-example to a minimal value. Default shrinkers ship for numeric primitives, Char, String, Array[Byte], List, Option, and tuples up to arity 5. Only inputs from Arbitrary (forAll { (x: A) => ... }) are shrunk. With an explicit generator, as in forAll(gen), the failing input is reported as generated, since a simpler value might not be one the generator can produce.

scala
test("sum is never negative for non-negative inputs") {
  forAll { (xs: List[Int]) =>
    // Sum as Long: an Int sum of large inputs overflows
    xs.filter(_ >= 0).map(_.toLong).sum >= 0 shouldBe true
  }
}

If this property were broken, the error message would point at a minimal list — typically List(...) with one or two elements — rather than the raw random failing input.

User types get an empty shrinker by default, so forAll[MyCaseClass] compiles even without a shrink instance. To customize, provide a given:

scala
import wvlet.uni.test.check.Shrink

given Shrink[Point] = Shrink { p =>
  LazyList(Point(0, 0), Point(p.x, 0), Point(0, p.y))
}

Preconditions with ==> / implies ​

Use ==> (or its English alias implies) to discard samples that don't satisfy a precondition. The runner retries with a new sample and fails the test only if the discard budget (default: 500) runs out.

scala
test("division by non-zero is self-inverse") {
  forAll { (a: Int, b: Int) =>
    (b != 0) ==> {
      // Multiply as Long: a * b can overflow Int
      ((a.toLong * b) / b) shouldBe a.toLong
    }
  }
}

test("reads the same way in prose") {
  forAll { (xs: List[Int]) =>
    xs.nonEmpty implies {
      xs.head shouldBe xs(0)
    }
  }
}

Statistics: classify and collect ​

Break down what the generator is actually producing to make sure your property is exercising the cases you care about. Counts print after the run.

scala
test("partition respects predicate") {
  forAll(Gen.listOf(Gen.chooseNum(-100, 100))) { xs =>
    classify(xs.isEmpty, "empty")
    classify(xs.size > 50, "large")
    collect(s"size=${xs.size / 10 * 10}")
    val (neg, nonNeg) = xs.partition(_ < 0)
    (neg ++ nonNeg).sorted shouldBe xs.sorted
  }
}

Output (example):

[property] 42 × size=10, 31 × size=0, 18 × large, 9 × size=20, 7 × empty

Configuration ​

Override propertyConfig on the test class to change sample count, seed, size range, discard limit, or shrink budget for every forAll in that class. For a one-off tweak of the sample count, override minSuccessfulTests.

scala
import wvlet.uni.test.check.PropertyConfig

class HeavyPropertyTest extends UniTest:
  override protected def propertyConfig: PropertyConfig =
    PropertyConfig.default
      .withMinSuccessful(500)
      .withSize(0, 256)
      .withSeed(1_234L) // reproducible runs
KnobMethodDefault
SampleswithMinSuccessful(n)100
Discard limitwithMaxDiscarded(n)500
Size rangewithSize(min, max)0..100
Shrink budgetwithMaxShrinks(n)1000
SeedwithSeed(s)System.nanoTime()

Every failure includes the seed. To reproduce a flaky failure, copy the seed from the error message into propertyConfig.withSeed(...) and rerun — the exact same samples are drawn.

Released under the Apache 2.0 License.