Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 60 ยท Test Design Techniques

State Transition Testing

Learn state transition testing: model a system as states, events and guards, draw a state table, and design tests for valid and invalid transitions.

Some systems behave differently depending on what happened before. An online order can be cancelled while it is new, but not after it has been delivered. A user account can be logged in, locked or deleted, and the same click means something different in each condition. The input alone does not decide the outcome: the input plus the current state does.

State transition testing is a black-box technique for exactly this kind of system. You describe the system as a small model of states and the events that move it between them, then you derive tests from the model. It finds a kind of bug that equivalence partitioning and decision tables tend to miss: a forbidden action that quietly works because nobody checked the state.

The four parts of a model

State A condition the system stays in until something happens.
Event A trigger such as a click, a payment or a timeout.
Guard A condition that must be true for the move to happen.
Transition A move from one state to another, caused by an event.

Add an optional action (for example send an email) and you have a full state machine.

1 / 2

States, events, guards and transitions

A state is a situation the system remains in until something happens. It must be something you can observe or verify, for example "Paid" (the order screen shows a receipt and the database says paid). "Processing" is a poor state if nobody can tell whether the system is in it.

An event is what makes the system react: a user action, a message from another system, or time passing. A transition is the change from one state to another when an event occurs. A guard is an extra condition attached to a transition: "refund is allowed only within 30 days of delivery". If the guard is false, the event is rejected and the system stays where it is.

Here is a small order model. Each arrow is labelled with the event; the guard is written in the label.

flowchart LR
  N["New"] -->|"pay (payment approved)"| P["Paid"]
  N -->|"cancel"| C["Cancelled"]
  P -->|"ship"| S["Shipped"]
  P -->|"cancel"| C
  S -->|"deliver"| D["Delivered"]
  D -->|"refund (within 30 days)"| R["Refunded"]

The order model used in this lesson: six states and six transitions.

Cancelled and Refunded have no outgoing arrows. They are final states: once there, nothing else may happen.

From a diagram to a state table

Diagrams are friendly but they hide what is missing. A state table lists every state against every event, so each cell is either a valid transition or an invalid one. That grid is the real test basis, because the invalid cells are where forgotten rules live.

State pay ship deliver cancel refund
New Paid (if approved) invalid invalid Cancelled invalid
Paid invalid Shipped invalid Cancelled invalid
Shipped invalid invalid Delivered invalid invalid
Delivered invalid invalid invalid invalid Refunded (if within 30 days)
Cancelled invalid invalid invalid invalid invalid
Refunded invalid invalid invalid invalid invalid

Six states and five events give 30 cells, of which 6 are valid and 24 are invalid. A tester who only reads the diagram sees 6 things to check. A tester who reads the table sees 30.

The model can also be written as data. That is what a stand-in implementation does, and it makes the table easy to print and test. The code below is a stand-in: real systems hide the same idea inside an order service or a workflow engine, but the structure (find the transition for this state and event, check its guard, move) is the same.

JS The order state machine as a table

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Designing tests: 0-switch coverage

Coverage in this technique is counted over the model. The most common target is 0-switch coverage (also called "all transitions" or "single transition coverage"): every valid transition is exercised at least once. It is called 0-switch because there are zero extra transitions between the one under test and the next.

To get it, write one test per transition. A transition with a guard needs two tests: one with the guard true (the move happens) and one with the guard false (the move is blocked). In the order model, that is 6 transitions plus 2 extra tests for the false side of the two guards, so 8 tests.

A good test records three things: the starting state, the event (with any guard data), and the expected resulting state. Checking the resulting state is the heart of the technique. "No error shown" is not enough; the system must also be in the right place afterwards.

JS 0-switch tests: one per transition and guard

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The guard tests use values on the boundary (30 and 31 days). That is boundary value analysis working inside a state model, and it is a common pairing.

Invalid transitions

A real user, a slow network or a buggy client will send events at the wrong moment: a second "pay" click, a "ship" request for an order that was cancelled a second ago. The system must reject the event and leave the state alone. This is called negative testing of the model.

Invalid transitions deserve their own tests because they are cheap to write and expensive to miss. Imagine a developer who forgot to check the status before shipping. The happy path still works, 0-switch tests for valid transitions all pass, and a cancelled order gets shipped to a customer. Only a test that fires ship at a Cancelled order catches it.

The next stand-in builds a buggy copy of the machine in which ship is wrongly accepted from New, then runs every invalid cell of the table against both versions.

JS Test every invalid cell and catch a planted bug

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The valid-transition tests would all pass for the buggy version, because the planted bug adds a move and removes none. That is the reason the invalid cells matter.

Going beyond single transitions: N-switch coverage

Single transitions miss bugs that depend on history. A system might handle pay correctly from New, and cancel correctly from New, yet fail on the sequence pay, then cancel, because the refund of the payment was never wired in. The state after "Paid" is correct, but something changed outside the model.

N-switch coverage extends the idea: test every sequence of N+1 consecutive transitions. 1-switch means all sequences of two transitions, 2-switch means all sequences of three, and so on. The number of tests grows fast, so you apply it only to risky parts of the model, and usually you choose a few meaningful paths (such as the full happy path and the "cancel after paying" path) rather than all of them.

JS Counting 0-switch and 1-switch sequences

Press Run (or Ctrl + Enter) to see the console output here.

No output.

For this small model the counts are 6, 4 and 2. Real models with loops (such as a "retry payment" transition) grow far faster, which is why N-switch coverage is applied selectively.

Practical tips

  • Make states observable. If you cannot tell which state the system is in, you cannot write an expected result.
  • Ask what is missing. For each state, ask "what if the user does X here?" for every event. That question produces the invalid cells.
  • Test final states. Check that a cancelled or refunded order really ignores every event.
  • Combine with other techniques. Guards usually hide a boundary or a rule combination, so use boundary value analysis and decision tables on them.
  • Watch for time. Timeouts (a session that expires after 15 minutes) are events too. Test just before and just after.

State models are only as good as the requirement behind them. If the requirement does not say what happens when you cancel a shipped order, writing the question down and getting an answer is itself a valuable result of the technique.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does a guard do in a state model?

    A guard is a boolean condition on a transition. If it is false, the event is rejected and the state does not change.
  2. 2. A model has 5 states and 4 events. How many cells does its state table have?

    Every state is combined with every event, so the table has 5 x 4 = 20 cells, each valid or invalid.
  3. 3. Why test invalid transitions when all valid transitions already pass?

    A bug that accepts a forbidden event adds behaviour; the tests for the intended moves still pass, so only negative tests catch it.
  4. 4. What does 0-switch coverage require?

    0-switch (all transitions) means each single valid transition is covered. Sequences of two transitions are 1-switch.
  5. 5. A refund guard allows 30 days. Which pair of guard tests is best?

    Boundary values on each side of the limit (30 allowed, 31 blocked) are the most likely to expose off-by-one mistakes.

Summary

  • State transition testing models behaviour as states, events, guards and transitions, and is ideal when history matters.
  • A state table (every state against every event) exposes the invalid cells that diagrams hide.
  • 0-switch coverage means every valid transition is tested; guards need a test for true and for false.
  • Invalid transitions must be rejected without changing the state, and they need their own tests.
  • N-switch coverage tests sequences of transitions, so use it selectively where history is risky.

Continue with Pairwise and Combinatorial Testing.