Skip to content
Back to skills

Write Tests

ASecurity

Use every time a test is proposed, or about to be written, edited, kept or deleted, in any language and any test framework: a unit, integration, scene, story, snapshot or end-to-end test, a fixture, a factory, a test double, a parametrized table, or a regression test for a bug fix. Also use before asking the user whether to write a test, and when auditing or pruning a suite. A test exists only when the user asked for it and stated the rule it checks; it checks that rule in every situation the...

  • 13 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 9, 2026
developmentjavascripttypescriptpythonrustgojavatestingapidatabase

Works with

  • api

Security analysis

A100/100

Scanned December 1, 2026

npx -y skills add CheckPickerUpper/skills --skill write-tests --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Write Tests?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Write Tests
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/checkpickerupper-write-tests/badge)](https://www.skillsdirectory.com/skills/checkpickerupper-write-tests)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: write-tests
description: "Use every time a test is proposed, or about to be written, edited, kept or deleted, in any language and any test framework: a unit, integration, scene, story, snapshot or end-to-end test, a fixture, a factory, a test double, a parametrized table, or a regression test for a bug fix. Also use before asking the user whether to write a test, and when auditing or pruning a suite. A test exists only when the user asked for it and stated the rule it checks; it checks that rule in every situation the rule covers, and its expected results come from the user's words, never from the code. Lists the tests agents write that check nothing, and how to audit a suite for them."
metadata:
  when: "proposing, writing, editing, keeping or deleting a test, or auditing a suite"
  situation: Writing code
---

# Write tests

A test exists only when the user asked for it and stated the rule it checks:
what must hold, and in which situations, in their own words. An agent never
decides on its own that a test is worth writing.

A test checks the rule in every situation the rule covers, never one example
of it. "Orders over $50 ship free" is a rule about every order total in every
region; a test of one $60 order passes while free shipping is broken at $50.01,
in every other region, and for every order with a coupon.

The reason is where the expected value comes from. When an agent writes both
the code and the test, both are the agent's reading of what the user wants.
The test agrees with the code because one author wrote both, and it checks
nothing about whether that reading was right. Breaking the code on purpose
does not expose this: a test that asserts the code's own constant fails
whenever the constant changes, so it passes every break proof and still only
repeats the implementation.

## Before writing a test

<what-to-do>
1. **Find the user's request for this test.** Quote it: the message, issue
   line or spec where the user asked for the test and stated its rule. With
   no request, write no test. Ask the user instead, and let them decide. The
   question says in plain words:
   - the rule the test checks, every member it applies to and the range of
     situations it checks it in, such as "every checkout, guest and
     registered, for every order total from $0 to $500, in every region, with
     and without a coupon";
   - what the user would see go wrong without it, naming only failures that
     some situation in that range shows;
   - how long it takes to run.
2. **Take the rule from the user's words.** An issue's acceptance line, its
   title, or an example the user gives, is one sighting of the rule. Name the
   rule it is a case of, then its range from the code:
   - **Every member it applies to:** each thing that runs the code the rule
     is about, read from where those things are declared, including ones
     added later. A noun in the issue title ("guest checkout", "the admin
     page") names where the rule was first seen, not its scope: when
     registered shoppers go through the same checkout code, the test covers
     them too.
   - **Every situation:** each variant of the input, each state the code can
     be in, each boundary.

   Both come from the code, so the user is never asked for them. The expected
   results come from the user's words. When those leave a result open, ask
   for it; never fill it in from the code.
3. **Check that no existing test already covers it.** Break the rule in the
   code in the smallest plausible wrong way (skip the request, return the
   default, swap the currency, drop the write), run the module's tests, and
   undo the break. If one fails, the rule is covered: write nothing. A test's
   name never shows what it covers; only the break shows it.
</what-to-do>

Without a test the user asked for, a change's proof is its compile, its type
and lint checks, and the tests that already exist.

## How to write it

- **Check the rule in every situation it covers.** When the situations can be
  listed, loop over all of them: every shipped variant, every state, every
  boundary and the values on each side of it. When they cannot be listed,
  generate them with the language's property-testing library (fast-check for
  JavaScript and TypeScript, Hypothesis for Python, proptest for Rust, rapid
  for Go, jqwik for Java) and assert the rule on each generated situation.
- **Expected results come from the user's statement of the rule.** Never copy
  one from the code, its constants, its tables or a run of it. A number that
  came from the code checks only that the code still says what it says.
- **Test at the public entry point against the real boundary.** Call the
  function, handler or command the caller calls. Use a real database (a
  scratch one), a real library, a real listener, a real temporary directory.
  Internal code always runs for real.
- **Replace only what the test cannot own,** such as a third-party remote API,
  the clock or randomness, and replace it at the edge where the process calls
  out, never an internal wrapper around it. Put the reason beside the
  replacement: what it stands for and why the real one cannot run here.
- **Assert the result the user named,** with the strongest assertion it
  allows: the exact value, the exact error variant, the whole payload when its
  shape matters. Each kind of failure has its own variant carrying its facts,
  and each test asserts its own variant.
- **An input expected to produce nothing is a near miss.** It shares words,
  tokens or structure with the inputs that do produce something, so it fails
  when the code matches too broadly.
- **Name the test for the user's rule, in their words.** "A shopper who asks
  to spend more points than they have gets what is left, with a notice"
  describes what happens. "decideAllocate rejects with NoPoints" repeats the
  code.
- **A case with the same entry point and setup as an existing test is a new
  row in that test.** Add it to the test's table of cases (a parametrized
  test, a table-driven loop) with its own input, expected result and name,
  instead of a new test that copies the setup. When the existing test has a
  single case, turn it into a table first. A case that needs different setup,
  such as another fixture state or another boundary, gets its own test.
- **Keep each situation small and the run short.** Each situation uses the
  smallest real input that shows the behaviour, in-process, so the whole
  range runs in seconds. Small never means fewer situations. Wait for the event
  the test needs (a process exit, a written row, a listener accepting), never
  for a fixed number of seconds. Build expensive setup once and share it.

For a bug fix the user asked to have tested, the test is written first and
must fail on the code before the fix.

## Tests agents write that check nothing

| Bad test | What to write instead |
| --- | --- |
| Checks one situation of a rule: one order total, one region, one state | Every situation the rule covers, listed or generated |
| Checks only the members the issue title names, when others run the same code | Every member that runs that code, read from where they are declared |
| A proposal that says it catches a failure the test would pass on | Name only the failures some situation in its range shows |
| A test the user never asked for | Nothing. Ask the user, with what it would catch and its run time |
| Asserts a value copied from the code: a constant, tuning number, seed, offset or size | Nothing. Changing that value is a decision, not a regression |
| Tests diagnostics, logging, developer-only commands, test tooling or the test runner | Nothing, unless the user asked for that test |
| Compares new code step by step against an older version or prototype | Nothing. Once the user accepted the new code, the old version is not a spec |
| Asserts appearance or feel: layout, animation, geometry, pacing | Nothing. The user judges those by eye |
| Asserts a mock or spy was called | Assert the result the call was for: the payload sent, the state it left |
| Snapshot of a constant or a config value | Nothing |
| Re-checks what the type checker already refuses | Nothing. The compiler is the test |
| Tests a library's own behaviour | Test your code that uses it |
| Restates the implementation: private state, internal call order, which helper ran | Assert what a caller observes: `cart.total()`, not `cart._items.length` |
| Asserts removed code stays gone, such as a file absent from its old path | Nothing. Bringing the code back is a decision, not a regression |
| Needs a production hook written only for it, such as `_resetCacheForTests()` | Test through the public entry point, and delete the hook with the test |
| Passes when the code returns its default or an empty result | Assert the value the user named, and give each failure its own variant |
| Replaces an internal module with a fake | Use the real module; replace only the outside effect |
| A second test for a break another test already fails on | Nothing. Keep the one at the public entry point |
| Repeats a shared mechanism's test inside every feature that uses it | Test the mechanism once in its own suite |
| A check that could not run, counted as a pass | Report "could not run" as its own result |
| Sleeps for a fixed time, then checks | Wait for the event |

## Prove the test

<what-to-do>
1. **Break the rule the user stated,** in the smallest plausible wrong way.
   Run the stack's mutation-testing tool when it has one (mutmut or
   cosmic-ray for Python, StrykerJS for JavaScript and TypeScript,
   cargo-mutants for Rust, PIT for Java, gremlins for Go), and break by hand
   what the test depends on: the request never arrives, the file never opens,
   the service is unreachable. Also break it in situations no example
   covered: the rule failing only at one boundary, for one variant or in one
   state.
2. **Run the module's tests and read which fail.**
   - Nothing fails: the test cannot fail. The usual cause is in the code:
     several failures collapse into one answer (`None`, an empty list, a
     generic error). Split the answer into variants and assert the right one.
   - Only this test fails: keep it.
   - This test and another fail: one is a duplicate. Keep the one at the
     public entry point against the real boundary, and delete the other.
3. **Undo the break** and run the module's tests again; all pass.
</what-to-do>

The break proof shows a test can fail. Only the user's stated rule shows it
checks the right thing, so the proof never stands in for step 1 of "Before
writing a test".

Done when the test's rule is quoted from the user, it checks every situation
the rule covers, it failed on each break, it passes on the fixed code, and it
is the only test that failed for its breaks.

## Audit a suite

<what-to-do>
An existing test usually has no record of whether the user asked for it, and
breaking the code once per test costs too much on a large suite. Judge
existing tests from one run and one read instead.

1. **Run the suite once with per-test coverage,** so each test's covered lines
   are recorded: pytest-cov's `--cov-context=test` for Python, or the
   runner's own per-test coverage. Where the runner records coverage only for
   a whole run, run each test file alone with coverage on.
2. **Read every test file,** split across cheap sub-agents when the suite is
   large. For each test the reader reports:
   - the request, when one can be found: the issue line, spec or message that
     asked for it, or a commit naming it;
   - each expected value, and whether that literal appears in the code under
     test or the test imports it from that code;
   - one line on what a user would see go wrong if the tested code broke, or
     "nothing a user sees".
3. **Flag each test without a request** by these three signals:
   - **Duplicate:** another test covers all of its lines and asserts the same
     result.
   - **Copied value:** its expected values come from the code under test.
   - **Nothing at stake:** a user sees nothing go wrong if its code breaks.
4. **Give the user two lists.** The deletion list holds every flagged test,
   grouped by signal, with each group's file count, line count and recorded
   run time; the user approves it by group. The decision list holds each
   unflagged test without a request, with its stakes line and run time; the
   user keeps or deletes each. Break the code for a test on the decision list
   only when the user wants proof before deciding.
5. **Delete what the user approves,** along with the fixtures, helpers, build
   setup and production code only those tests used. Record the user's words
   beside each test they keep, as its request.
6. **Run "Prove the test" and the table above** over every test that has a
   request.
</what-to-do>

Done when every test left has a recorded request, the deletion and decision
lists are settled by the user, and no file exists only for the deleted tests.

## When a test fails for the wrong reason

A test that fails under load, on a slow machine or in another order is a bug
in the test. Fix it at once: wait for the event instead of a time, give it its
own scratch resources, remove the shared state. Never retry it, skip it, mark
it slow or raise its time limit.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.