CodeBlanks Logo Icon CodeBlanks

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.

Practice 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');.

Starter code

main.dart
void main() {
  // define a sortList function (copy and sort)
  // define an isSorted function (is it ascending)
  // test several lists; print PASS if all sorted
}

Sample solution

main.dart
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');
}

Expected output

PASS: idempotent

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Number of trials

Question
for (var i = 0; i < ____; i++) { check(random()); }
Answer
for (var i = 0; i < 100; i++) { check(random()); }

Explanation: a property test typically runs 100 trials.

2 What does a property test check?

  • A single fixed value
  • Only the null case
  • Invariants on random inputs Correct answer
  • Only the empty list

Explanation: a property test checks invariants on random inputs.

3 Match the property test parts

Term Meaning
property Invariant
generator Random producer
forAll For all
shrink Shrinking

All lessons in Expert