Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Test-Driven Development (TDD) is an established software engineering discipline where tests are written before the implementation code. Formulated by Kent Beck, TDD transforms automated tests from an afterthought into a design tool that enforces loose coupling, modularity, and high code quality.
The core methodology of TDD operates in a tight, iterative three-stage loop:
Let's build a production Token Bucket Rate Limiter using the TDD cycle.
Result: Test passes! (GREEN)
Result: Test passes! (GREEN)
threading.Lock, and add type annotations without breaking the existing test suite.pytest-covCode coverage measures the proportion of executable statements verified by automated tests. In Python, coverage is measured using the coverage package or pytest-cov:
if condition: ... else: ...) were evaluated. Branch coverage is vastly superior for identifying subtle logic flaws.| Anti-Pattern | Description | Remediation |
|---|---|---|
| Testing Implementation Details | Asserting private variables (_internal_cache) instead of public API behaviors. | Test public contracts; private mechanics should be free to refactor. |
| The 100% Coverage Illusion | Achieving high line coverage by running code without meaningful assertions. | Focus on edge cases, branch boundaries, and invariant validation. |
| Brittle Mocks | Mocking internal methods of the class under test. | Only mock external boundaries (network, disk, clock). |
| Writing Tests After Code | Writing code first and reverse-engineering tests to fit implementation bugs. | Stick strictly to the Red-Green-Refactor discipline. |
| Phase | Goal | Mindset |
|---|---|---|
| RED | Formulate a clear specification via a failing test | "What should the public interface and behavior be?" |
| GREEN | Make the test pass as rapidly as possible | "What is the simplest solution to verify correctness?" |
| REFACTOR | Clean up design and eliminate technical debt | "How can this be made cleaner while keeping tests green?" |
Writing production code before writing the failing test often results in tests that pass trivially without actually exercising the required behavior.
Write unit tests against public API contracts, not private internal helper methods (_helper). Testing private methods creates brittle test suites that break during simple refactoring.
Practice Test-Driven Development by writing a test for a roman numeral converter function before implementing the function itself.
What are the three sequential phases of the Test-Driven Development (TDD) cycle? A. Plan, Code, Ship B. Red (write failing test), Green (make test pass), Refactor (clean code) C. Compile, Link, Execute D. Mock, Patch, Assert
Why is Branch Coverage considered more rigorous than standard Line Coverage? A. Branch coverage requires less memory. B. Branch coverage verifies that both True and False pathways of every conditional branch are evaluated, whereas line coverage only checks if a line was executed. C. Branch coverage compiles tests to machine code. D. Line coverage cannot detect syntax errors.
if condition. Branch coverage ensures all logical decision paths (if, elif, else) are exercised.What is a major testing anti-pattern when writing unit tests under TDD? A. Using fixtures to manage state. B. Testing private internal implementation details rather than observable public behaviors. C. Using descriptive test names. D. Running tests in continuous integration.
What is the primary goal of the "GREEN" phase in TDD? A. To achieve perfect architectural perfection immediately. B. To write the minimal code necessary to satisfy the failing test and verify correctness as quickly as possible. C. To generate documentation for end users. D. To deploy code to production.
Which command line flag in pytest-cov enables evaluation of conditional branch paths? A. --cov-branch B. --branch-mode C. --verify-all D. --deep-coverage
--cov-branch flag instructs coverage.py and pytest-cov to measure branch coverage across all conditional statements.In the next lesson, we will continue your advanced Python journey with Project: Tested Calculator Application (13: Testing & Best Practices).
👉 Continue to Next Lesson: Project: Tested Calculator Application →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Mocking and Fixtures | Project: Tested Calculator Application |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.