Notes
The problem
In ShopRung, a customer can cancel an order while it is placed or paid, but not once it has shipped or been delivered. So testing the cancel button once tells you almost nothing. Which state was the order in? A tester who only cancels fresh orders will never meet the ShopRung bug that lets a shipped order be cancelled, a mistake that costs real shops money: the parcel arrives and the refund goes out too.
- order is PLACED → cancel ok
- order is PAID → cancel ok
- order is SHIPPED → refused
- order is DELIVERED→ refused
- testing "cancel" once tells
- you almost nothing
- bug "cancel-shipped" lets a shipped order be cancelled
The idea
Five words. A state is a situation the system remembers, like paid. An event is something that happens, like pay, ship or cancel. A transition is the move from one state to another when an event happens. An action is what the system does on the way, like putting items back in stock. And a guard is a condition that must be true for the transition, like only an admin can ship. State transition testing tests these moves on purpose.
- state: a situation the system remembers (PAID)
- event: something that happens (pay, ship, cancel)
- transition: state + event → new state
- action: what happens on the way (stock goes back)
- guard: a condition on the transition (only an admin)
The diagram
Here is rule order four drawn as a state diagram. Five states and five valid transitions. Placed to paid on pay. Paid to shipped on ship. Shipped to delivered on deliver. And placed or paid to cancelled on cancel. Order five adds guards: the customer pays and cancels, only an admin ships and delivers. Delivered and cancelled are final states, with no way out. Drawing this is often the first time a team sees the rules clearly.
PLACED --pay--> PAID PAID --ship--> SHIPPED SHIPPED--deliver--> DELIVERED PLACED --cancel--> CANCELLED PAID --cancel--> CANCELLED guards (ORD-5): pay, cancel: the customer ship, deliver: admin only DELIVERED, CANCELLED: final
The state table
The diagram shows what may happen. The state table shows everything else too. Put every state down the side and every event across the top. Five states times four events is twenty cells. Five hold a new state. The other fifteen are invalid transitions, and each one is a question: what should happen if someone tries this? ShopRung answers with status four hundred and nine, invalid transition, and the order stays as it was.
pay ship deliver cancel PLACED PAID - - CANC PAID - SHIP - CANC SHIPPED - - DELIV - DELIVERED - - - - CANCELLED - - - - 5 states x 4 events = 20 cells 5 valid, 15 invalid (-) invalid → 409 invalid_transition
Valid transitions
First, cover every valid transition at least once. That is called zero switch coverage, or transition coverage. Five tests: pay a placed order, ship a paid one as admin, deliver a shipped one, cancel a placed one, cancel a paid one. For each, check the new status and the action. When you cancel, the items must go back in stock, which is rule order six. A status that changes while the stock does not is still a bug.
- T1 PLACED --pay--> PAID
- T2 PAID --ship(admin)--> SHIPPED
- T3 SHIPPED --deliver--> DELIV.
- T4 PLACED --cancel--> CANCELLED + stock goes back (ORD-6)
- T5 PAID --cancel--> CANCELLED
- check the new status AND the action (stock, refund)
- all 5 = 0-switch coverage
Invalid transitions
Next, the invalid transitions, where most state bugs hide. Pay an order that is already paid: ShopRung must answer four oh nine, cannot pay an order that is paid. Cancel a shipped order: refused. Ship a placed order that was never paid: refused, even for an admin. For each, check two things: the error, and that the status did not change. Switch on the cancel shipped bug and only the second test fails. That is why you test the blanks.
pay a PAID order
409 "Cannot pay an order
that is PAID"
cancel a SHIPPED order
409 "Cannot cancel an order
that is SHIPPED"
ship a PLACED order (admin)
409, still PLACED
and the status must NOT change Guards
Guards need their own tests: each guarded transition once with the guard true and once with it false. Ship as admin works. Ship as a customer is forbidden with status four hundred and three. And another guard comes from order seven: a customer can only see their own orders. If Bob tries to cancel Alice's order, ShopRung answers not found, so it does not even reveal that the order exists. With the order leak bug on, Bob can cancel it.
- ship as admin → SHIPPED
- ship as customer → 403 "Only an admin can ship an order"
- Bob cancels Alice's order → 404 "No order with id" (ORD-7: do not reveal it)
- guard false: no state change
Paths
Some bugs only show up after a sequence. Pay then cancel: does the stock come back exactly once? Cancel then try to pay: is it refused? These pairs of transitions are called one switch coverage. Longer chains are two switch, three switch and so on. The number of paths grows fast, so pick the risky ones, usually the ones that touch money or stock, plus one full happy path from placed to delivered.
- PLACED -pay-> PAID -cancel-> CANCELLED: stock back once?
- PLACED -cancel-> CANCELLED -pay-> refused?
- full path PLACED → DELIVERED with checks at each step
- n-switch = chains of n+1
- transitions: pick the risky
- ones, not all of them
Rules of thumb
Rules of thumb. Draw the diagram first, then the state table, and treat every blank as a test idea. Cover each valid transition once, and the risky invalid ones. Test each guard both ways. Always check the side effects, not only the status. Add the risky pairs. Reset the data between tests, so the starting state is known. And try two users or two browser tabs at once, because two cancels at the same time is a classic state bug.
- draw the diagram first
- build the state table: the blanks are test ideas
- every valid transition once
- the risky invalid ones
- each guard true and false
- check status AND side effects
- add risky pairs (1-switch)
- reset data between tests
- try two users at once
Common mistakes
Common mistakes. Testing only the happy path through the states. Checking the error message but not that the status stayed the same. Ignoring side effects like stock, emails or refunds. Testing only the buttons on the screen. The ShopRung page hides the cancel button on shipped orders, but the API is still there, so the shipped order rule must be tested through the API too. Forgetting that final states must stay final. And not knowing which state the test started in.
- only the happy path
- not checking the status stayed the same on refusal
- ignoring side effects (stock, emails, refunds)
- testing the UI buttons only: the API may still allow it
- forgetting final states
- not knowing the start state
Say it like this
Interviewers ask how you would test an order status flow, a payment or a ticket workflow. Say: I draw the state diagram, then a state table of every state against every event. I test each valid transition and its side effects, then the risky invalid ones, which must be refused without changing the state, then each guard both ways, and a few risky sequences. I test through the API as well as the UI, because a hidden button is not a rule.
- "I draw the state diagram, then a state table of every state and event. I test each valid transition and its side
- effects, the risky invalid
- ones, which must be refused
- without changing state, each guard both ways, and a few
- risky sequences, through the UI and the API."