/code:writing-testsfrom code
Guides writing and reviewing tests across any language and framework. Covers test structure (AAA), mocking, flakiness, and coverage decisions. Includes Python-specific practices in a reference file.
Ships in code. 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.
Read when SKILL.md points to them; scripts run unread.
python skill.writing-tests.
Skip the notice when the user asked for the skill by name or slash command; spelling and spacing need not match.
A phrase from this skill's own trigger list is not a name — naming the work is not naming the skill.Every test follows the same three-phase structure — called Arrange/Act/Assert, Given/When/Then, or Setup/Exercise/Verify depending on tradition. All three describe identical phases:
| Phase | AAA | Given/When/Then | Meszaros |
|---|---|---|---|
| 1 | Arrange | Given | Setup |
| 2 | Act | When | Exercise |
| 3 | Assert | Then | Verify |
| 4 | — | — | Teardown |
Separate phases with a blank line — that is the minimum required structure.
Section-label comments (# Arrange / # Act / # Assert, or # Given / # When / # Then) are acceptable when the team finds them helpful; they are not required.
The examples in this document include labels as a teaching aid.
// Arrange
cart = new_cart()
cart.add(item("widget", price=9.99), quantity=3)
// Act
total = cart.total()
// Assert
assert total == 29.97
Teardown is required when a test acquires real resources (files, connections). Prefer automatic cleanup via framework hooks over manual teardown in the test body.
Test organization (file layout, grouping by class vs. flat functions, describe blocks) follows your testing framework's conventions.
The AAA structure applies at the individual test function level, regardless of how tests are grouped.
Violations that signal a structural problem:
See references/test-structure.md for AAA in depth, DAMP vs. DRY, and GWT vs. AAA.
Test names are the first thing a reader sees on a failure. They must communicate the scenario and the expected outcome without entering the test body. The name is the test's specification.
Three-part form: <Behavior>_<Condition>_<ExpectedOutcome>
Casing and separators follow your language convention — the goal (communicating scenario and expected outcome) is universal; the style is not.
// snake_case (Python, Ruby)
withdraw_money__insufficient_funds__raises_error
cart_with_no_items__total_is_zero
// camelCase segments (Java, C#, JS)
withdrawMoney_insufficientFunds_raisesError
cartWithNoItems_totalIsZero
Alternative — full behavioral statement (readable as documentation):
should_reject_transfer_when_balance_is_negative
returns_empty_list_for_unknown_category
Note: some frameworks require a specific prefix for test discovery (e.g., test_ in pytest, Test in Go).
That prefix is a framework requirement, not a naming-quality choice — it does not substitute for a descriptive name.
A function named test_withdraw is as opaque as testWithdraw; test_withdraw__insufficient_funds__raises_error communicates what the test verifies.
Avoid: numbered suffixes (test_2, test_order_2), names that state only the entry point without a condition or expected outcome, names that describe input type rather than scenario.
See references/test-structure.md for naming conventions in depth.
Don't write comments that explain what the test does — the name and structure should. Write a comment only when:
A test that requires comments to be understandable has a naming or structure problem. Fix the test.
Shape the suite toward many fast unit tests, fewer integration tests, and a minimal set of end-to-end tests — the testing pyramid. Treat the ratios as a sanity check against a slow, expensive, inverted pyramid, not a quota. Let contract risk and the natural behavioral unit drive the actual mix.
Match test scope to the natural behavioral unit of the code:
Coverage expectations for each change:
Multiple write-sites: when the same contract outcome can be produced by more than one code path (canonical path, dedup shortcut, cache hit, idempotency early-return), each path needs its own test. A test of the canonical path does not prove the shortcut is correct.
// Test the canonical path
result = repo.save(record, mode=CANONICAL)
assert result.id != null
// Also test the shortcut — it may skip validation or use a different write order
result = repo.save(record, mode=DEDUP)
assert result.id != null
assert result.normalized_url == normalize(record.url)
Derived-pair invariants: when a spec asserts field_a == f(field_b) and the two fields are written by different producers, neither field-local tests nor per-producer tests prove the invariant holds after composition.
Write a post-composition test.
// Each producer may be correct in isolation but the pair must be verified together
manifest = merge(resolver_output, fetcher_output)
assert manifest.normalized_url == normalize(manifest.source_url)
See references/test-portfolio.md for the pyramid vs. trophy vs. honeycomb debate, scope decision questions, property-based testing, and mutation testing.
"Mock" is not a synonym for "test double." Use Meszaros's taxonomy:
| Type | Definition | Use when |
|---|---|---|
| Dummy | Passed but never used | A parameter is required but irrelevant to this test |
| Fake | Simplified real implementation | In-memory database, fake queue, in-process event bus |
| Stub | Returns canned values | Providing indirect inputs: HTTP responses, clocks, config |
| Spy | Stub that records calls | Verifying a side-effect occurred; assert post-hoc on the recording |
| Mock | Pre-programmed expectations | The interaction itself is the behavior under test |
Prefer state verification (assert on observable outcomes) over behavior verification (assert on call sequences). Tests that assert call sequences are structure-sensitive: they break when implementation is refactored without changing behavior.
When to use doubles: external I/O (network, filesystem, email), non-determinism (clocks, randomness), operations that must not execute in tests (charge a card, send a real notification).
Prefer real collaborators for domain logic — test domain objects with their actual peers rather than stubbing business logic.
A Fake (in-memory implementation) is a test double and is appropriate for external infrastructure; using a FakeRepo does not violate this principle.
Stub at the boundary, not deep inside. Replace the dependency at the seam where your module uses it. Deep mock chains that mirror the internal call graph are a design smell — they indicate I/O has not been properly decoupled from logic.
Common mistake: asserting that a mock was called without asserting on observable output. Call-count assertions prove the mock was invoked, not that the behavior was correct.
See references/test-doubles.md for the full taxonomy with examples, the classical vs. mockist distinction, and common misuse patterns.
Red-green-refactor is an ordering discipline: write a failing test, write minimum code to pass, then refactor. The principles in this skill apply regardless of whether tests are written before or after code.
One rule from TDD that applies universally: when fixing a bug, write the failing test that reproduces it before touching production code. A test you never saw fail doesn't prove the bug is gone. For failures that can't be reproduced locally (race conditions, load-dependent), write a characterization test capturing the known behavior and confirm the failure reproduces in CI before patching.
A test protects behavior only if some wrong implementation makes it fail. A test that stays green whatever the code does is a false green: it adds coverage and confidence but no protection. Generated tests often have this defect, because they run the code and look thorough.
Review each test with these questions, in order. The first question that gets "no" names the defect.
return, or inside a try whose handler discards the failure checks nothing.
A test the runner does not collect checks nothing either.The expected value must come from a source that does not depend on the code under test. Use the first source available:
The current output of the code is not an oracle. An expected value copied from a run of the code, or computed by the same formula the production code uses, agrees with whatever the code does, bugs included. When the current output is the only source, ask before encoding it (see Intent and Scope). If the user confirms that the current behavior is the behavior to keep, write a characterization test, and name and document it as one so a reader does not take it as a statement of correct behavior.
For a test written before the code, red-green-refactor shows that it fails (see TDD).
For any other test, including a generated test, a test written after the code, and a repair of a false green, break the line under test on purpose: flip an operator, return a constant, or skip a call.
Confirm that the test fails, then revert the change.
This temporary edit is allowed on a test-only task; after the revert, confirm that the production file matches its original state.
A repair that stays green against the broken code is a new false green.
Mutation testing automates this check across a suite (see references/test-portfolio.md).
For Python, scripts/run_falsegreen.py runs a static scanner that finds the structural forms of questions 1 and 4.
It does not judge whether an oracle is independent or whether the test exercises the real unit, so apply the questions as well.
Its output refers to a /falsegreen semantic pass; this skill does not ship that command, and the questions above take its place.
See references/python-testing.md for usage.
sleep instead of condition-based waits, unordered collections in equality checks, unseeded randomness, and resource leaks from missing teardown.
Diagnose ordering issues by running with a randomized seed.
Fix with per-test setup/teardown hooks, condition-based waits, sorted comparisons, and seeded randomness.references/test-structure.md — AAA in depth, naming conventions, DAMP vs. DRY, GWT vs. AAAreferences/test-portfolio.md — pyramid, trophy, honeycomb, scope decision guide, property-based testing, mutation testingreferences/test-doubles.md — full taxonomy with pseudocode, classical vs. mockist school, when to use each typereferences/python-testing.md — Python-specific practices: unittest.mock discipline, pytest structure, false-green patterns, async testing, dependency auditsscripts/run_falsegreen.py — runs the falsegreen scanner for Python tests through uvxWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
test-writing, test-review, test-structure, flaky-tests, mocking
npx claudepluginhub ahgraber/skills --plugin codeOther plugins' skills for the same job
10 skills in this plugin
/api-designUse when designing, reviewing, or planning REST or GraphQL APIs — endpoint structure, schema design, versioning, error handling, pagination, URI naming, or choosing between REST and GraphQL. Also triggers for OpenAPI spec creation and API contract review./changelogUse when writing, updating, or generating a CHANGELOG.md — for a new release, an unreleased section update, or drafting from git history or diff. Triggers: "update the changelog", "write release notes", "what changed since X", "generate changelog for v1.2.0", "add this to the changelog", "draft unreleased section". Not for commit messages (use commit-message skill) or internal deployment runbooks./code-reviewUse when reviewing code changes before committing or merging — staged diffs, branch diffs, or PR readiness checks. Triggers: 'review my code', 'check these changes', 'review this branch', 'give feedback on my diff', 'is this PR ready?'. Not for implementing features, running test suites, formatting-only passes, or building understanding of a change (use teach-me).