What Is State Transition Testing?
State transition testing models a system as a finite state machine — a system that can be in one of a limited number of states, and transitions between states in response to events. This technique is ideal for testing workflows, processes, and any system with distinct modes of operation.
Key Concepts
- State: A condition the system is in (e.g., “Logged Out”, “Active”, “Locked”)
- Transition: Movement from one state to another
- Event: Something that triggers a transition (e.g., “enter correct password”)
- Guard condition: A condition that must be true for the transition to occur
- Action: Something that happens during a transition (e.g., “send notification”)
State Transition Diagram
This diagram shows an authentication system with 4 states and 7 transitions.
Reading the Diagram
Each arrow represents a transition with the format: Event [Guard] / Action
Valid credentials→ Event that triggers the transition[attempts < 3]→ Guard condition that must be true- The target state is where the arrow points
When to Use State Transition Testing
This technique shines when:
- The system has clearly defined modes or statuses (order: pending → paid → shipped → delivered)
- User workflows have multiple paths (shopping cart, checkout process)
- Protocols have defined states (TCP connections, authentication flows)
- Hardware devices have operational modes (standby, active, error)
Building a State Transition Table
The table lists every state-event combination systematically:
| Current State | Event | Next State | Action |
|---|---|---|---|
| LoggedOut | Valid credentials | LoggedIn | Create session |
| LoggedOut | Invalid credentials (< 3) | LoggedOut | Increment counter |
| LoggedOut | Invalid credentials (= 3) | Blocked | Lock account |
| LoggedIn | Logout | LoggedOut | Destroy session |
| LoggedIn | 30 min inactivity | Locked | Save state |
| Locked | Valid credentials | LoggedIn | Restore state |
| Locked | Logout | LoggedOut | Destroy session |
| Blocked | Admin unlock | LoggedOut | Reset counter |
Identifying Invalid Transitions
The real power of the table is finding what shouldn’t happen. For every state-event pair NOT in the table, the system should either ignore the event or show an error.
| Current State | Invalid Event | Expected |
|---|---|---|
| LoggedIn | Valid credentials | Ignore (already logged in) |
| Blocked | Valid credentials | Reject (account blocked) |
| Blocked | 30 min inactivity | Ignore (not logged in) |
Test Coverage Levels
0-switch coverage: Test every valid transition at least once. Minimum coverage.
1-switch coverage: Test every pair of consecutive transitions. Catches defects where the system’s history matters.
Example 1-switch sequences:
LoggedOut → LoggedIn → Locked
LoggedOut → LoggedIn → LoggedOut
Locked → LoggedIn → Locked
Advanced State Transition Techniques
E-Commerce Order State Machine
Real-world state machines can be complex. Here’s a typical order lifecycle:
From this diagram, derive test cases for:
- Happy path: Created → PaymentPending → Paid → Processing → Shipped → Delivered
- Payment failure: Created → PaymentPending → Created (retry) → PaymentPending → Paid
- Cancellation: Created → PaymentPending → Cancelled
- Return flow: … → Delivered → ReturnRequested → Returned
- Invalid transitions: Can a “Delivered” order go back to “Processing”? It shouldn’t.
N-Switch Coverage
For critical systems, test sequences of N+1 consecutive transitions:
0-switch: Each transition once (8 transitions = 8 tests) 1-switch: Each pair of transitions (e.g., LoggedOut→LoggedIn→Locked) 2-switch: Each triple (e.g., LoggedOut→LoggedIn→Locked→LoggedIn)
Higher N catches more history-dependent defects but grows exponentially.
Concurrent State Machines
Some systems have multiple independent state machines running simultaneously. For example, a media player might have:
- Playback state: Stopped, Playing, Paused
- Connection state: Online, Offline, Reconnecting
- Download state: Idle, Downloading, Paused, Complete
Test the interaction between state machines: What happens to a download when the connection drops? What happens to playback when the download of the current track completes?
State Transition Testing for APIs
API resources often follow state machines. Test by making API calls that trigger transitions:
POST /orders → Created
POST /orders/{id}/checkout → PaymentPending
POST /orders/{id}/pay → Paid
POST /orders/{id}/cancel → 409 Conflict (can't cancel paid order)
Exercise: Design a State Transition Test Suite
Scenario: A document approval system has these states and transitions:
- Draft → Submit → Pending Review
- Pending Review → Approve → Approved
- Pending Review → Reject → Draft (with comments)
- Pending Review → Request Changes → Changes Requested
- Changes Requested → Resubmit → Pending Review
- Approved → Publish → Published
- Published → Archive → Archived
- Any state → Delete → Deleted (only by admin)
Tasks:
- Draw the state transition diagram
- Build the state transition table
- List all invalid transitions
- Design test cases for 0-switch coverage
Hint
There are 7 states (Draft, Pending Review, Changes Requested, Approved, Published, Archived, Deleted) and 8 valid transitions. For invalid transitions, think about what events shouldn’t work in each state — e.g., can you “Approve” a Draft without submitting it first?
Solution
State transition table:
| Current State | Event | Next State |
|---|---|---|
| Draft | Submit | Pending Review |
| Draft | Delete (admin) | Deleted |
| Pending Review | Approve | Approved |
| Pending Review | Reject | Draft |
| Pending Review | Request Changes | Changes Requested |
| Pending Review | Delete (admin) | Deleted |
| Changes Requested | Resubmit | Pending Review |
| Changes Requested | Delete (admin) | Deleted |
| Approved | Publish | Published |
| Approved | Delete (admin) | Deleted |
| Published | Archive | Archived |
| Published | Delete (admin) | Deleted |
| Archived | Delete (admin) | Deleted |
Key invalid transitions to test:
| State | Invalid Event | Expected |
|---|---|---|
| Draft | Approve | Error: must submit first |
| Draft | Publish | Error: not approved |
| Approved | Submit | Error: already approved |
| Archived | Publish | Error: archived |
| Deleted | Any event | Error: document deleted |
| Pending Review | Publish | Error: not yet approved |
0-switch test cases (8 tests for valid transitions + 6 for invalid):
- TC1: Draft → Submit → Pending Review
- TC2: Pending Review → Approve → Approved
- TC3: Pending Review → Reject → Draft
- TC4: Pending Review → Request Changes → Changes Requested
- TC5: Changes Requested → Resubmit → Pending Review
- TC6: Approved → Publish → Published
- TC7: Published → Archive → Archived
- TC8: Draft → Delete (admin) → Deleted
- TC9-TC14: Invalid transition tests listed above
Pro Tips
- Start with the happy path. Map the main success scenario first, then add alternative and error paths.
- Look for missing transitions. If a state has no outgoing transitions, users can get stuck. If a state has no incoming transitions (except the initial state), it’s unreachable.
- Test concurrent events. What happens if two events occur simultaneously? Race conditions in state machines are a common source of defects.
- Use state machines for test automation. Many test frameworks support state machine models for generating test sequences automatically.
- Guard conditions need BVA. If a transition has a guard like
[attempts < 3], apply boundary value analysis to the guard condition.