Test-driven development writes tests before implementation, runs them to expose the missing behavior, implements the feature, and reruns the full suite to catch regressions. Tests cannot prove a program has no errors, but they provide a practical check against imagined or discovered bugs. section 2.3.5
Use the cycle incrementally:
- Add a test for an unimplemented unit and confirm that it fails. section 11.3.2
- Write the smallest implementation that makes the test pass, then run all tests. section 11.3.2
- Refactor for readability or structure while using the tests to verify behavior. section 11.5
Testing add1()
Start with def add1(n): pass, then test expected results such as add1(0) == 1, add1(1) == 2, and add1(-1) == 0. Replace pass with return n + 1 and rerun pytest. section 11.3.2
def test_add1(): assert add1(0) == 1 assert add1(1) == 2 assert add1(-1) == 0
Inspect the compact assertion-based test that drives the add1() implementation. section 11.3.2














