Getting Started With Property Checking
The fundamental idea behind property checking is to test the properties of the target function using random inputs.
So to test a system using property checking, two things are required:
- Properties
- Generators
A property of a system is a predicate that is always true regardless of the system's input. For example, the addition of two numbers is commutative. So it doesn't matter what numbers we pass to the addition function, for any pair of a and b, the result of add(a, b) is always the same as add(b, a):
def add(a: Int, b: Int): Int = ???
def is_add_commutative(a: Int, b: Int): Boolean =
add(a, b) == add(b, a)
The is_add_commutative predicate takes two inputs and checks if the add function is commutative or not. To check this property, we need some random integer pairs. This is where generators come in.
The Gen[A] data type is used to generate random values of type A. ZIO Test provides numerous Gen instances for common types:
import zio.test._
val intGen: Gen[Any, Int] = Gen.int
val stringGen: Gen[Any, String] = Gen.string
It is also composable, so we can combine them to generate random values of more complex types:
val stringIntGen: Gen[Any, (String, Int)] = stringGen <*> intGen
case class Person(name: String, age: Int)
val personGen: Gen[Any, Person] =
stringIntGen.map { case (name, age) => Person(name, age) }
ZIO Test provides the check function for this purpose. It takes from one to eight generators and provides their values to another function, which is a property checker. The checker can return a TestResult, such as the result of assertTrue, or an effect that produces one: a ZIO, a ZSTM or an Either:
def property[T1, T2](input1: T1, input2: T2, ...): Boolean = ???
val input1Gen: Gen[_, T1] = ???
val input2Gen: Gen[_, T2] = ???
check(input1Gen, input2Gen, ...) { (input1, input2, ...) =>
assertTrue(property(input1, input2, ...))
}
In our example, the is_add_commutative predicate takes two inputs. So we need to pass two generators of type Int to the check function:
def add(a: Int, b: Int): Int = ???
test("add is commutative") {
check(Gen.int, Gen.int) { (a, b) =>
assertTrue(add(a, b) == add(b, a))
}
}
Number of Samplesโ
In the previous example, we used check to test if the add function is commutative. In other words, we try to generate samples of random pairs of integers and try to falsify the is_add_commutative predicate. If we find a pair of integers that falsifies the predicate, then we know that the property is violated.
By default, the check function, try to generate 200 samples. We can change this by using the samples test aspect:
import zio.test._
object AdditionSpec extends ZIOSpecDefault {
def spec =
test("add is commutative") {
check(Gen.int, Gen.int) { (a, b) =>
assertTrue(add(a, b) == add(b, a))
}
} @@ TestAspect.samples(10)
}
Check Functionsโ
The zio.test package has five families of functions that run a property against generated values. They differ in where the values come from and in how many are checked:
| Function | Generators | Values checked |
|---|---|---|
check(gen1, ...)(property) | 1 to 8 | samples samples, 200 by default |
checkN(n)(gen1, ...)(property) | 1 to 6 | exactly n samples |
checkAll(gen1, ...)(property) | 1 to 8 | every value of a finite, deterministic generator, once |
checkPar(gen1, ..., parallelism)(property) | 1 to 8 | samples samples, running up to parallelism of them at the same time |
checkAllPar(gen1, ..., parallelism)(property) | 1 to 8 | every value of a finite, deterministic generator, up to parallelism at a time |
All of them stop at the first failing value and shrink it, as described on the shrinking page.
checkN runs a property for a number of samples that does not depend on the samples setting:
import zio.test._
test("addition is commutative") {
checkN(50)(Gen.int, Gen.int) { (a, b) =>
assertTrue(a + b == b + a)
}
}
checkAll is for deterministic generators, such as Gen.fromIterable, whose values we want to check exhaustively. A random generator such as Gen.int produces one value each time it runs, so checkAll checks one sample of it. With several generators, checkAll checks the cartesian product of their values:
import zio.test._
test("every digit is below ten") {
checkAll(Gen.fromIterable(0 to 9)) { digit =>
assertTrue(digit < 10)
}
}
checkPar and checkAllPar take the number of values to run at the same time as the last generator argument. They are useful when each run of the property is slow:
import zio.test._
test("parallel checks") {
checkPar(Gen.int, 4) { n =>
assertTrue(n + 0 == n)
}
}
Effectful Propertiesโ
The function that we pass to a check function can return a TestResult, as assertTrue does, or an effect that produces one. The accepted types are ZIO, ZSTM and Either. The ZIO and ZSTM environments are added to the environment of the generators, and the error of the ZIO, of the ZSTM or the Left of the Either becomes the error of the check:
import zio.Queue
import zio.test._
test("a queue returns the elements that we offered, in order") {
check(Gen.listOf(Gen.int)) { xs =>
for {
queue <- Queue.unbounded[Int]
_ <- queue.offerAll(xs)
popped <- queue.takeAll
} yield assertTrue(popped.toList == xs)
}
}
def parse(s: String): Either[String, Int] =
s.toIntOption.toRight(s"not a number: $s")
test("parsing the string of an integer gives the integer") {
check(Gen.int) { n =>
parse(n.toString).map(parsed => assertTrue(parsed == n))
}
}
If an effect fails with an error, the check treats the sample as failing and shrinks its input. If an effect dies, the check dies with that defect and does not shrink.
Test Aspects for Property Testsโ
Two test aspects change how the check functions run:
TestAspect.samples(n)sets the number of samples ofcheckandcheckPar.checkNhas its ownn, andcheckAllandcheckAllParuse every value of their generators, so they ignoresamples.TestAspect.shrinks(n)sets the maximum number of shrinking steps of all the check functions. See the shrinking page.