0
0
Testing Fundamentalstesting~10 mins

State transition testing in Testing Fundamentals - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if a simple login system correctly changes states from 'Logged Out' to 'Logged In' and back to 'Logged Out' after logout. It verifies that the system allows login with correct credentials and denies access otherwise.

Test Code - unittest
Testing Fundamentals
class LoginSystem:
    def __init__(self):
        self.state = 'Logged Out'

    def login(self, username, password):
        if username == 'user' and password == 'pass':
            self.state = 'Logged In'
            return True
        else:
            return False

    def logout(self):
        if self.state == 'Logged In':
            self.state = 'Logged Out'
            return True
        return False

import unittest

class TestLoginSystem(unittest.TestCase):
    def setUp(self):
        self.system = LoginSystem()

    def test_valid_login_and_logout(self):
        # Initial state should be Logged Out
        self.assertEqual(self.system.state, 'Logged Out')

        # Login with correct credentials
        login_result = self.system.login('user', 'pass')
        self.assertTrue(login_result)
        self.assertEqual(self.system.state, 'Logged In')

        # Logout should return to Logged Out
        logout_result = self.system.logout()
        self.assertTrue(logout_result)
        self.assertEqual(self.system.state, 'Logged Out')

    def test_invalid_login(self):
        # Attempt login with wrong password
        login_result = self.system.login('user', 'wrong')
        self.assertFalse(login_result)
        self.assertEqual(self.system.state, 'Logged Out')

if __name__ == '__main__':
    unittest.main()
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Test starts and LoginSystem instance is createdSystem state is 'Logged Out'Assert initial state is 'Logged Out'PASS
2Call login with username 'user' and password 'pass'System processes login credentialsAssert login returns True and state changes to 'Logged In'PASS
3Call logout methodSystem processes logoutAssert logout returns True and state changes to 'Logged Out'PASS
4Call login with username 'user' and wrong password 'wrong'System processes login credentialsAssert login returns False and state remains 'Logged Out'PASS
Failure Scenario
Failing Condition: Login method does not change state to 'Logged In' on correct credentials
Execution Trace Quiz - 3 Questions
Test your understanding
What is the initial state of the system before any action?
ALogged Out
BLogged In
CUnknown
DError
Key Result
State transition testing verifies that the system correctly changes from one state to another based on inputs, ensuring all valid and invalid transitions behave as expected.