Test Overview
This test checks the basic Create, Read, Update, and Delete (CRUD) operations on a simple user record system. It verifies that each operation works correctly by asserting expected results after each step.
This test checks the basic Create, Read, Update, and Delete (CRUD) operations on a simple user record system. It verifies that each operation works correctly by asserting expected results after each step.
import unittest class TestCRUDOperations(unittest.TestCase): def setUp(self): # Simulate a simple in-memory user database as a dictionary self.users = {} def test_crud_operations(self): # Create operation self.users['user1'] = {'name': 'Alice', 'age': 30} self.assertIn('user1', self.users) self.assertEqual(self.users['user1']['name'], 'Alice') # Read operation user = self.users.get('user1') self.assertIsNotNone(user) self.assertEqual(user['age'], 30) # Update operation self.users['user1']['age'] = 31 self.assertEqual(self.users['user1']['age'], 31) # Delete operation del self.users['user1'] self.assertNotIn('user1', self.users) if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and sets up an empty user dictionary | Empty dictionary for users initialized | - | PASS |
| 2 | Create operation: Adds user1 with name 'Alice' and age 30 | users = {'user1': {'name': 'Alice', 'age': 30}} | Check user1 exists and name is 'Alice' | PASS |
| 3 | Read operation: Retrieves user1 data | users unchanged | Check user1 data is not None and age is 30 | PASS |
| 4 | Update operation: Changes user1 age to 31 | users = {'user1': {'name': 'Alice', 'age': 31}} | Check user1 age is updated to 31 | PASS |
| 5 | Delete operation: Removes user1 from users | users = {} | Check user1 no longer exists in users | PASS |