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

stateDiagram-v2 [*] --> LoggedOut LoggedOut --> LoggedIn: Valid credentials LoggedIn --> LoggedOut: Logout LoggedIn --> Locked: 30 min inactivity Locked --> LoggedIn: Valid credentials Locked --> LoggedOut: Logout LoggedOut --> LoggedOut: Invalid credentials [attempts < 3] LoggedOut --> Blocked: Invalid credentials [attempts = 3] Blocked --> LoggedOut: Admin unlock

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 StateEventNext StateAction
LoggedOutValid credentialsLoggedInCreate session
LoggedOutInvalid credentials (< 3)LoggedOutIncrement counter
LoggedOutInvalid credentials (= 3)BlockedLock account
LoggedInLogoutLoggedOutDestroy session
LoggedIn30 min inactivityLockedSave state
LockedValid credentialsLoggedInRestore state
LockedLogoutLoggedOutDestroy session
BlockedAdmin unlockLoggedOutReset 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 StateInvalid EventExpected
LoggedInValid credentialsIgnore (already logged in)
BlockedValid credentialsReject (account blocked)
Blocked30 min inactivityIgnore (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:

stateDiagram-v2 [*] --> Created Created --> PaymentPending: Checkout PaymentPending --> Paid: Payment success PaymentPending --> Created: Payment failed PaymentPending --> Cancelled: User cancel Paid --> Processing: Warehouse confirms Processing --> Shipped: Carrier pickup Shipped --> Delivered: Delivery confirmed Shipped --> ReturnRequested: Customer requests return Delivered --> ReturnRequested: Customer requests return ReturnRequested --> Returned: Return received Paid --> Refunded: Admin refund ReturnRequested --> Refunded: Return approved Cancelled --> [*] Delivered --> [*] Refunded --> [*]

From this diagram, derive test cases for:

  1. Happy path: Created → PaymentPending → Paid → Processing → Shipped → Delivered
  2. Payment failure: Created → PaymentPending → Created (retry) → PaymentPending → Paid
  3. Cancellation: Created → PaymentPending → Cancelled
  4. Return flow: … → Delivered → ReturnRequested → Returned
  5. 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:

  1. Draw the state transition diagram
  2. Build the state transition table
  3. List all invalid transitions
  4. 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 StateEventNext State
DraftSubmitPending Review
DraftDelete (admin)Deleted
Pending ReviewApproveApproved
Pending ReviewRejectDraft
Pending ReviewRequest ChangesChanges Requested
Pending ReviewDelete (admin)Deleted
Changes RequestedResubmitPending Review
Changes RequestedDelete (admin)Deleted
ApprovedPublishPublished
ApprovedDelete (admin)Deleted
PublishedArchiveArchived
PublishedDelete (admin)Deleted
ArchivedDelete (admin)Deleted

Key invalid transitions to test:

StateInvalid EventExpected
DraftApproveError: must submit first
DraftPublishError: not approved
ApprovedSubmitError: already approved
ArchivedPublishError: archived
DeletedAny eventError: document deleted
Pending ReviewPublishError: 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.