Dart
Property tests
Learn property-based testing.
Property tests
Property-based testing checks invariants with random inputs. Instead of a fixed input, 100+ random values are generated and the property is tested.
Example: for addition the invariant is a + b == b + a (commutativity). For each random a and b this is verified. If a counterexample is found the test fails.
On failure the tool tries to find the smallest counterexample (shrinking). For example [3, -1, 2] shrinks to [0, -1]. This makes finding the root cause easier.
Invariant example
A common invariant: sort(sort(list)) == sort(list) (idempotent). Once a list is sorted, sorting again does not change it.
Another example: reverse(reverse(list)) == list (double reversal). This is verified for each random list.
A property test checks these invariants on 100+ random inputs. If even one fails the test fails and a counterexample is reported.
Exercise Test the sort invariant
Define a List<int> sortList(List<int> input) function with final out = List<int>.from(input)..sort(); return out;. Define a bool isSorted(List<int> list) function with for (var i = 1; i < list.length; i++) { if (list[i - 1] > list[i]) return false; } return true;. In main define final lists = [[3, 1, 2], [5, 4, 6], [9, 7, 8]];; var pass = true;; for each list call if (!isSorted(sortList(list))) pass = false;; finally add print(pass ? 'PASS: idempotent' : 'FAIL');.
Show example solution
List<int> sortList(List<int> input) {
final out = List<int>.from(input)..sort();
return out;
}
bool isSorted(List<int> list) {
for (var i = 1; i < list.length; i++) {
if (list[i - 1] > list[i]) return false;
}
return true;
}
void main() {
final lists = [
[3, 1, 2],
[5, 4, 6],
[9, 7, 8],
];
var pass = true;
for (final list in lists) {
if (!isSorted(sortList(list))) pass = false;
}
print(pass ? 'PASS: idempotent' : 'FAIL');
}
Test yourself
-
Number of trials
for (var i = 0; i < ____; i++) { check(random()); }
Show answer
for (var i = 0; i < 100; i++) { check(random()); }
-
What does a property test check?
- A single fixed value
- Only the null case
- Invariants on random inputs
- Only the empty list
Show answer
Invariants on random inputs — a property test checks invariants on random inputs.
-
Match the property test parts
- property
- Invariant
- generator
- Random producer
- forAll
- For all
- shrink
- Shrinking
Next lesson: Advanced mocking