Test Overview
This test demonstrates the Test-driven development (TDD) approach by first writing a test for a simple function that adds two numbers, then implementing the function to pass the test.
This test demonstrates the Test-driven development (TDD) approach by first writing a test for a simple function that adds two numbers, then implementing the function to pass the test.
import unittest # Step 1: Write the test first class TestAddFunction(unittest.TestCase): def test_add_two_numbers(self): result = add(2, 3) self.assertEqual(result, 5) # Step 2: Implement the function to pass the test def add(a, b): return a + b if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and discovers test_add_two_numbers | Test framework ready to run tests | - | PASS |
| 2 | test_add_two_numbers calls add(2, 3) | Function add is called with inputs 2 and 3 | - | PASS |
| 3 | add function returns 5 | Function returns the sum of inputs | - | PASS |
| 4 | test_add_two_numbers asserts result == 5 | Test compares actual result with expected value 5 | assertEqual(result, 5) | PASS |
| 5 | Test runner reports test_add_two_numbers PASSED | Test completed successfully | - | PASS |