Module 6: Mocking, Fixtures and Validation Loops
Module 6: Mocking, Fixtures and Validation Loops
Module 6: Mocking, Fixtures and Validation Loops
Capsule overview
There's code you can't test directly. Your function calls the OpenAI API — do you need to make a real call in every test? Your endpoint writes to PostgreSQL — do you need a database server running? Your service sends emails — are real emails going to arrive every time you run pytest?
The answer is mocking: replacing external dependencies with controlled versions that simulate the real behavior. A mock of the OpenAI API returns a predefined response in microseconds. A mock of the database simulates inserts and queries with no disk. The tests become fast, deterministic, and cheap.
But mocking is only one part of the module. You'll also master advanced fixtures (scope, factories, a professional conftest.py) and validation loops — the automatic cycle where Claude Code runs the tests, detects failures, fixes the code, and re-runs. Validation loops are the superpower that makes agentic TDD truly automatic.
Module Context
Where are we?
This is Module 6 — the last of Phase 2 (Testing Workflows).
What you already know:
- ✅ The complete TDD workflow: red-green-refactor (Module 4)
- ✅ Coverage, edge cases, property testing (Module 5)
- ✅ Unit, integration, E2E tests (Phase 1)
What's missing: Testing code with external dependencies and automating the correction cycle with Claude Code.
Where are we headed?
Phase 1: TDD Fundamentals with AI ✅
Phase 2: Testing Workflows
├── Module 4: ✅ The complete TDD workflow
├── Module 5: ✅ Coverage and edge cases
└── Module 6: ← YOU ARE HERE — Mocking, fixtures, validation loops
Phase 3: Final Project
├── Module 7: → Professional testing strategy
└── Module 8: → Capstone project
This module closes Phase 2. With mocking and validation loops, you have all the tools for Phase 3.
Professional Objective
Master mocking to test code with external dependencies, organize fixtures professionally, and use validation loops to automate the correction cycle with Claude Code.
By the end of this module you'll be able to:
- ✅ Use
unittest.mockandpytest-mockto replace APIs, databases, the filesystem - ✅ Decide when to mock: external services (yes), internal logic (no), boundaries (it depends)
- ✅ Create advanced fixtures: scope, autouse, factories, a conftest.py per directory
- ✅ Generate realistic mocks with Claude Code
- ✅ Implement validation loops: test→failure→fix→automatic re-test
- ✅ Use factory patterns for varied, realistic test data
Module Progression
Module Map
| Capsule | Topic | What you'll learn |
|---|---|---|
| 02 | Mocking fundamentals | unittest.mock, Mock, MagicMock, patch, return_value, side_effect |
| 03 | Advanced pytest fixtures | scope, autouse, factories, a professional conftest.py, fixture composition |
| 04 | Validation loops with Claude Code | The automatic test→fix→re-test cycle, prompts for validation, when to step in |
| 05 | Mocking external services | Mocking HTTP APIs, databases, the filesystem, time, environment variables |
| 06 | Project: A validation pipeline | A pipeline with mocks of OpenAI and a DB, fixtures, complete validation loops |
Learning flow
First you'll master the base mocking technique (capsule 02): Mock, MagicMock, patch, return_value, side_effect. This is the fundamental tool.
Then you'll go deeper into advanced fixtures (capsule 03): scope, factories, a professional conftest.py. Well-organized fixtures are the difference between maintainable tests and tests nobody wants to touch.
Capsule 04 introduces validation loops: the automatic cycle where Claude Code uses the test output as feedback to iterate. This is the superpower of agentic TDD.
In capsule 05 you'll apply mocking to real services: HTTP APIs, databases, the filesystem, time. Practical scenarios you'll find in any professional project.
The project (capsule 06) integrates it all: a pipeline with mocks for external services, fixtures for test data, and automatic validation loops.
Project Connection
This module's project: A validation pipeline
You'll build a processing pipeline that depends on external services (an AI API, a database) and create a complete test suite with:
- Mocks for each external service
- Fixtures organized in conftest.py by scope
- Factory patterns for test data
- At least 1 complete validation loop with Claude Code
Connection with the final project (Module 8)
The final project needs mocks for external services, reusable fixtures, and deterministic tests. Everything you build here gets replicated directly. Validation loops will speed up your development in the final project.
Boundaries: What This Module Will NOT Do
- ❌ CI/CD integration — Introduced in Module 7
- ❌ Docker/containers for testing — Outside the scope
- ❌ Microservices testing — The focus is local mocking, not orchestration
- ❌ Advanced concurrency mocking — Thread/async mocking is an advanced topic
Evidence of Success
By the end of this module, you'll know you succeeded if:
- ✅ You can mock an external API and verify that your code uses it correctly
- ✅ You can explain when to mock and when not to (with clear heuristics)
- ✅ You can organize fixtures in conftest.py by scope and directory
- ✅ You can run a validation loop with Claude Code (test→fix→re-test)
- ✅ You completed the project: a pipeline with mocks, fixtures, and validation loops
Summary
- ✅ Mocking replaces external dependencies with controlled versions
- ✅ The decision of when to mock requires judgment: external services yes, internal logic no
- ✅ Advanced fixtures (scope, factories, conftest.py) make tests maintainable
- ✅ Validation loops automate the test→fix→re-test cycle with Claude Code
- ✅ This module closes Phase 2 — you have all the tools for the final project
Next capsule: Mocking Fundamentals — Mock, MagicMock, patch, and the base technique.
Additional Resources
- unittest.mock Documentation - The official mocking reference
- pytest-mock - A pytest plugin for mocking
- pytest Fixtures - Advanced fixtures
- Martin Fowler: Mocks Aren't Stubs - Conceptual differences
- Factory Boy - Factory patterns for testing
Module 6 — Testing with Claude Code Guide From external dependencies to deterministic tests