Module 2: Unit Tests with Claude Code

Module 2: Unit Tests with Claude Code

Module 2: Unit Tests with Claude Code

Capsule overview

In Module 1 you learned the philosophy: spec-first, tests as specification, the inversion of control. You wrote tests and Claude Code implemented. Now you're going to partially reverse the flow: you'll give existing code to Claude Code and ask it to generate unit tests. But not just any test — professional-quality tests.

Here's the problem this module solves: Claude Code can generate 50 tests in 30 seconds. If you don't know how to evaluate the quality of those tests, you have a false sense of security. 40 of those 50 tests could be trivial — they verify the obvious but ignore edge cases, boundary conditions, and error handling. This module teaches you to tell useful tests apart from decorative ones, and to use prompts that produce quality tests.

The dual value of this module: you'll master both the generation of tests with Claude Code (prompt engineering for testing) and the evaluation of generated tests (what makes a test good vs trivial). Both skills are essential for the complete TDD workflow of Module 4.


Module Context

Where are we?

This is Module 2 of the guide — the second of Phase 1 (TDD Fundamentals with AI).

What you already know (Module 1):

  • ✅ Why TDD matters more with AI than without it
  • ✅ Spec-first methodology: tests as specification
  • ✅ The inversion of control: you define specs, Claude Code implements
  • ✅ Anatomy of a good test-spec: deterministic, independent, focused
  • ✅ Your first spec→implement→validate cycle

What's missing: Knowing how to generate and evaluate professional-quality unit tests with Claude Code. The tests you wrote in Module 1 were simple and manual — now you'll scale with the AI's help.

Where are we headed?

Module 1: ✅ Spec-first philosophy (completed)
Module 2: ← YOU ARE HERE — Unit tests with Claude Code
Module 3: → Integration and E2E tests (expands to more levels)

After this module, you'll have a solid technical foundation for unit testing. Module 3 expands to integration and E2E — the other levels of the test pyramid.


Professional Objective

Master the generation and evaluation of professional unit tests using Claude Code as your testing co-pilot.

By the end of this module you'll be able to:

  • ✅ Generate quality unit tests with Claude Code using specific prompts
  • ✅ Master pytest patterns: arrange-act-assert, parametrize, basic fixtures
  • ✅ Apply naming conventions that document behavior
  • ✅ Tell trivial tests apart from useful ones — evaluate quality, not just quantity
  • ✅ Organize tests in files and directories following professional conventions
  • ✅ Critically validate AI-generated tests before trusting them

Module Progression

Module Map

CapsuleTopicWhat you'll learn
02Generate unit tests with Claude CodePrompt engineering for tests: how to ask for tests that cover happy path, edge cases, and errors
03Pytest patterns — Arrange-Act-AssertThe AAA pattern in depth, basic fixtures, conftest.py
04Parametrize — multiple scenarios@pytest.mark.parametrize to test N scenarios with a single test
05Validate AI-generated testsHow to evaluate whether Claude Code's tests are useful or trivial — the most critical skill
06Project: Generated unit test suiteGenerate a complete test suite for a utilities module with Claude Code

Learning flow

First you'll learn to generate tests with Claude Code (capsule 02) — which prompts produce quality tests and which produce generic ones. It's prompt engineering applied to testing.

Then you'll master the pytest patterns (capsules 03-04) — not as a pytest course, but as the tools you need to understand and improve what Claude Code generates. Arrange-act-assert gives you structure. Parametrize gives you efficiency.

Next you'll learn to validate the generated tests (capsule 05) — the most important skill of the module. 50 tests that pass mean nothing if they don't test real behavior. You'll learn to identify trivial tests, tests that test implementation instead of behavior, and tests that give false confidence.

Finally, in the project (capsule 06) you'll integrate it all: you receive a Python code module and use Claude Code to generate a complete suite of unit tests, evaluating and refining the quality until you have a professional suite.


Project Connection

This module's project: Generated unit test suite

You'll receive a Python utilities module (data_utils.py) with data validation, formatting, and transformation functions. Your job is to use Claude Code to generate a complete suite of unit tests, evaluating the quality and refining until you have professional behavior coverage.

The focus isn't the complexity of the code under test — it's the quality of the generated tests. A mediocre test suite has 30 tests that verify the obvious. A professional test suite has 30 tests that cover happy path, edge cases, boundary conditions, error handling, and document the behavior with clear names.

Connection with the final project (Module 8)

In the final project you'll build a complete application with TDD. Every feature will have unit tests. If the unit tests you generate are weak (trivial, no edge cases), the final project's test suite will be fragile. This module builds the skill of generating quality tests that scales to any project.


Boundaries: What This Module Will NOT Do

  • ❌ Integration tests — Covered in Module 3. Here only unit tests (isolated functions)
  • ❌ Advanced mocking — Covered in Module 6. Here only tests of pure functions
  • ❌ Coverage metrics — Covered in Module 5. Here we evaluate quality manually, not with tools
  • ❌ Advanced pytest — No fixtures with scope, no plugins, no custom markers. Only what's needed
  • ❌ A complete pytest course — This is NOT a pytest tutorial. It's "how to use Claude Code to generate tests with pytest"

Evidence of Success

By the end of this module, you'll know you succeeded if:

  • ✅ You can give code to Claude Code and get professional-quality tests (not generic ones)
  • ✅ You can identify in 30 seconds whether an AI-generated test is useful or trivial
  • ✅ You can use parametrize to reduce 10 repetitive tests to 1 test with 10 cases
  • ✅ Your tests have names that document the system's behavior
  • ✅ You completed the project: a generated and evaluated unit test suite

Summary

  • ✅ This module teaches how to generate and evaluate unit tests with Claude Code
  • ✅ The prompt determines the quality: "write tests" ≠ "write tests that cover edge cases, errors, and boundary conditions"
  • ✅ Pytest patterns (AAA, parametrize) are tools to understand and improve what Claude Code generates
  • ✅ Validating AI-generated tests is the most critical skill — quantity ≠ quality
  • ✅ The project produces a professional test suite for a utilities module

Next capsule: Generate unit tests with Claude Code — prompt engineering for testing.


Additional Resources

  1. pytest Documentation - Complete pytest reference
  2. pytest: Parametrize - Official parametrize documentation
  3. Effective Python Testing with pytest - A complete Real Python tutorial
  4. Anthropic: Claude Code Best Practices - Official best practices
  5. Python Testing with pytest (Book) - A pytest reference book

Module 2 — Testing with Claude Code Guide Quality tests: the prompt determines the result