Testing principles and the pragmatic TDD loop — stack-agnostic and canon-anchored. Use when writing or reviewing tests, deciding what to test and how to name it, choosing between unit/integration/e2e, deciding whether to mock or use the real thing, judging whether coverage is enough, starting a feature test-first, or when tests are flaky, coupled or unreadable — in any language, front or back. It encodes: done means tested, the red-green-refactor loop, the test pyramid, one test file per unit...
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Testing?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/jorgecostamacia-testing-code-harness)
---
name: testing
description: >-
Testing principles and the pragmatic TDD loop — stack-agnostic and
canon-anchored. Use when writing or reviewing tests, deciding what to test
and how to name it, choosing between unit/integration/e2e, deciding whether
to mock or use the real thing, judging whether coverage is enough, starting
a feature test-first, or when tests are flaky, coupled or unreadable — in
any language, front or back. It encodes: done means tested, the
red-green-refactor loop, the test pyramid, one test file per unit, names as
specification, AAA and test independence, classicist test doubles (real
collaborators and fakes over mocks), behavior over implementation, and rule
coverage over line coverage. Test file placement and tooling are
stack-specific and live in each stack's skill.
---
# Testing & TDD (principles)
Canon-anchored: sections marked **[canon]** state the standard; **[our convention]** marks deliberate positions, each with its why. Where test files live and which framework/tooling to use is each stack skill's decision. **A rule changes only by revision. If a rule does not fit the reality of a project, stop and tell the person which rule and why. Do not bend or skip it on your own.**
## Done means tested — [our convention]
A use case, rule, or fix without tests **doesn't exist**. Tests are part of the change — same commit, same PR, same definition of done — never a follow-up task.
The one exception is a **thin driving adapter**: one that only unpacks its input and calls a use case, with no rule of its own. It can go without a unit test of its own. The composition test proves that the adapter is wired, and you watch it in production until it shows that it works. Test anything that holds a rule.
## The loop — [canon: red-green-refactor]
1. **Red** — write the failing test that states the expected behavior. Name it first: if you can't name it, you don't know what you're building yet.
2. **Green** — the simplest code that passes. Add nothing that the test does not ask for.
3. **Refactor** — with green tests: names, duplication, structure. Tests are code — they refactor too.
**[our convention]** Pragmatics over ritual: an exploratory spike can come first. But write the tests **before you consider the work done**, and check the design against them again. What is required is the end state (tested), not the ceremony.
## The test pyramid — [canon]
```text
/ e2e \ fewest — slowest, most brittle, vaguest diagnosis
/ integration \ some — real infrastructure, slower
/ unit \ most — milliseconds, failure points at the exact rule
```
- **Unit**: one unit in isolation — fast, no infrastructure, pinpoint failures.
- **Integration**: pieces collaborating with real infrastructure — a real database, a real broker, a module's wiring. **[our convention]** Build that infrastructure from the **same artifacts that production runs**: its schema scripts or migrations. Never generate it from the code's own model. A generated schema always agrees with the mapping, so a mapping that drifted from the real tables would still pass.
- **E2E**: the deployed system through its public surface.
The shape follows **cost**: the higher you go, the slower the feedback and the less clear the diagnosis. So most checks live at the bottom, and only critical paths at the top. It's a **guide, not a mandate**: not every project needs all three levels, and the right proportion is each stack/project's call. What the pyramid really forbids is **inverting it**: thousands of e2e tests over a few unit tests. Then every change needs a full, slow run to validate.
## One test file per unit under test — [our convention: Meszaros' Testcase Class per Class]
Meszaros documents three ways to organize tests: a class per production class, a class per feature of it, a class per fixture. **We take the first**: each test file exercises exactly **one** unit. A reader sees at once what the tests cover, and the test tree mirrors production. Use one file per feature only when a class has grown several independent behaviors, and then ask whether the class should split. Never use one file per fixture: it groups by setup and hides which unit the file tests. Shared infrastructure is the job of the stack's fixture mechanism, not of the file. What's forbidden either way is the lumped file testing many units at once: the test-side mirror of the god-class. File *placement* (mirrored tree, colocated specs…) → the stack skill.
**Pin a convention that holds for every unit of a kind once**, with a test that enumerates the kind. For example: every value object declares its factories, every exception carries a distinct code, every handler has its registration. That test goes next to the per-unit files, not instead of them. It is not the lumped file: it tests one rule, named as such, and a failure names the unit that broke it. A unit gets its own file the day it has behavior of its own.
## Test names are specification — [canon]
A test name states the behavior it verifies: a reader learns the rule without opening the body, and the suite reads as the unit's specification. **One act per test**: a name whose "and" joins two acts is two tests. An "and" that joins facets of one act's outcome (returned nothing *and* changed nothing) is one test with several asserts.
The *format* is per school and per stack tooling — all equivalent realizations of the same canon: `MethodName_StateUnderTest_ExpectedBehavior` (Osherove), `Should_X_When_Y`, Given-When-Then (BDD), `describe`/`test` sentences (JS). Each stack skill fixes its idiomatic one.
## Structure and independence — [canon]
- **Arrange – Act – Assert**, visually separated. One behavior per test; several asserts are fine when they verify facets of the same outcome.
- Tests are **independent and order-agnostic**: no shared mutable state, no dependence on execution order; each test builds what it needs.
- Shared builders/fixtures stay close to the tests that use them — a test file reads top-to-bottom without hunting elsewhere.
- No timing-based tests: inject the clock or control the scheduler. Where the code reads the clock itself, assert the instant against a window that you capture just before and just after the call. A sleep in a test makes it fail at random, sooner or later.
## Test data: Object Mother + Builder — [canon: Schuh & Punke's Object Mother, Pryce's Test Data Builder; our default]
Two complementary patterns build the Arrange, each with its own job. Use them instead of building objects by hand in every test:
- **Object Mother** — named, canonical instances that model **domain-meaningful states**: `Invoices.Overdue()`, `Customers.Premium()`. The default entry point; most tests read exactly one. **[our convention]** reach for a mother first.
- **Test Data Builder** — fluent, field-level **variation**: `.WithNoEmail()`, `.WithTwoItems()`. For the tweak a mother doesn't name.
- **Compose them — the mother returns a builder**: `Customers.Premium()` yields a builder preloaded with a valid premium customer. Call `.Build()` for the canonical case, and `.WithNoEmail().Build()` for a one-off variation. You get the canonical default *and* variation on demand, without a method for each combination.
The guardrail: **a mother names a STATE, not a field tweak.** `Invoices.Overdue()` — yes; `Customers.PremiumButNoEmail()` — no, that's the builder's job. Once mother names start carrying `WithX`, the variation belongs in the builder. Well-segregated units (see `solid`) have few meaningful states, so the mother set stays small on its own.
The stack skill decides the concrete form and placement: static classes, naming, where the mothers and builders live. It also decides the builder's shape: a fluent type and optional parameters on the mother's method realize the same pattern.
## Behavior over implementation — [canon: classicist school]
- Verify **outputs and state changes**, not internal call sequences. Tests that check many interactions break on refactors that change nothing observable. A test that fails on an improvement does not do its job.
- If the type's equality is shallow (collections compared by reference), assert **field by field** in round-trip tests.
- A test that restates its own mock setup asserts nothing.
## Test doubles — [canon: classicist school, our pick]
Two canonical schools exist: **classicist** (real collaborators and fakes; verify outcomes) and **mockist** (mocks at every boundary; verify interactions). **We deliberately follow the classicist school** — consistent with behavior-over-implementation, and the position that most of the industry now takes.
The ladder — **use the most real rung you can afford**:
1. **Real collaborator** whenever it's cheap — the real validator, the real value object.
2. **Fake** (a working lightweight implementation — an in-memory provider/store) when the real thing needs infrastructure.
3. **Mock only what you really cannot control**: a third-party API, the clock. Mock also to verify an *outbound* contract at an architectural boundary ("the event was published"), where the interaction IS the observable behavior.
Never mock the thing under test — a partially-mocked SUT tests the mock, not the code.
## Coverage: rules, not lines — [our convention]
- Line coverage says a line *executed*; it doesn't say its behavior was *verified*. The question that matters: **"if someone deletes this rule, does a test go red?"** If not, no test covers the rule, whatever the percentage says. (Mutation testing automates exactly this question.)
- Every business rule and every validation gets its **failing case** — including the red side of each validation rule.
- Edge cases are first-class: null/empty, boundaries (0, 1, max), invalid input, duplicates, out-of-order and idempotency where the code consumes messages. **Every bound from both sides** — the last value accepted and the first rejected — so neither the operator nor the limit can drift.
- **Seed known data, assert real values**: exact counts, the returned ids, the stored fields. Never assert only "not null" or "not empty": an empty store or a wrong query also satisfies them.
- **A filter needs its negative case**: data that must be left out, asserted absent. A predicate that matches everything passes every positive test.
- Do not chase 100% line coverage with trivial tests or tests without asserts. A covered line with no meaningful assert is worse than an uncovered one, because it looks safe.