Property-Based Testing
ScalaCheck.
Property-Based Testing is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is Property-Based Testing?
In property-based testing you state a general property that should hold for all inputs, and the framework generates many random inputs to try to falsify it. This finds edge cases hand-written examples miss.
Example vs Property
An example test checks one case: reverse(List(1,2,3)) == List(3,2,1). A property states a rule: reversing twice yields the original list, for any list. The framework checks that rule on hundreds of generated lists.
ScalaCheck
ScalaCheck is the property-testing library for Scala. Add it as a test dependency; it integrates with ScalaTest.
libraryDependencies += "org.scalatestplus" %% "scalacheck-1-17" % "3.2.18.0" % TestforAll
The core construct is forAll: it generates random values and asserts the property body for each. Here we test that addition is commutative.
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
class AddSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyChecks {
"addition" should "be commutative" in {
forAll { (a: Int, b: Int) =>
a + b shouldBe b + a
}
}
}A Round-Trip Property
A powerful pattern is the round trip: encoding then decoding (or reversing twice) should return the original value, for any input.
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
class RevSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyChecks {
"reverse" should "be its own inverse" in {
forAll { (xs: List[Int]) =>
xs.reverse.reverse shouldBe xs
}
}
}Generators
ScalaCheck supplies Gen generators for built-in types automatically. You can also build custom ones, e.g. Gen.choose(1, 100) for a bounded integer.
import org.scalacheck.Gen
val smallInt: Gen[Int] = Gen.choose(1, 100)
val nonEmpty: Gen[List[Int]] = Gen.nonEmptyListOf(Gen.choose(0, 9))Using a Custom Generator
Pass a generator to forAll to constrain the inputs, for example only positive numbers.
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
import org.scalacheck.Gen
class PosSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyChecks {
"abs" should "stay positive" in {
forAll(Gen.choose(1, 1000)) { (n: Int) =>
math.abs(n) should be > 0
}
}
}Shrinking
When a property fails, ScalaCheck shrinks the failing input to the smallest example that still fails. Instead of a huge random list, you get a minimal counterexample that is easy to debug.
Conditional Properties
Use whenever to restrict a property to inputs that satisfy a precondition. Inputs that fail the condition are discarded.
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
class DivSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyChecks {
"division" should "hold for nonzero divisors" in {
forAll { (a: Int, b: Int) =>
whenever(b != 0) {
(a / b) * b + (a % b) shouldBe a
}
}
}
}Good Properties to Test
Useful property categories:
- Round trips: encode/decode, reverse/reverse.
- Invariants: sorting preserves length.
- Algebraic laws: commutativity, associativity, identity.
When to Use It
Property testing complements example tests; it does not replace them. Use it for pure functions with clear mathematical or structural rules. Keep targeted example tests for specific known cases and regressions.
Quick Check
Test your knowledge of property-based testing.
Recap
You learned property-based testing with ScalaCheck:
- State properties true for all inputs; the framework generates many.
forAlldrives generation;Gencreates custom inputs.wheneveradds preconditions; failures are shrunk to minimal examples.- Great for round trips, invariants, and algebraic laws.
Frequently asked questions
Is the “Property-Based Testing” lesson free?
Yes — the full text of “Property-Based Testing” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “Property-Based Testing”?
ScalaCheck. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Property-Based Testing” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- ScalaTest Styles
- Matchers
- Property-Based Testing
- Mocking and Fixtures