tdd-best-practices
Skill5 starsChanged 31 days agoMITView on GitHub
Use when developing test-first with Red-Green-Refactor, reproducing a bug, testing Value Objects, Aggregates, Domain Events, typed Results, transaction/cache boundaries, data migrations or Criteria/query adapters, selecting test boundaries or doubles, or reviewing test quality. Prefer observable behavior and independent expected values; use refactoring guidance for legacy characterization. Documentation-only edits need no TDD cycle.
In this repository11 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- boostSkill
- data-migration-best-practicesSkill
- ddd-best-practicesSkill
- design-patterns-best-practicesSkill
- flowSkill
- fp-best-practicesSkill
- infrastructure-designSkill
- oop-best-practicesSkill
- refactoring-best-practicesSkill
- rest-api-best-practicesSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
27 rules. About 0 are common, 8 are distinctive, and 19 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈8
Distinctive · estimate
This author's own choices. Worth a look.
19
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 12
Use this skill when the main question is how to drive implementation from tests, how to structure a test suite, or how to recover …
- Distinctive: line 16
Write one failing test, then make it pass with the minimum code needed.
- Distinctive: line 18
Test behaviors, not implementations — the public API, not private methods.
- Distinctive: line 19
Keep tests as simple as the production code they verify.
- Distinctive: line 54
Start with units for logic-heavy code.
- Distinctive: line 62
Use real Value Objects and child Entities.
- Distinctive: line 68
Test the applicable public contract: semantic equality with distinct instances, exact invariant boundaries, and any exposed normalization, …
- Distinctive: line 77
Treat difficulty writing a test first as a design or boundary signal.
- Very specific: line 90
Read references/behavioral-feedback.md when selecting a test boundary, checking an independent oracle, or proving a regression.
- Very specific: line 92
Read references/tdd-core-practices.md for Red-Green-Refactor detail, FIRST properties, the test pyramid, triangulation, and baby steps.
- Very specific: line 93
Read references/test-doubles.md for the Meszaros taxonomy (Dummy, Fake, Stub, Spy, Mock), when to use each, and the classicist vs mockist …
- Very specific: line 94
Read references/tdd-schools.md for the London (outside-in/mockist) vs Chicago (inside-out/classicist) schools, BDD, and ATDD.
- Very specific: line 95
Read references/tdd-anti-patterns.md for James Carr's 15 anti-patterns, Ian Cooper's "TDD, Where Did It All Go Wrong" insights, and …
- Very specific: line 96
Read references/tdd-language-examples.md for Red-Green-Refactor walkthroughs in TypeScript, Java, Python, C#, Ruby, and PHP with their …
- Very specific: line 97
Read references/projection-testing.md for duplicate, ordering, concurrency, checkpoint, schema migration, replay/rebuild, and …
- Very specific: line 98
Read references/migration-testing.md for transformation, dirty-data, idempotent rerun, batch, checkpoint, concurrent-write, reconciliation, …
- Very specific: line 99
Read references/value-object-testing.md for semantic equality, invariant boundaries, deep immutability, normalization, optionality, …
- Very specific: line 100
Read references/aggregate-testing.md for invariant-first Aggregate tests, repository contract and adapter tests, including atomic failures, …
- Very specific: line 101
Read references/domain-event-testing.md for Aggregate event assertions, application handoff, subscriber and real-bus tests, Outbox/Inbox …
- Very specific: line 102
Read references/transaction-testing.md for atomic commit/rollback, connection propagation, concurrency, cleanup, commit ambiguity, …
- Very specific: line 103
Read references/cache-testing.md for cache hit/miss contracts, invalidation, TTL, key isolation, corruption, failure degradation, stampede, …
- Very specific: line 104
Read references/domain-error-testing.md for typed failure assertions, Result short-circuit tests, exhaustive boundary mappings, public …
- Very specific: line 105
Read references/criteria-testing.md for parser/AST tests, shared query semantics, real-adapter operator/security tests, pagination …
- Very specific: line 109
Use oop-best-practices when a class is hard to test — the design needs improvement first.
- Very specific: line 110
Use refactoring-best-practices when adding tests to untested legacy code (characterization tests).
- Very specific: line 111
Use ddd-best-practices to decide Aggregate boundaries and invariant ownership before testing them.
- Very specific: line 112
Use data-migration-best-practices to design the operational migration workflow and cutover gates before testing it.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
TDD Best Practices
Use this skill when the main question is how to drive implementation from tests, how to structure a test suite, or how to recover discipline in a codebase where tests were written after the fact.
Working Style
- Write one failing test, then make it pass with the minimum code needed.
- Refactor only when all tests are green.
- Test behaviors, not implementations — the public API, not private methods.
- Keep tests as simple as the production code they verify.
- A test suite that is hard to change is as expensive as production code that is hard to change.
The Red-Green-Refactor Cycle
RED → Write a failing test that describes the next behavior.
GREEN → Write the minimum code to make it pass.
REFACTOR → Clean up both the code and the test — no new behavior.The discipline is in the order. Never refactor on Red. On Green, add only the behavior demanded by the current failing test.
The 3 Laws (Uncle Bob)
- You may not write production code unless you have a failing unit test.
- You may not write more of a unit test than is sufficient to fail. An intentionally missing public API/type can be a valid first Red; unrelated compilation or setup failures are not.
- You may not write more production code than is sufficient to make the currently failing test pass.
Design Workflow
- Discover the test command and describe the behavior — use the repository's tooling and name the next observable outcome.
- Write a failing test — expose the intended assertion or missing new capability, rather than an unrelated setup error. A regression test must reproduce the existing defect.
- Make it pass — take the simplest path; you can clean up after.
- Refactor — remove duplication, improve names, reduce complexity.
- Repeat — the next test should be the smallest step forward.
Choosing Test Granularity
| Level | Tests | Speed | Confidence |
| Unit | Single class/function in isolation | Milliseconds | Behavior of one unit |
| Integration | Multiple real collaborators | Seconds | Module boundaries work |
| Acceptance / E2E | Full system from user perspective | Minutes | Feature works end-to-end |
Start with units for logic-heavy code. Start with acceptance tests when following outside-in TDD. Integration tests fill the seams.
Heuristics
When to mock
Mock or fake I/O boundaries in unit/application tests. Use real disposable infrastructure in adapter integration tests where SQL, collation, constraints, search analyzers, transactions, or protocol behavior are the subject.
When testing an Aggregate
Use real Value Objects and child Entities. Test commands through the root, including the rule that a rejected command leaves both state and pending events unchanged.
When testing Domain Events
Assert exact facts after Act: type/name, aggregate identity, business payload, count, and controlled metadata. Test subscriber handling separately from real-bus registration and prove durable duplicate/retry behavior with integration tests.
When testing a Value Object
Test the applicable public contract: semantic equality with distinct instances, exact invariant boundaries, and any exposed normalization, hashing, defensive-copy, operation, or serialization behavior. Inject clocks or policies instead of reading ambient context.
When a test is too big
Large setup is a signal to review fixture clarity and test scope, not proof that the test is too big. Split when multiple behaviors or failure reasons are coupled.
When tests break on every refactor
Tests are coupled to implementation, not behavior. Move the assertion to the public API surface.
When you can't write a test first
Treat difficulty writing a test first as a design or boundary signal; legacy constraints, framework coupling, or missing seams may require characterization tests before redesign.
Warning Signs
- Tests that only pass in a specific execution order.
- Mocks that reproduce the production logic (over-mocking).
- Tests with no assertion (
assertTrue(true)). - Tests named after methods, not behaviors.
- A test suite that takes more than 10 minutes to run on CI.
- Tests that break when internal details change, not when behavior changes.
References
- Read
references/behavioral-feedback.mdwhen selecting a test boundary, checking an independent oracle, or proving a regression. - Read
references/tdd-core-practices.mdfor Red-Green-Refactor detail, FIRST properties, the test pyramid, triangulation, and baby steps. - Read
references/test-doubles.mdfor the Meszaros taxonomy (Dummy, Fake, Stub, Spy, Mock), when to use each, and the classicist vs mockist distinction. - Read
references/tdd-schools.mdfor the London (outside-in/mockist) vs Chicago (inside-out/classicist) schools, BDD, and ATDD. - Read
references/tdd-anti-patterns.mdfor James Carr's 15 anti-patterns, Ian Cooper's "TDD, Where Did It All Go Wrong" insights, and recovery strategies. - Read
references/tdd-language-examples.mdfor Red-Green-Refactor walkthroughs in TypeScript, Java, Python, C#, Ruby, and PHP with their respective test frameworks. - Read
references/projection-testing.mdfor duplicate, ordering, concurrency, checkpoint, schema migration, replay/rebuild, and eventual-consistency tests for read models. - Read
references/migration-testing.mdfor transformation, dirty-data, idempotent rerun, batch, checkpoint, concurrent-write, reconciliation, cutover, and recovery tests for data migrations. - Read
references/value-object-testing.mdfor semantic equality, invariant boundaries, deep immutability, normalization, optionality, serialization, and property-based Value Object tests. - Read
references/aggregate-testing.mdfor invariant-first Aggregate tests, repository contract and adapter tests, including atomic failures, deterministic Mothers, collection equality, transaction propagation, and concurrency integration tests. - Read
references/domain-event-testing.mdfor Aggregate event assertions, application handoff, subscriber and real-bus tests, Outbox/Inbox delivery tests, Integration Event contracts, and CDC mapping tests. - Read
references/transaction-testing.mdfor atomic commit/rollback, connection propagation, concurrency, cleanup, commit ambiguity, decorators, and side-effect boundary tests. - Read
references/cache-testing.mdfor cache hit/miss contracts, invalidation, TTL, key isolation, corruption, failure degradation, stampede, and HTTP privacy tests. - Read
references/domain-error-testing.mdfor typed failure assertions, Result short-circuit tests, exhaustive boundary mappings, public redaction, unknown 500 behavior, Effect execution, and error-test-double caveats. - Read
references/criteria-testing.mdfor parser/AST tests, shared query semantics, real-adapter operator/security tests, pagination traversal, joins, nested boolean filters, and false-confidence warnings.
Related Skills
- Use
oop-best-practiceswhen a class is hard to test — the design needs improvement first. - Use
refactoring-best-practiceswhen adding tests to untested legacy code (characterization tests). - Use
ddd-best-practicesto decide Aggregate boundaries and invariant ownership before testing them. - Use
data-migration-best-practicesto design the operational migration workflow and cutover gates before testing it.
Source Influences
This skill is synthesized from:
- Test Driven Development: By Example by Kent Beck
- Growing Object-Oriented Software, Guided by Tests (GOOS) by Steve Freeman & Nat Pryce
- Working Effectively with Legacy Code by Michael Feathers
- xUnit Test Patterns by Gerard Meszaros
- Ian Cooper — "TDD, Where Did It All Go Wrong" (talk, Vimeo)
- James Carr — "TDD Anti-Patterns" (blog)
- Tim Ottinger & Jeff Langr — "Unit Tests Are FIRST" (Pragmatic Bookshelf)
- Dan North — "Introducing BDD"
- CodelyTV Aggregates course (including testing counterexamples)
- CodelyTV Value Objects course (including testing and TypeScript counterexamples)
- CodelyTV Repository Pattern course (including repository-double and integration-test counterexamples)
- CodelyTV Domain Events course (including self-asserting-double and Event Bus coverage counterexamples)
- CodelyTV Domain Modeling Errors course (including Result, Effect, stale-contract, and mock counterexamples)
- CodelyTV Criteria Pattern course (including converter, pagination, injection, and test-coverage counterexamples)
- CodelyTV Infrastructure Design Transactions course (including transaction-scope, shared-connection, deferred-side-effect, and coverage counterexamples)
- CodelyTV Infrastructure Design Cache course (including key-collision, invalidation, ETag, destructive-test, and self-asserting-double counterexamples)
- CodelyTV Four Rules of Simple Design course (including private-method coupling and passing repository-mock counterexamples)
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

