0
0
Testing Fundamentalstesting~10 mins

CRUD operation verification in Testing Fundamentals - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - unittest
Testing Fundamentals
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()
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test starts and sets up an empty user dictionaryEmpty dictionary for users initialized-PASS
2Create operation: Adds user1 with name 'Alice' and age 30users = {'user1': {'name': 'Alice', 'age': 30}}Check user1 exists and name is 'Alice'PASS
3Read operation: Retrieves user1 datausers unchangedCheck user1 data is not None and age is 30PASS
4Update operation: Changes user1 age to 31users = {'user1': {'name': 'Alice', 'age': 31}}Check user1 age is updated to 31PASS
5Delete operation: Removes user1 from usersusers = {}Check user1 no longer exists in usersPASS
Failure Scenario
Failing Condition: If any CRUD operation does not perform as expected, such as missing user after creation or user still present after deletion
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after the Create operation?
AUser1 age is updated to 31
BUser1 exists and has name 'Alice'
CUser1 is deleted
DUser1 data is None
Key Result
Always verify each CRUD operation separately with clear assertions to catch errors early and ensure data integrity.