agentleFS
swadhinbiswasswadhinbiswas/SKILLS/skills/testing/test-design/SKILL.md

test-design

Skill1 starsChanged 2 months agoMITView on GitHub

Design tests that are worth keeping - behaviour over implementation, where unit vs integration vs contract vs E2E tests genuinely belong, what not to test, AAA and given-when-then structure, test names that document themselves, mocking limits, and error-path coverage. Use when adding tests, when a suite is slow or flaky, when coverage is high but bugs still ship, or when someone asks "how many tests do we need". Triggers on "flaky test", "too many mocks", "test coverage", "E2E test", "refactor breaks tests", "test is brittle".

26 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use add-a-use-case, branches-and-pull-requests and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • LeanMeanCleanAppsSkill

    add-a-use-case

    How to add a command or query to the Application layer as a vertical slice (request, handler, validator, tests) using source-generated Mediator. Use when adding a new feature, a new query, or a new write operation.

    Also in 5 projects that use this

  • LeanMeanCleanAppsSkill

    branches-and-pull-requests

    Branch naming (feature/, fix/, chore/), how labels and release notes follow from the branch, the pull request template, and how changes get merged in the template repository. Use when creating a branch, writing a commit or opening a pull request.

    Also in 5 projects that use this

  • +10 more from LeanMeanCleanApps

The file

We split this skill into the rules it gives an agent

11 rules. 0 are common, 5 are distinctive, and 6 are very specific to this project.

  • 0

    Common

    Most of the 20 edited versions we read keep these.

  • 5

    Distinctive

    This author's own choices. Worth a look.

  • 6

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Test the contract of a unit — the promises it makes to callers — and nothing else.

    line 17
  2. Distinctive:

    Delete or rewrite it, do not just fix it, when:

    line 43
  3. Very specific:

    Use AAA when the code is procedural.

    line 59
  4. Very specific:

    No logic in the test: No loops building inputs, no if, no arithmetic you would have to debug.

    line 93
  5. Very specific:

    No sleep: Use fake clocks, event waiters, or poll with a deadline.

    line 95
  6. Very specific:

    Never assert on mock.called: when you can assert on the outcome.

    line 98
  7. Distinctive:

    Contract tests are what lets you delete the E2E matrix: If each consumer records what it needs, you can check compatibility per-commit …

    line 138
  8. Very specific:

    A mock that returns another mock is unreadable: If your test needs mock.return_value.mock.return_value.side_effect, the real code is hard …

    line 158
  9. Distinctive:

    Prefer asserting on a stable error code over an error message: codes are part of the contract.

    line 190
  10. Very specific:

    assert is stripped in optimised Python (python -O): Never use bare assert for control flow or side effects in library code.

    line 240
  11. Distinctive:

    When in doubt, delete the mock and see if the test still passes.

    line 243

“Common” is measured: kept by at least 6 in 10 of the 20 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Test Design

A test suite is an asset only if changing behaviour correctly breaks it and changing implementation correctly does not. Every rule below is that idea applied.

The decision that matters: what to test

Test the contract of a unit — the promises it makes to callers — and nothing else.

LayerTestsDon't test
Pure logic, domain rules, parsing, formattingEvery branch including boundaries and error pathsFramework plumbing
Service with real collaborators behind an interfaceRequest → observable state change; authorisation and validation failuresThat the DI container resolved the class
HTTP/GraphQL handlerStatus codes, response shape, error body, side effectsWhich helper called which helper
Database accessMigrations apply; constraints actually reject bad rowsSQL string text
UI componentWhat the user sees and can do, at the DOM/a11y levelComponent instance state, hook call counts, snapshots of markup internals

What not to test, ever

  • Getters, setters, __init__ field assignment. Zero value, real maintenance cost.
  • Framework code. Your test is not asserting that Django resolves a URL.
  • Constant returns and pass-through delegation. return self.x.x() has no branch worth covering.
  • Private methods directly. A private method is an implementation detail; reach it through the public method. It is fine to be covered incidentally.
  • Exact log text, internal state, call ordering of collaborators. These change without behaviour changing, so they cause failures that mean nothing.
  • "Coverage" for its own sake. Executed line ≠ tested.

When a test is a liability

Delete or rewrite it, do not just fix it, when:

  • It fails intermittently and nobody can name the cause after 20 minutes.
  • It asserts on a private field or exact internal call sequence.
  • It duplicates another test; the first failure found is the useful signal and the rest are noise.
  • It needs a hard-coded sleep, a fixed date, or a specific execution order.
  • It breaks on every refactor but has never caught a real bug.
  • It is slow (>1s) and covers logic that a fast unit test would cover better.
  • Its name no longer describes what it asserts.

Suite debt compounds: every 10% of tests that are noise is permission to ignore the suite, and ignored suites have a 100% pass rate with zero bugs found.

Structure: AAA and given-when-then

Use AAA when the code is procedural. Use given/when/then when the scenario is the interesting part — a table cell that reads as a spec, a failure that names its own precondition.

def test_expired_token_is_rejected(self):
    # given
    token = issue_token(user, ttl=timedelta(seconds=-1))

    # when
    result = authenticate(token)

    # then
    assert result == Err(AuthError("token expired"))
    assert result.error.code == "token_expired"   # stable code, not the message
it('returns 409 when the seat is already taken', async () => {
  // given
  await bookSeat(seatId, userA.id);

  // when
  const res = await request(app).post('/bookings').send({ seatId, userId: userB.id });

  // then
  expect(res.status).toBe(409);
  expect(res.body.code).toBe('SEAT_TAKEN');
});

Rules that follow from the structure:

  • One behaviour per test. One failing assertion cluster, one fix.
  • No logic in the test. No loops building inputs, no if, no arithmetic you would have to debug. Move that into a builder or parametrise it.
  • No sleep. Use fake clocks, event waiters, or poll with a deadline.
  • Assert on values, not on the absence of exceptions where a value is available. assert result.status == "deactivated" beats "did not raise".
  • Never assert on mock.called when you can assert on the outcome. The outcome test survives the refactor that replaces the call with a direct call.
  • A test with no assertions is either dead code or a missing assertion.

Test names are the documentation

A name should let someone delete the body and still know what is guaranteed. Name by behaviour and condition, not by method under test.

WeakStrong
test_pricetest_applies_member_discount_before_tax
testUsertest_renders_empty_state_when_cart_has_no_items
test_errortest_returns_422_when_email_is_not_a_valid_address
test_2test_retries_3_times_then_propagates_the_last_error

For BDD frameworks use full sentences; for everything else use test_<condition>_<expected> with underscores (languages without spaces).

One behaviour per test also means the test name is the assertion: if you cannot express the expectation in the name, the test is doing two things.

The pyramid, honestly

The classic triangle is roughly right about shape and wrong about why.

LayerShare of the suiteRuntimeWhat breaks it
Unit~70%msrefactors, dependency changes
Integration (real DB/HTTP)~20%100ms–10sdependency version bumps, CI parallelism
Contract (API/schema compatibility)~5%ms–sbreaking API changes by teammates
E2E (browser/full system)~5%minuteseverything, and it is the least informative failure
  • The shape comes from cost, not virtue. A unit test is fast and local, so there can be thousands. An E2E test is slow and local-only in its failure, so there can be ten.
  • E2E tests diagnose worse than they catch. A failure says "checkout is broken", not which field or which function. Keep them for the handful of journeys whose failure means an outage, and push the detail down into integration and unit layers.
  • Contract tests are what lets you delete the E2E matrix. If each consumer records what it needs, you can check compatibility per-commit instead of waiting for a full E2E run to go red.
  • Injected bugs live in the layers you skipped. The most productive thing you can do when choosing a layer is to inject a bug into the code and check that some test fails. A suite that survives mutation testing has a layer distribution problem.

Over-mocking

Mocks are a tax. Every mock is a lie about the world that must be maintained by hand, and it dies the moment the code is refactored.

  • Mock only what crosses a process or is nondeterministic: network, clock, randomness, payment provider, mailer, queue. Prefer a real in-memory or containerised instance of the thing (SQLite, fakeredis, a real Postgres container) over a mock — it is faster to set up than you think and it cannot drift.
  • Do not mock the class under test's own collaborators' internals. Assert on the returned value or the persisted state.
  • A mock that returns another mock is unreadable. If your test needs mock.return_value.mock.return_value.side_effect, the real code is hard to use; that is a design finding, not a test problem.
  • Do not copy the implementation into the expectation. assert result.total == sum(item.price for item in items) reimplements the logic and breaks whenever it changes.
  • Patch where it is looked up, not where it is defined. In Python, patch("mypkg.service.Client") (where used) is correct; patch("mypkg.client.Client") is not, and fails with AttributeError the moment someone refactors the import.
  • Over-specified mocks are the main cause of "tests break on refactor". If a test asserts assert mock_db.execute.call_args_list == [call(...), call(...)], the test is a second copy of the code.

Error paths

Untested error paths are the most common source of production incidents that were "obvious in hindsight", because the code is written once and only the happy path is ever exercised by hand.

Cover, for every unit that can fail:

  1. Each distinct failure the code can produce — an explicit error return, a rejected promise, a raised exception. Not "errors" in general; each one.
  2. The fallback path — defaults, partial results, degraded mode.
  3. Boundaries — 0, 1, max, max+1, negative, empty string/collection, the largest value the type allows, and one past it. Parametrise these.
  4. Invalid input at the boundary that owns it — validation belongs in one place; test it there and do not re-test it in every caller.
  5. Partial failure — the third item in a batch failing must not corrupt the first two. Test atomicity, not just success.

Prefer asserting on a stable error code over an error message: codes are part of the contract; messages are for humans and get reworded.

# One test per failure mode. The name is the spec.
@pytest.mark.parametrize("error", [AuthError, RateLimitError, UpstreamTimeout])
def test_verify_rejects_on_transient_failures(error):
    # given
    verifier = Verifier(stub=StubVerifier(raises=error))

    # when
    result = verifier.verify(Credentials(token="t"))

    # then
    assert result.is_error
    assert result.error.code in TRANSIENT_CODES

Safety

  • Never make the suite hit production or any shared real environment. Test credentials and endpoints must be guarded; assert in the harness that the target host is not a production hostname before running.
  • Never let a test send real email, real SMS, or real payments. Use provider sandbox modes or a local fake; assert the send was routed to the fake, and fail the test if it was not.
  • Never weaken or delete a failing test to make CI green without a written issue and an owner. xfail and skip must carry a reason and a tracking link, and xfail(strict=True) so a fixed bug fails the build until someone removes the marker.

Gotchas

  • "No tests on main" is the most common cause of untested critical paths. Coverage of main measures the last deploy, not the PR.
  • A test suite that is 5 minutes long gets run less, and run partially. Split the slow tier into a separate job so the fast gate stays fast.
  • Asserting on a rendered HTML snapshot fails on every whitespace change and teaches the team to regenerate snapshots without reading them. Assert on roles and text instead, or keep snapshots at a single coarse level.
  • Testing the happy path only is the shape of a green suite that misses every incident. If you have one happy-path test and no error-path tests, the suite is decoration.
  • Tests that depend on execution order pass in one order and fail in CI, where the order differs. Randomise the order (pytest-randomly, or pytest -p no:randomly to turn it off) to find them, and compare two fixed seeds.
  • A test that passes when run alone and fails in the suite is shared state: a module-level mutable, a singleton, a cache, a leaked thread, or a temp file. Fix the state, not the order.
  • assert is stripped in optimised Python (python -O). Never use bare assert for control flow or side effects in library code.
  • Testing what a mock returns proves the mock returns what you told it to. When in doubt, delete the mock and see if the test still passes.

Files

  • references/test-smells.md — catalogue of specific smells with the refactor for each, plus the review checklist for a PR that adds tests. Read it when reviewing a test PR or when a suite has become slow and flaky.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.