0
0
Testing Fundamentalstesting~15 mins

State transition testing in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify state transitions of a simple turnstile
Preconditions (1)
Step 1: Check that the turnstile is Locked
Step 2: Insert a coin
Step 3: Verify the turnstile changes to Unlocked state
Step 4: Push the turnstile
Step 5: Verify the turnstile changes back to Locked state
Step 6: Push the turnstile again
Step 7: Verify the turnstile remains Locked
Step 8: Insert a coin again
Step 9: Verify the turnstile changes to Unlocked state
✅ Expected Result: The turnstile correctly changes states according to the inputs: Locked -> Unlocked after coin, Unlocked -> Locked after push, Locked remains Locked after push without coin
Automation Requirements - unittest
Assertions Needed:
Assert initial state is Locked
Assert state changes to Unlocked after coin
Assert state changes to Locked after push
Assert state remains Locked after push without coin
Best Practices:
Use a State Machine class to represent states and transitions
Use clear method names for actions (insert_coin, push)
Use unittest assertions for validation
Keep tests independent and clear
Automated Solution
Testing Fundamentals
import unittest

class Turnstile:
    def __init__(self):
        self.state = 'Locked'

    def insert_coin(self):
        if self.state == 'Locked':
            self.state = 'Unlocked'

    def push(self):
        if self.state == 'Unlocked':
            self.state = 'Locked'

class TestTurnstileStateTransitions(unittest.TestCase):
    def setUp(self):
        self.turnstile = Turnstile()

    def test_state_transitions(self):
        # Initial state
        self.assertEqual(self.turnstile.state, 'Locked')

        # Insert coin unlocks
        self.turnstile.insert_coin()
        self.assertEqual(self.turnstile.state, 'Unlocked')

        # Push locks again
        self.turnstile.push()
        self.assertEqual(self.turnstile.state, 'Locked')

        # Push again stays locked
        self.turnstile.push()
        self.assertEqual(self.turnstile.state, 'Locked')

        # Insert coin unlocks again
        self.turnstile.insert_coin()
        self.assertEqual(self.turnstile.state, 'Unlocked')

if __name__ == '__main__':
    unittest.main()

This test uses a simple Turnstile class to model the states Locked and Unlocked.

The insert_coin method changes the state from Locked to Unlocked only.

The push method changes the state from Unlocked to Locked only.

The test case test_state_transitions checks the initial state, then simulates inserting a coin and pushing the turnstile, verifying the state changes after each action.

Assertions confirm the state is exactly as expected after each step, ensuring the state machine behaves correctly.

Common Mistakes - 3 Pitfalls
Not initializing the state before testing
Changing state incorrectly on invalid actions
Using print statements instead of assertions
Bonus Challenge

Now add data-driven testing with 3 different sequences of actions and expected final states

Show Hint