The Testing Discipline Guide — Claude Code Testing Pyramid | AI Code Toolkit
📚 Testing

The Testing Discipline Guide

How unit, integration, and E2E tests fit together with Claude Code assistance. Three subagents; clear boundaries; coverage targets that actually matter; the review discipline that catches bad tests.

By Zain Khan
28 min read read
Updated Sep 18, 2026
Guide v1.0

Testing discipline is one of the areas where AI assistance changes the economics substantially. Tests that used to be too expensive to write are now cheap. Coverage that used to be aspirational is now practical. But the change also introduces new failure modes — AI-generated tests that verify nothing, comprehensive-looking suites that miss real bugs, coverage numbers that mask brittleness. This guide is the discipline we've evolved for using Claude Code across the three test levels.

The pyramid in 2026

The testing pyramid isn't a new idea. What's new: the pyramid was always right and always too expensive for most teams to actually build. Unit tests at the base (many, fast, isolated); integration tests in the middle (some, slower, boundary-focused); E2E tests at the top (few, slow, journey-focused). Every team knew the shape; most teams didn't achieve it.

AI assistance changes the math. Unit tests that used to take 30 minutes each now take 5. Integration tests that used to require 2-3 hours of test-infrastructure setup are now setup-plus-tests in 45 minutes. E2E tests that used to be maintained by a dedicated QA engineer are now maintained by every developer who ships a feature.

The pyramid is now achievable. Which means the discipline needs to keep up. Loose discipline plus fast test-writing produces test suites that look impressive but verify nothing.

What each level actually tests

Clear boundaries prevent test suite drift. Our team's rules:

Unit tests verify single-class or single-function behavior. Dependencies are test doubles (mocks, fakes, stubs). Fast (<100ms each). Should exercise every logical branch of the function under test. Failure mode: implementation-detail testing that breaks on refactor without behavior change.

Integration tests verify component boundaries. Dependencies are real (real database via testcontainers, real HTTP layer via supertest, real message broker via embedded Kafka). Medium speed (100ms-2s each). Should exercise integration points that unit tests can't hit. Failure mode: flaky tests due to shared state or non-determinism.

E2E tests verify user journeys. Dependencies are the whole stack; browsers driven via Playwright/Cypress; APIs via HTTP; UI as-rendered. Slow (5-30s each). Should exercise workflows a user would actually perform. Failure mode: coupling to specific UI implementation; tests break on any UI change.

These boundaries matter because they determine what each subagent produces. unit-test-writer produces unit tests, not integration tests or E2E. Mixing the levels produces tests that don't fit any level well.

Unit tests with Claude

The unit-test-writer subagent is Sonnet-based; reads existing tests for patterns; produces framework-appropriate tests (Jest, Vitest, pytest, RSpec, JUnit, Go stdlib).

The rule we've evolved: tests should verify what code does, not how. Reference config emphasizes this. Enforce via:

  • Mutation testing. After Claude writes tests, introduce a bug in the code under test; verify tests fail. If tests still pass, they're not verifying behavior.
  • Refactoring resistance. Rename an internal variable; verify tests still pass without modification. If tests break on internal renames, they're testing implementation.
  • Descriptive names. Test names should be sentences: "returns null when input is empty" not "test_empty_input". Enforces behavior-focus.

Common Claude-generated unit test failures:

  • Over-mocking. Every collaborator is a mock; test verifies mock interactions; tests interaction not outcome. Fix: prefer real objects when construction is cheap.
  • Silent tests. Test doesn't actually assert anything meaningful; passes regardless of code changes. Fix: mutation testing catches these.
  • Setup sprawl. Each test has 20+ lines of setup; hard to see what's being tested. Fix: extract fixtures or factories; keep setup close to what's being tested.

Target unit test coverage: 70-90% of code paths, weighted by risk. High-risk code (money handling, security decisions, error paths) covered thoroughly; low-risk code (glue code, simple mappers) less so.

Integration tests with Claude

The integration-test-writer subagent handles the middle tier. Framework-agnostic (testcontainers, supertest, pytest-testcontainers, dockertest).

The critical decision for integration tests is test-double strategy. Options:

  • Real containers. testcontainers spins up real Postgres, Redis, Kafka for tests. Slower but catches real integration bugs.
  • Fake implementations. In-memory database (H2, sqlite), fake queues. Faster but hides some bugs.
  • Hybrid. Real DB, fake external APIs. Balances speed and coverage.

Our team uses real containers exclusively. In-memory databases have subtly different behavior (SQL dialects, transaction semantics); tests that pass against H2 sometimes fail against production Postgres. The speed cost is real (10-20% longer test runs) but the bug-catching value is worth it.

Integration test rules:

  • Shared containers, per-test data isolation. Containers spin up once for the test suite; each test uses transaction rollback (for DBs) or unique-data patterns to isolate. 100x speedup vs. per-test containers.
  • Fast setup, fast teardown. Total test suite runtime under 5 minutes for local development; under 15 minutes in CI.
  • Deterministic. No dependence on wall-clock, network, external state. If a test is flaky, fix or delete; never rerun to make green.

Target integration test coverage: every API endpoint, every database query pattern, every message consumer. Not every code path (that's unit territory); the boundaries themselves.

E2E tests with Claude

The e2e-test-writer subagent produces Playwright or Cypress tests for user journeys. Highest level of the pyramid; fewest tests; slowest runtime.

E2E discipline focuses on user journeys, not features. A journey is a sequence of steps a user takes to accomplish something. Features are technical capabilities. Testing features via E2E produces brittle tests; testing journeys produces meaningful ones.

Example: for an e-commerce app, journeys might be "browse category, add to cart, check out" and "log in, view order history, get invoice." Features underlying these (category browser, cart, checkout, auth, order display) are tested at lower levels.

E2E rules:

  • Fewer than you think. A well-tested app has 20-100 E2E tests, not 500-1000. Beyond ~100 the maintenance cost dominates.
  • Page objects or similar abstraction. Direct selector references in tests break on any UI change; abstract via page objects or React Testing Library-style queries.
  • Realistic data. Test data that resembles production; enough to exercise realistic UI states (long text, edge case dates, unicode).

Target E2E coverage: every critical user journey. Not every feature; not every path through the UI.

The three subagents together

The three test-writer subagents (unit, integration, E2E) form a testing trio. Each has clear scope; each produces tests at its level; each defers to the others outside its scope.

Workflow pattern:

  1. Feature development. Framework specialist (Next.js, Vue, Django, etc.) writes the code.
  2. Unit tests. unit-test-writer produces tests for the specific logic added.
  3. Integration tests. integration-test-writer produces boundary tests if the change touches DB, API, or messaging.
  4. E2E tests. e2e-test-writer produces E2E tests if the change is a new user journey (not for every feature; specific journeys).

Coordination via the subagent router. When to invoke each is a routing decision; test-writer descriptions make the scope clear.

Coverage goals that actually matter

Coverage numbers are a tool with limits. High coverage doesn't mean good tests; low coverage doesn't mean bad tests. But coverage does tell you what's untested.

Our coverage targets:

  • Business logic: 90%+ line coverage. Money, security, decisions.
  • Data access layer: 80%+ line coverage. Repository patterns, queries.
  • Application layer: 70%+ line coverage. Business workflows.
  • Infrastructure: 50%+ line coverage. Glue code, configuration.
  • UI components: Focus on integration/E2E rather than line coverage.

These are ceilings, not floors. If tests aren't finding bugs, adding tests won't help; investigate why existing tests miss what they miss.

Test review discipline

PR review for tests is different from PR review for code. For tests, ask:

  • Does the test fail if the code is wrong? Mutation-thinking: what code change would make this test fail? If the answer is "any change," test is coupled to implementation. If "no change," test verifies nothing.
  • Does the test name describe the behavior? "Test 1" and "test_authorization" are bad; "rejects request when user lacks admin role" is good.
  • Is the setup obvious? Can a reader see what's arranged, what's acted upon, what's asserted? If setup is 30 lines and act+assert is 3, refactor.
  • Is the test at the right level? Testing DB query correctness in a unit test with a mock is wrong level. Testing single-class behavior via E2E is wrong level.

For AI-generated tests specifically:

  • Verify the test would actually fail if code broke (introduce a bug; run test; verify red).
  • Verify the test isn't redundant with other tests (same behavior tested twice by accident).
  • Verify the test doesn't introduce new patterns if existing patterns work.

When tests hide more than they catch

Bad tests are worse than no tests. Three failure modes:

Coverage theater. High coverage numbers with tests that verify nothing. Coverage looks good; bugs still ship. Fix: mutation testing periodically to sanity-check.

Brittle tests. Tests that break on any code change; developers stop trusting them; disabled tests accumulate. Fix: enforce behavior-focused testing; delete brittle tests rather than fix them.

Slow tests. Tests that take too long to run; developers stop running them locally; only CI runs; feedback loop dies. Fix: aggressive optimization (parallelization, shared setup, faster infrastructure) or reduction in test count if optimization exhausted.

The testing culture shift

AI assistance for testing shifts the team culture in specific ways. Two big ones:

Test-writing is no longer a bottleneck. Historically, test coverage lagged because tests took forever to write. With Claude, tests take minutes. This means "we didn't have time to test" is no longer a valid excuse; the decision to skip tests is now a positive choice, not a resource constraint.

Test quality matters more. With more tests, the risk of low-quality tests goes up. Team norms need to enforce quality; PR review needs to catch bad tests; coverage-theater needs to be actively resisted. The discipline shifts from "produce tests" to "produce good tests."

What to do next

  1. Install the three test-writer subagents (unit, integration, E2E).
  2. Set the routing pattern via subagent router discipline.
  3. Establish level boundaries in your team's testing docs.
  4. Set coverage targets appropriate for each code area.
  5. Institute test review discipline that catches behavior-vs-implementation issues.
  6. Set a quarterly mutation-testing check to prevent coverage theater.

Testing discipline plus AI assistance is one of the areas where you can compound value substantially. The base-case win is meaningful; the compounded win over 6-12 months as coverage grows and confidence grows is significant. Worth the discipline.

Next in the series →

The Onboarding Playbook

24 min read

Debugging Claude Code errors? See our sister site AI Error Hub for common error messages and fixes.
Visit AI Error Hub →

Frequently asked questions

Answers to the questions readers ask about this guide.

Two approaches:

  • Incremental rewrite as you touch related code.
  • Accept debt; add discipline for new tests.

Full rewrite of legacy tests: rarely worth it.

Incremental improvement as-you-go: accumulates over time.

Reference: test-after.

TDD-strict variant: available.

  • Test-first: design benefits.
  • Test-after: more common.

Team practices TDD: use TDD-strict variant.

Not: test-after fine and often faster.

Multi-user E2E: harder; coordination between test sessions.

  • Playwright: multi-context sessions.
  • Cypress: some multi-user patterns.

Reference config: single-user E2E.

Multi-user: additional context in invocation.

Specialized tool.

Options:

  • Percy.
  • Chromatic.
  • Playwright's screenshot comparison.

Reference E2E config: doesn't include visual regression.

Separate concern.

Teams caring about visual consistency: add to E2E infrastructure.

Different strategies per level.

  • Unit — constructed in-test.
  • Integration — factories or fixtures.
  • E2E — seeded database with realistic data.

Different data at different levels for different reasons.

No.

Diminishing returns above 80-90%:

  • Last 10-20%: trivial code (getters, glue) or hard-to-test paths (rare errors).

Cost of chasing exceeds value.

Coverage: tool, not goal.

Different discipline.

Traditional tests don't work for stochastic outputs.

Use:

  • Property-based tests.
  • Statistical assertions (accuracy above 90%).
  • Snapshot tests for representative inputs.

Specialized area; reference config doesn't cover deeply.

Rough rule:

  • Per 10 unit tests: 1-2 integration, 0.1-0.2 E2E.

Absolute numbers vary; ratios more stable.

Well-tested medium codebase:

  • 5000 unit.
  • 500-1000 integration.
  • 50-100 E2E.