Test every arrow
Your state machine has 9 transitions. How many tests prove it works? (Hint: not 1, and not 1,000.)
About 35 min · 3 goals · Checked by automated tests
1.Why should I care?
A state machine is unusually testable: each transition is one small, checkable fact. Writing one test per arrow, plus the guard boundaries, catches most mistakes before the robot ever moves.
- ◇ Write a transition table as data and a test for each transition.
- ◇ Test guard boundaries on both sides of the threshold.
- ◇ Measure transition coverage of a test suite.
2.Predict
A guard says battery ≥ 30. Which battery values are the most valuable to test?
Guess first. Being wrong here is how the surprise works.
3.Discover
In the Code Lab, write the delivery robot's rules as a list of (state, event, guard, next) tuples and a step() function. Then write one assert per rule plus asserts at battery 29 and 30. Break one rule on purpose: does a test catch it?
Can't use the interactive? Read a description instead
In the Code Lab, the rules are written as a list of tuples and a step function applies the first matching rule; tests assert the next state for each rule and for battery values 29 and 30.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
Tests exercise 12 of a machine's 16 transitions. What is the transition coverage, in percent?
5.Understand
Rules as data
Store transitions in a table (a list of tuples or a dictionary) instead of scattered if-statements. Then one generic step() function runs them, and the table itself can be printed, checked and tested.
Transition coverage
Transition coverage = transitions exercised by tests ÷ total transitions. Aim for 100 %: one test per arrow that starts in the source state, sends the event and checks the target. Add tests for important ignored events too, such as 'order while DRIVING does nothing'.
Boundaries and sequences
Test each guard on both sides of its threshold (29 and 30 for ≥ 30). Then add a few sequence tests that run realistic event chains, such as a full delivery, because some bugs only appear after the battery has changed.
Explain it another way
Checking a train timetable: for every station and every departure, does the train go where the board says?
Unit tests per transition give transition coverage; boundary-value analysis covers guards; scenario tests cover paths. Together they are a cheap, strong test strategy for discrete logic.
Safety-related controllers are often required to show that every transition was exercised by a test, and code coverage tools can report it automatically.
Worked example 1
A suite has tests for 6 of the 7 delivery-robot transitions. What is its transition coverage, and which test is most likely missing?
6/7 ≈ 86 %. Commonly the missing one is FAULT + reset → IDLE, because fault paths are the easiest to forget.
Common mix-ups
- 100 % transition coverage proves the machine is correct.
✓ It proves every arrow was exercised, not that the arrows are the right ones. Reviews and scenario tests still matter.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2
A guard is speed < 0.5 m/s. Which two test values check its boundary best?
Practice 3 · a fresh version every time
A delivery robot starts in IDLE (state 0) with its battery at 45 %. Rules: IDLE(0) + order → DRIVING(1) if battery ≥ 30 %, else → CHARGING(3). DRIVING(1) + arrived → DELIVERING(2). DRIVING(1) + bump → FAULT(4). DELIVERING(2) + done → IDLE(0) and the battery drops by 20 %. CHARGING(3) + charged → IDLE(0) with the battery at 100 %. FAULT(4) + reset → IDLE(0). Any other event in any state is ignored. Events arrive in this order: done, done, order, arrived, order, bump. Which state number is it in at the end?
Practice 4
Why store transitions in a table rather than nested if-statements?
Practice 5
All single-transition tests pass, but the robot fails after its second delivery. Which test would have caught it?
7.Apply it
Write a test suite with 100 % transition coverage for the delivery robot, plus boundary tests at 29 % and 30 % and one two-delivery sequence test. Report the coverage figure and one bug the tests caught when you broke a rule.
8.Reflect
Concepts: Testing and debugging · Super-loops and finite state machines