ScalaCheck properties

An example-based test names one input and one output. A property names a rule that must hold for every input of a type, and ScalaCheck 1.20.0 generates the inputs: a hundred random lists for a rule about lists, a hundred numbers for a rule about numbers. When the rule fails, the library shrinks the failing case to the smallest input that still fails, which is usually the one line that explains the bug.

A property and its generator

Prop.forAll takes a function and builds a property from it; the types of the function's parameters decide which generator runs. Test.check_ runs the property with the given parameters and prints the result, and ConsoleReporter is what makes it print at all — a bare Test.check returns the result as a value instead. The file is a .sc script, so the dependency directive and the top-level values are read by scala-cli without a build file:

props.sc

//> using dep org.scalacheck::scalacheck::1.20.0 import org.scalacheck.Gen import org.scalacheck.Prop.forAll import org.scalacheck.Test import org.scalacheck.util.ConsoleReporter val params = Test.Parameters.default.withTestCallback(ConsoleReporter(1, 75)) val reverseTwice = forAll((xs: List[Int]) => xs.reverse.reverse == xs) val sortedIsOrdered = forAll { (xs: List[Int]) => xs.sorted.sliding(2).forall(pair => pair.size < 2 || pair(0) <= pair(1)) } val smallEven: Gen[Int] = Gen.choose(1, 50).map(_ * 2) val alwaysEven = forAll(smallEven)(n => n % 2 == 0) Test.check_(params, reverseTwice) Test.check_(params, sortedIsOrdered) Test.check_(params, alwaysEven)

scala-cli run --server=false props.sc

+ OK, passed 100 tests.
+ OK, passed 100 tests.
+ OK, passed 100 tests.

The third property does not use the default generator at all: Gen.choose(1, 50).map(_ * 2) is a generator of even numbers between 2 and 100, and forAll(smallEven) uses it for every case. A generator of your own is the way a property says what "any input" means for a domain where zero, an empty string or a negative length would be meaningless.

A failure and its shrinking

forAll((n: Int) => n < 10) is false for most integers, so the run finds a counter-example at once. The seed is pinned, because otherwise the numbers in the report change on every run; the last line is the original input before shrinking:

failing.sc

//> using dep org.scalacheck::scalacheck::1.20.0 import org.scalacheck.Prop.forAll import org.scalacheck.Test import org.scalacheck.rng.Seed import org.scalacheck.util.ConsoleReporter val params = Test.Parameters.default .withTestCallback(ConsoleReporter(1, 75)) .withInitialSeed(Seed(2026L)) Test.check_(params, forAll((n: Int) => n < 10))

scala-cli run --server=false failing.sc

! Falsified after 0 passed tests.
> ARG_0: 14
> ARG_0_ORIGINAL: 963539032

ARG_0: 14 is the shrunken input and ARG_0_ORIGINAL is the value the generator produced first: the library tried smaller numbers until it found the smallest one that still fails. A seed turns a failure into a case a colleague can repeat, and a property in a build keeps the seed of a failure in its report for exactly that reason. failing exits 0 because Test.check_ reports the failure instead of throwing; a test integration turns the failed result into a red test.

Related

Автор статьи: Arthur Isaev

Изображение баннера