QA AND TEST AUTOMATION · PART 1 · MANUAL TESTING, PROPERLY · TECHNIQUE · 12 OF 112

State Transition Testing: Diagrams and Test Cases

How to test anything that remembers where it is: states, events, transitions and guards, drawn as a diagram and a state table, then valid transitions, invalid ones and paths through the machine. Worked on the ShopRung order life cycle (placed, paid, shipped, delivered, cancelled) with the real status codes and messages.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 14

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • States, events, transitions, guards
  • diagram and state table
  • valid, invalid, guards, risky paths
  • check side effects

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.

Cancel an order
  • 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.

The vocabulary
  • 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.

ORD-4
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.

State x event
           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.

Valid transition tests
  • 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.

Invalid transition tests
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.

Guard tests (ORD-5, ORD-7)
  • 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.

1-switch: pairs of moves
  • 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

Hidden states

Many features are state machines that nobody drew. A login session goes from logged out to logged in to expired after two hours. A ShopRung cart goes from empty, to items, to coupon applied, to coupon no longer applies when the cart shrinks. A form moves between untouched, invalid, valid and submitting. Even a bug ticket has a life cycle, which is the next episode but one. Drawing the machine before testing is a quick way to find missing rules.

Find the hidden machines
  • login: logged out → logged in → expired after 2 h (ACC-7)
  • cart: empty → has items → coupon applied → coupon no longer applies (PRICE-8)
  • a form: untouched → invalid → valid → submitting
  • a bug ticket: new → open → fixed → verified → closed
  • draw them before testing

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.

Rules of thumb
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1What is state transition testing?

A black-box technique for systems whose response depends on their current state. You model states, events, transitions and guards, then test valid transitions, invalid ones and sequences.

Q2What is the difference between a state diagram and a state table?

The diagram shows the valid transitions. The table lists every state against every event, so it also shows the invalid combinations, which become negative tests.

Q3What is 0-switch coverage?

Covering every valid single transition at least once. 1-switch coverage covers every valid pair of consecutive transitions, and so on.

Q4What should you check when an invalid transition is attempted?

That it is refused with the right error (for example 409 Conflict), and that the state and any side effects (stock, payments, emails) did not change.

Q5What is a guard condition?

A condition that must be true for a transition to happen, such as "only an admin can ship". Test each guarded transition with the guard true and false.

Q6Give examples of state machines in everyday apps.

Orders and payments, login sessions, subscriptions, a bug ticket life cycle, a traffic light, an ATM card session, a multi-step form.

Q7Why test state transitions through the API and not only the UI?

The UI may hide a button, but the API can still accept the request. Rules must be enforced on the server, so invalid transitions must be tested where they are enforced.

Q8How would you test an ATM PIN entry?

States for card inserted, PIN wrong once, twice, card blocked after the third, and access granted. Test each transition, that a correct PIN resets the counter, and that a blocked card stays blocked.