Some features cannot be tested by looking at inputs alone, because the same input produces different results depending on what happened before. Pressing “Cancel” on an order works on Monday and fails on Wednesday, because by Wednesday the order has shipped. Entering a wrong PIN shows an error the first two times and blocks the card the third. The system has memory, and that memory is called state. State transition testing is the technique for features like these.
The four ingredients
A state transition model has four parts, and it helps to name them precisely:
- States: the distinct conditions the object can be in, such as Created, Paid or Shipped for an order.
- Events: things that happen to it, such as a payment arriving, a courier scanning a parcel, or a customer clicking Cancel.
- Transitions: the move from one state to another caused by an event.
- Actions and guards: what the system does during a transition (send an SMS, start a refund) and any condition that must hold for it to be allowed (return requested within 7 days of delivery).
With those four, you can describe almost any workflow: a loan application, a support ticket, a KYC verification, a train ticket on a waitlist, a user account that can be active, locked or closed.
A worked model: an online order
Take a simplified order on an Indian e-commerce site. It has six states: Created, Paid, Shipped, Delivered, Cancelled and Returned. Five events can occur: pay, cancel, ship, deliver and return. The business allows six transitions:
- Created, on pay, becomes Paid.
- Created, on cancel, becomes Cancelled.
- Paid, on cancel, becomes Cancelled, with the action “initiate refund”.
- Paid, on ship, becomes Shipped.
- Shipped, on deliver, becomes Delivered.
- Delivered, on return, becomes Returned, guarded by “within 7 days of delivery”.
Drawn as a diagram, this is six boxes and six arrows. Diagrams are good for discussion with the product team. For testing, a table is better, because it shows what the diagram hides.
The state table and the 24 empty cells
Put states down the side and events across the top. Six states and five events give 30 cells. Six of them hold the valid transitions above. The other 24 are combinations the diagram never mentions, because nobody draws an arrow for something that should not happen.
| State / Event | pay | cancel | ship | deliver | return |
|---|---|---|---|---|---|
| Created | Paid | Cancelled | – | – | – |
| Paid | – | Cancelled | Shipped | – | – |
| Shipped | – | – | – | Delivered | – |
| Delivered | – | – | – | – | Returned* |
| Cancelled | – | – | – | – | – |
| Returned | – | – | – | – | – |
Each dash is a question: what should happen if this event arrives in this state? Usually the answer is “reject it and leave the state unchanged”, but some dashes hide real-world situations that the system absolutely will face:
- Cancelled + pay. The customer cancels, but the payment gateway’s success callback arrives a few seconds later. Money has been taken for a cancelled order. The system must either auto-refund or flag it. Doing nothing is the bug.
- Paid + pay. The gateway retries its callback, so the same payment event arrives twice. The order must not be charged or credited twice.
- Shipped + cancel. The customer still sees a Cancel button because their app has a stale screen. The server must refuse, and the message should make sense.
- Delivered + deliver. A courier partner sends the delivery scan twice. Is the “delivered” SMS sent twice?
These invalid-transition tests are often where the serious defects are. Valid paths get exercised by every demo; invalid ones only get exercised by networks, retries, impatient users and partner systems, which is to say by production.
How much coverage is enough
There are recognised levels of coverage for state models, and it is worth knowing what each one costs.
All states. Every state is visited at least once. Two test paths do it here: Created to Paid to Shipped to Delivered to Returned, and Created to Cancelled. This is weak, because it never exercises Paid to Cancelled, which is the transition with the refund action.
All valid transitions (sometimes called 0-switch coverage). Every arrow is exercised at least once. Three paths are enough: the long happy path, Created to Cancelled, and Created to Paid to Cancelled. This is the usual minimum for anything that matters.
All transition pairs (1-switch coverage). Every sequence of two consecutive transitions is exercised. In this model there are four such pairs: pay then ship, pay then cancel, ship then deliver, deliver then return. Pair coverage matters when the system’s behaviour depends on how it reached a state, for example if a refund calculation differs for an order cancelled straight after payment versus one cancelled after an address change.
Invalid transitions. Each of the 24 dash cells tested once, checking that the event is rejected and nothing changes. In practice, teams prioritise the ones that can really occur (duplicate callbacks, late events, stale screens) and treat the rest as lower risk.
Guards deserve their own tests
The return transition only fires within 7 days of delivery. That guard is a boundary, so it needs boundary tests: a return request on day 7 and on day 8, and a decision about what “7 days” means. Seven times 24 hours from the delivery scan? Until midnight IST on the seventh calendar day? Business users often have not thought about it until a tester asks. Time-based guards also need a way to control the clock in the test environment, otherwise someone ends up waiting a week or editing database dates by hand.
Where the technique helps most, and where it strains
State transition testing fits naturally when the requirement already talks in terms of statuses. If a Jira ticket says “the claim moves from Submitted to Under Review”, someone has a state model in mind, and drawing it out usually surfaces missing transitions within minutes. It also fits login lockouts, OTP flows (sent, expired, verified, attempts exhausted), subscription lifecycles and device workflows like an ATM or a lift controller.
It strains when the state space explodes. An order that can be partially shipped, split across sellers, partially returned and partially refunded has far more than six states, and a full table becomes unmanageable. The practical response is to model one concern at a time, for instance payment status and fulfilment status as two separate machines, and then test the handful of interactions between them that the business cares about.
Even when you never write the full table, the habit transfers. For any status-driven feature, ask two questions: which event could arrive at the wrong moment, and which event could arrive twice? Those two questions alone find a surprising share of the defects that state transition testing was designed to catch.
Written by the RP Strativa Team. The order workflow and return window are illustrative.
Leave a Reply