/craft:testing-principlesfrom craft
Provides cross-language testing principles: DAMP over DRY, AAA/GWT, spy over mock, what NOT to test, factory functions, parameterized tests, and interface tests coupled to perceivable content.
Ships in craft. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
The rules below are language-agnostic. Tool-specific examples (Prophecy,
vi.fn(),vi.spyOn(), PHPUnit data providers,it.each(), NestJSTest.createTestingModule) live in:
phpunit:php-test-conventionssymfony:symfony-test-conventions(anything booting a Symfony kernel —WebTestCase, crawler, container doubles)vitest:vitest-test-conventionsnode-test:node-test-conventions(a file importingnode:test, or a package whose test script runsnode --test)shell-test:shell-test-conventions(a bash test suite, or a test of a Claude Code hook)A suite's economics and wiring (test sizes, what may be shared, where each check runs) live in
craft:test-suite-design.
Three layers are universal:
| Layer | What it covers |
|---|---|
| Unit | Pure logic, no framework/container/DB |
| Integration | Real dependencies (DI container, real serializer, real DB) |
| Functional / E2E | HTTP / UI / full flow |
Decision rule: does the test need the framework's container or external I/O? No → Unit. Yes → Integration. Full HTTP/UI flow? → Functional/E2E.
Never write tests coupled to implementation without behavior. Only test classes that have behavior worth verifying.
| Worth testing | NOT worth testing |
|---|---|
| Business logic / domain rules | Simple DTOs / data containers |
| Validation logic | Events with only properties |
| Calculations / transformations | Value objects without logic |
| State machines / workflows | Entities with only getters/setters |
| Serialization / deserialization | Tests re-verifying what a higher-level test already covers |
| Logs required by the business, audit traces, monitored metrics | Other log / monitoring calls |
Rule: if the test only verifies that a getter returns what was passed to the constructor, delete it.
Anti-pattern — tautological assertion: an assertion that recomputes the expected value the same way the code does (expect(add(a, b)).toBe(a + b)) passes by construction and proves nothing. Expected values must come from an independent source of truth — a hardcoded literal, a hand-worked example, or a known-good fixture.
Anti-pattern — the name promises a rule the assertion doesn't show: a test named for a business rule (submittingTheCouponExposesTheCoupon) that only echoes back the value it submitted proves nothing — binding input to output is a framework given. Assert what the code exposes or withholds as a function of inputs/context (which fields exist per case, what is dropped when a condition flips). No such rule at this layer → the test belongs elsewhere or nowhere.
Anti-pattern — testing the absence of a rule: a test proving that unwired code does not run ("the other provider is not filtered" when no production path wires the filter) protects nothing — there is no rule, so there is nothing to break. Absence of behavior is recorded in the plan's scope, never in a test.
Anti-pattern — testing a capability where production doesn't wire it: a generic mechanism (an option, an extension point) is tested only through the concrete class production actually wires to it. Exercising it elsewhere proves a possibility, not a rule.
Anti-pattern — change detector: a test that re-states what the code or the repository
already holds (an exact count of tests, lines or files, a line number, a list copied from the
code, a checksum of a tree) breaks on every edit and detects no defect. Rewrite it against an
observable behaviour, or delete it. A count of tests, lines or files that a behaviour-preserving
edit moves is printed by a command, or dated where it is written; it is never pinned in prose and
re-checked by a test. During TDD the same defect shows up as the "Tests That Don't Survive
Refactor" warning sign of craft:tdd-workflow-principles.
Don't verify log or monitoring calls. A log is a side channel: rewording a message or changing its level breaks the test while no user sees a difference, and the log check sits next to the real outcome as a second intention.
Exception: the log is a business requirement, an audit trace or a monitored metric (a compliance record, an alert someone pages on). Then verifying it is the point of the test.
// ❌ The log line rides along with the real outcome
processOrder(order)
assertEquals('CONFIRMED', order.status)
expect(logger.info).toHaveBeenCalledWith('Order processed')
// ✅ Assert the behaviour; the logger is a null object
processOrder(order, nullLogger)
assertEquals('CONFIRMED', order.status)
Before writing a new test, confront it to these 3 questions. If you can't answer "yes" to all three, do not write the test.
Prefer Descriptive And Meaningful Phrases over Don't Repeat Yourself in tests.
setUp() / beforeEach() prepares the environment (kernel, container, clock). Avoid it for fixtures and test doubles: they belong in each test's Arrange. Keep the full Arrange-Act-Assert in each test.Each test builds its own world, acts and asserts, and runs alone, in any order. Never make a test consume what another test produced (PHPUnit @depends / #[Depends], a shared variable filled by a previous test). A dependent test cannot run on its own or in parallel, and when the first one fails the others are skipped, so their behaviours go unchecked.
// ❌ The second test asserts on what the first one did
test pushesAnItem(): stack.push(1); assertEquals(1, stack.size()); return stack
test popsAnItem(stack) depends on pushesAnItem: assertEquals(1, stack.pop())
// ✅ Each test arranges its own stack, through a shared factory if needed
test popsAnItem(): stack = stackWith(1); assertEquals(1, stack.pop())
Arrange — Act — Assert (or Given — When — Then), separated by blank lines, no comments.
A test reads top to bottom and runs the same path every time: no if, for, foreach, while or switch in its body. When the condition is false, the assertions under it silently do not run; a loop over an empty list asserts nothing, and a failure says "at iteration 3" instead of naming the case. One case per test, or one data-provider row per case (§7). A loop that only walks a structure belongs in a helper outside the test body.
// ❌ Assertions hidden behind a loop and a branch
for (case in cases):
if (case.valid): assertTrue(validate(case.input))
// ✅ One row per case, the body is a straight line
@dataProvider validInputs
test acceptsAValidInput(input): assertTrue(validate(input))
Mocks set expectations before the act (violates AAA). Spies verify after the act (respects AAA).
service.expect('call').withArgs(...) → call SUT → done.expect(service.call).toHaveBeenCalledWith(...).Criterion: if you write expectations before the act, you're using a mock; switch to a spy that asserts after.
Judge by the criterion, not by the tooling's vocabulary. vi.fn() and jest.fn() are called
mock functions by their own documentation, and Prophecy hands back what it calls a mock — but a
bare double carries no expectation until you arm it with one. Created in Arrange and asserted after
the act, it is a spy, whatever the library named it.
Verify at most one double per test. Each verified double is an intention about an outgoing interaction; two of them make a test whose name cannot say which one broke, and every extra interaction check is another way to break when the implementation changes without the behaviour changing. The other collaborators are stubs or null objects. Stubs do not count.
// ❌ One test, two intentions
confirmOrder(order)
expect(mailer.send).toHaveBeenCalled()
expect(audit.record).toHaveBeenCalled()
// ✅ One verified double each
test emailsTheCustomer(): confirmOrder(order); expect(mailer.send).toHaveBeenCalled()
test recordsTheConfirmation(): confirmOrder(order); expect(audit.record).toHaveBeenCalled()
When multiple tests have the same Arrange-Act-Assert structure and only differ in input data, consolidate via the language's parameterization (PHPUnit @dataProvider, Vitest it.each, etc.).
Criterion: no if should be needed in the test body — same structure, different data.
Do NOT parameterize when assertions differ or setup is specific to each case.
Split first, consolidate after. One test per case is fine while developing, but tests sharing the same Act+Assert and differing only in data are a smell — do a consolidation pass into one parameterized test. Split leftovers read as many features when it is one.
Name = mechanism; keys = the rule, in domain terms. Keep the method name plain about what it exercises (itRetainsTheSubmittedData), not the rule crammed in (itRetainsTheFieldsAvailableToThePremiumCustomerContext). Each case key carries the rule using the domain's existing words (a premium customer gets free shipping), never jargon coined for the test (eligible, the good case) — an unresolvable term just moves the unclear intent into the key. Criterion: name + keys make the rule legible with no prose comment.
Identifiers follow the same law as keys. Test method names, provider names, and variables
reuse the codebase's exact vocabulary: the class is Subscriber → never "client"; the
field is source → never "origin". And never borrow a term the domain reserves for something
else — "offered/offering" reads as the Offer product, not as select choices. A synonym in a
test name is a future misreading.
Create helper methods in the test class (or shared module) to build test fixtures. When duplicated across multiple test files, extract to a dedicated Factory class / file.
A factory takes a partial override of its defaults, so each test states only the field it varies.
An exception test names the error it expects (its class or code), never only that something threw.
Compare complete objects with the language's structured assertion rather than asserting field by field.
assertEquals with the expected object.toEqual with the expected object, or expect.objectContaining.Why: a single assertion either passes or fails as a whole — easier to read failures.
When debugging a flaky/broken behavior, prefer real dependencies over mocks. Mocks return what you tell them — they can pass while production crashes.
Two hygiene rules when a double is unavoidable:
When a real collaborator trivially substitutes — a null object, an in-memory implementation, a fixed clock — and reads cleaner, use it instead of a double. Reach for a double only when the test needs it: to verify an interaction or to script a return / throw. Otherwise pick whatever is fastest and cleanest — a manual stub, or the mocking tool.
Criterion: don't mock a collaborator you neither assert on nor script when a real / null implementation is available.
Never reimplement the real logic inside the double. A stub/mock must not copy the collaborator's (or SUT's) production body into its factory. If the double re-encodes the behavior, your assertions verify the copy, not the real code: the test goes green by construction, proves nothing about production, and silently rots the day the real logic changes (the copy stays, the real one drifts). This is the tautological-assertion trap (§2) wearing a mock's clothes.
When you genuinely can't run the real module in this test — an untransformable file type, a side-effectful global, a heavy dependency — do one of:
jest.fn(), a no-op) and assert it was called with the right arguments for the
right target. The double stays behavior-free; you verify the contract you own (the call),
not the collaborator's internals.// ❌ The mock reimplements the real setTooltip — assertions test the copy
jest.mock('@theme/tooltip', () => ({
setTooltip: (el, text) => { el.setAttribute('data-bs-title', text); /* …the real body… */ },
}));
expect(el.getAttribute('data-bs-title')).toBe('…'); // verifies the mock, not production
// ✅ Default double; assert the interaction you own
jest.mock('@theme/tooltip', () => ({ setTooltip: jest.fn() }));
import { setTooltip } from '@theme/tooltip';
expect(setTooltip).toHaveBeenCalledWith(el, 'Le compte par défaut ne peut pas être supprimé');
// …and the attribute-writing behavior is tested in the tooltip component's own repo.
Criterion: if deleting the real implementation would leave your double still "passing" the assertion, the double has reimplemented the logic — strip it to a spy.
Production code must serve production. Never add anything to production code — a
CSS class, js-* hook, id, attribute, getter, or method — whose only purpose is to
give a test something to locate or assert. This kind of code should never exist.
Only exception: what is strictly required for the environment to run or the test to boot — e.g. a test-only DI alias exposing a private service so it can be substituted. A test locator is never covered by this exception.
Instead: locate via markup/API the production code already exposes for a production reason — the real field name, a class the framework/theme actually emits. If no clean locator exists without adding test-only production code, do not add it — find another assertion.
The mirror rule holds inside the test suite: never write a production-shaped class that exists only for tests — a concrete subclass of a production abstraction, a fake form type, a test-only implementation used as the SUT. The fixture for an abstract class's behavior is the real concrete class that carries the business rule. (Doubles for collaborators — §10/§11 — remain legitimate; this rule is about the thing under test.)
Drive the code through its public interface. Never reach private methods or properties through reflection or closure binding: the test then breaks when a private step is renamed, merged or inlined, although every caller sees the same behaviour, and it pins a choice the class is free to change. A private step that seems to need its own test wants to be a public method of a smaller class. Reading metadata (a class name, attributes) is fine.
// ❌ Invokes a private step by name
hashed = reflect(password).method('hashed').invoke('secret')
assertEquals(sha256('secret'), hashed)
// ✅ Asserts the public contract
assertTrue(Password.fromPlain('secret').matches('secret'))
An interface / UI test proves behavior: the text, values, labels, and state a user actually perceives render correctly. Couple maximally to elements that carry functional meaning and are visible; avoid coupling to purely technical elements.
Selectors only LOCATE — assertions express FUNCTION. A CSS class, js-* hook, test
id, or XPath is just an address to reach an element. Once located, assert what the user
reads or perceives there: the visible text, the field value, the selected / checked /
disabled state. Never let the technical address be the thing you assert.
.form-check-inline, .card, wrapper divs) that carry no functional meaning and
change with a restyle.assertCount(2, '.form-check-inline')
passes even when every option renders the wrong text. Presence/count-by-class is
acceptable only as a secondary invariant next to content assertions, never as the
whole test.// ❌ Technical — passes even if the labels render garbage
assertCount(2, page.filter('.form-check-inline'))
assertNull(radios[0].attr('checked')) // framework default: nothing set → nothing checked
// ✅ Functional — locate by the field the app owns, assert the visible labels
options = page.filter('input[name$="[shipping_method]"] + label')
assertCount(2, options)
assertSame("Standard delivery", options[0].text())
assertSame("Express delivery", options[1].text())
Rule: locate with any selector you like; assert only what a user could see or perceive. If your assertion would still pass with the wrong text/value/state on screen, it tests wiring — rewrite it against perceivable content.
A business rule is tested where its business intent is most visible — not mechanically at the class that implements it, and not mechanically end-to-end either.
<option> lists from the DOM, counting nodes), is at the wrong level — that fragility
is the signal to move down, not a cost to accept.Criterion: read the test's name and assertions alone — they must state the business rule in domain terms. If they describe scraping mechanics, descend one level and retry.
A module-level replacement exists only as a dependency injector, to redirect an infrastructure singleton that cannot be injected to a hand-written stub.
In a Jest + Cucumber codebase, jest.mock is the module-level dependency injector.
Never a mocking-tool double for a port: write an in-memory stub in tests/Helpers and apply it to the curried handler.
The stub is typed Port & { test accessors }: it is its own spy, and you assert on its state rather than a call registry.
The stub's state resets before each test, through the runner's own per-test hook.
In a Jest + Cucumber codebase, the stub resets in beforeEach on the Jest side, and through a tagged Before/After hook on the Cucumber side.
Determinism comes from stub generators injected over a fixed list, never from a fake clock.
One assertion vocabulary per runner, never two in one file: node:assert under node:test, expect under Jest or Vitest.
AAA stays separated by blank lines, a Result guard precedes the payload assertion, and several assertions per test are allowed.
Nothing asserts a payload field ahead of that guard: the guard runs first, or the payload assertion is unreachable when the Result failed.
A snapshot applies only to a whole value (an aggregate, an HTTP body, a rendered template) and is always paired with a discrete assertion in the same test.
In a Jest + Cucumber codebase, Gherkin owns the business acceptance criteria: Feature/Scenario/Given-When-Then in English with data tables, and steps that run the handlers against stubs, with no HTTP and no database. Zero Gherkin step definitions in this pack open an HTTP client or a database connection — every step reaches the handler through its stub.
In a Jest + Cucumber codebase, the Pact suite stays outside the normal run: a *.pact.spec.ts suffix, a dedicated script, a CI job in allow_failure, and a published, versioned pact.
Zero *.pact.spec.ts files run inside the default test script; the allow_failure job is the only runner that executes them.
A characterization net records what the code does today, bugs and quirks included, never what it should do. Its examples pin a current output, even a wrong one, not a corrected one.
When it is required: a change must preserve an observable behaviour that no existing test pins. It is skipped, with one stated reason, when existing tests already pin it, when the code is new, or when its behaviour is meant to change. It is never "characterize everything": the inventory covers only the surface the change touches.
The order, with its gate between each step:
If code was already modified before a net existed, the net is recorded against the unmodified revision.
Intended changes on a pinned surface. A change that deliberately alters pinned behaviour (a bug fix, an intended change on part of the surface) updates exactly the net cases it targets, in the same change, and names them. Every other case stays green untouched. A red case nobody named is a regression and is never re-recorded. A bug's correct behaviour also gets its own failing-first test: see the Bug-Fix Workflow in craft:tdd-workflow-principles.
Bounded exception to one owner per rule (§14 rule 5). A net may re-verify rules owned at lower levels while the change it guards is in flight.
A test gives the same result on every run. Inputs that move on their own (the clock, a random generator) make it flaky and force loose or computed assertions.
Never read the system clock in a test: no "now", no relative date ('tomorrow', '+29 days'), in the test, its helpers or its data providers. Inject a clock into the code and give it a fixed instant in the test; an absolute date literal is fine. A test built on "now" fails at midnight, on the 31st or on a daylight saving change, and the value it asserts has to be computed from "now" too.
// ❌ Depends on today
subscription = Subscription.start(now())
assertEquals(now() + 30 days, subscription.renewsAt())
// ✅ A fixed instant in, a literal out
subscription = Subscription.start(fixedClock('2026-11-01 09:00'))
assertEquals('2026-12-01 09:00', subscription.renewsAt())
No random values in a test: no random number, shuffled list, random UUID or unseeded fake-data generator. A random input explores a different case on each run, the rare failure cannot be reproduced, and the assertion becomes loose. Use literals. When generated data helps (many fields nobody cares about), seed the generator so the data is the same every time. Property-based testing is a different, deliberate technique with a tool that records and shrinks the failing case.
// ❌ A different order on every run
order = Order.create(id: uuid4(), amount: randomInt(1, 1000))
// ✅ Literal inputs, or a seeded generator
order = Order.create(id: 'b7f1c2e0-0000-4000-8000-000000000001', amount: 250)
faker.seed(1234)
| Rule | Principle |
|---|---|
| Test type | Unit (no container) / Integration (real deps) / Functional (full flow) |
| What not to test | No tests on data containers / pure getters |
| Tautological assertion | Expected value comes from an independent source, never recomputed like the code |
| Name over-promises | A test named for a rule but only echoing its input back proves nothing — assert what the code exposes/withholds as a function of inputs & context |
| Pre-test checklist | Valid state? Not covered already? Behavior, not impl? |
| DAMP > DRY | Full AAA in each test; setUp/beforeEach for the environment (kernel, container, clock), avoided for fixtures and doubles |
| AAA / GWT | Blank lines between Arrange/Act/Assert, no comments |
| Spy > Mock | Verify after act, not before |
| Parameterized tests | Same structure + different data → data provider / it.each |
| Consolidation pass | Split-per-case is fine in dev; same Act+Assert differing only in data must be merged into one parameterized test |
| Name vs keys | Method name = mechanism tested (itRetainsTheSubmittedData); provider keys = the business rule, in existing domain terms, no invented jargon |
| Factories | Helper methods, extract when duplicated |
| Structured assert | Compare complete object, not field by field |
| Real over mock | When debugging, use real dependencies — mocks hide bugs |
| Mock hygiene | Mirror the real data's full shape; never stub the behavior under test; double a boundary you own, not a third-party type |
| Real over double | Use a null-object / in-memory / fake when you don't verify or script the collaborator |
| No logic in the double | Never reimplement the real body in a mock — use a default stub + spy on the call, or assert the behavior in the collaborator's own test |
| No prod code for tests | Never add a class/js-*/id/attribute to production solely as a test locator — locate via real production markup |
| Interface tests | Selectors only locate; assert perceivable content (text/value/state), not technical classes or framework defaults |
| Level choice | The level where the intent reads best; the deciding class is the last resort; orchestration → assert what transits to a doubled collaborator |
| Absence of rule | Never prove that unwired code does not run — scope lives in the plan, not in a test |
| Unwired capability | A generic mechanism is tested only through the concrete class production wires to it |
| Change detector | A test re-stating counts, line numbers, copied lists or checksums breaks on every edit and catches nothing — rewrite it against behaviour, or delete it |
| Test-only classes | No production-shaped class living only for tests — the fixture is the real concrete class |
| Characterization net | Before changing unpinned behaviour: freeze inputs, pin today's output (bugs included), prove it can fail, land it first; a temporary, marked exception to one owner per rule (§16) |
| Logging | Don't verify logs or monitoring calls, unless the log is a business requirement, an audit trace or a monitored metric |
| Independent tests | No test consumes what another produced; each runs alone, in any order |
| No control flow | No if/loop/switch in a test body: one case per test or per data-provider row |
| One verified double | At most one verified double per test; the other collaborators are stubs or null objects |
| No reflection | Drive the code through its public interface, never reflection on private members |
| Repeatable inputs | Fixed clock, literal or seeded data: never "now", never unseeded randomness |
| Vocabulary | Identifiers reuse the codebase's exact terms — no synonyms ("client" for Subscriber), no reserved-term collisions ("offered" vs Offer) |
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub fabiensalles/claude-marketplace --plugin craft8 skills in this plugin
/code-style-principlesACTIVATE whenever writing or modifying production code in src/, in any language — this is the base layer these rules build on. ACTIVATE for code review, formatting questions, or 'coding standards'. Covers cross-language code style: control-structure spacing, early-return pattern, continue-vs-if/else in loops, explicit (non-truthy/falsy) checks, flattening nested null checks, parameter ordering. These rules go BEYOND standard linters. Language-specific syntax (no-empty in PHP, template literals in TS, nowdoc, optional chaining vs nullsafe) lives in php-code-conventions / ts-code-conventions, which apply on top of this skill rather than instead of it. DO NOT use for: language-specific syntax already covered by php-code-conventions / ts-code-conventions (see php:php-code-conventions + typescript:ts-code-conventions for PHP/TS specifics)./ddd-fp-principlesACTIVATE when modeling DDD aggregates, value objects and domain logic using functional patterns — immutable records, curried operations, Result types, validation/enrichment pipelines, handler orchestration. ACTIVATE for 'aggregate', 'smart constructor', 'make*', 'validation pipeline', 'enrichment', 'domain handler' in a functional context. Provides cross-language functional DDD principles. For OOP-style DDD (classes, decorators, module-bounded contexts) see craft:ddd-principles. Companion examples: ddd-ts-fp.Other plugins' skills for the same job