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.
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.
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()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and LoginSystem instance is created | System state is 'Logged Out' | Assert initial state is 'Logged Out' | PASS |
| 2 | Call login with username 'user' and password 'pass' | System processes login credentials | Assert login returns True and state changes to 'Logged In' | PASS |
| 3 | Call logout method | System processes logout | Assert logout returns True and state changes to 'Logged Out' | PASS |
| 4 | Call login with username 'user' and wrong password 'wrong' | System processes login credentials | Assert login returns False and state remains 'Logged Out' | PASS |