Lesson 48 of 55
10 mins readPython Property-Based Testing with QuickCheck
In Plain English
QuickCheck revolutionizes software testing: instead of testing manually chosen sample inputs, you write mathematical properties (invariants) and QuickCheck generates hundreds of random test cases to verify them.
Deep Dive: How It Works
Property-Based Testing: Test universal rules such as reverse (reverse xs) == xs for all lists.
Automatic Shrinking: When QuickCheck finds a failing test case, it automatically shrinks the input to the smallest possible counterexample.
Arbitrary Typeclass: Generates pseudorandom values for testing custom data types.
Core Rules to Remember

Universal Invariants: Test properties that must hold true for all possible inputs.

Automated Counterexample Shrinking: Pinpoint edge cases instantly (e.g. empty lists, negative numbers).
Live Interactive Example
Hit Run Code to see it liveTesting Invariants with Property Functions
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Output Console
Click "Run Code" to view the rendered output.
How it works: Both property functions validated the invariant properties.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Verify Sort Length Invariant
Check if `length [3, 1, 2] == length [1, 2, 3]`.
Print `"Invariant holds: "` followed by its `show` string.
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.