writing-tests
Writes behavior-focused tests using the Testing Trophy model with real dependencies and minimal mocking. Use when writing or reviewing tests, choosing between…
от rileyhilliard★ 133 звезды на GitHub
Установить writing-tests в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Об этом скилле
If the current repo has its own testing rules or skills (check .claude/rules/ and the repo CLAUDE.md), those take precedence. Apply this skill where they're silent.
Writing Tests
Test what a user or caller can observe, using real dependencies, so tests survive refactoring and fail only when behavior breaks.
The check for any test: a refactor that improves the code without changing what the app does must leave it passing, so the same test proves the behavior is unchanged before and after. A test that breaks on such a refactor hard-codes or stubs an assumption about the internals, which is the usual failure of unit tests.
Which kind of test
Most tests should be integration tests written as behavior tests (the Testing Trophy: integration is the widest layer, with fewer unit and E2E tests around it). Enter through an interface a user or caller really uses, such as an HTTP route, a CLI command, a rendered component or a service's public function. Run it against the real database and real internal modules, then assert on the observable result. Name each test for the behavior ("rejects a duplicate email"), not the function it calls.
Integration is the default because it covers the wiring between units, which is where most bugs live, and it survives refactoring because it knows nothing about internals. Isolated unit tests of code with dependencies mostly end up testing the mocks.
Reach for another type only when it fits:
| Type | Use it for | Don't use it for |
|---|---|---|
| Unit | Edge cases a behavior test can't reach, and pure logic (parsers, calculations, validators, state machines) whose input combinations are too many to run through the interface | Cases a behavior test already covers, and anything that needs a mock of your own module to run. Write an integration test instead |
| E2E / acceptance | A handful of critical journeys (sign-up, checkout, login) through the deployed stack in a real browser, and behavior only the full stack shows: browser quirks, deployment wiring, third-party redirects | Edge cases and error branches. Cover those at the integration level, where tests are faster and less flaky |
When unsure, write the integration test.
Mocking
Mock only what you don't own or can't control: external HTTP APIs, third-party services (payments, email), time and randomness. Use the real database, real internal modules and real business logic. When a mock is needed, make it match the real API's shape, including error responses.
Assertions
| Context | Assert on | Not on |
|---|---|---|
| UI | Visible text, roles, labels | CSS classes, component state |
| API | Status and response body | Rows in the database behind it |
| Library | Return values and raised errors | Private methods |
Anti-patterns
| Pattern | Instead |
|---|---|
| Asserting a mock was called | Assert the outcome the call produced |
| Test-only methods in production code | Put helpers in test utilities |
sleep(500) before an assertion |
Wait for the condition (findBy, waitFor, Playwright auto-wait, polling helpers) |
| Asserting internal state | Assert observable output |
Cover behavior realistically
A behavior test is not only the happy path. Drive the real entry point through the combinations and edge cases real use produces: inputs in the order and shapes callers send them, chained and mixed operations, state left over from an earlier step, failures of the things it depends on, and the near misses it must not get wrong. One realistic flow often exercises what several contrived single-function tests would, and it keeps doing so through a refactor. Give each realistic case its own row so a failure names it.
Find the cases by asking what real users, machines and data will throw at the code, not only the cases you had in mind while writing it. Bugs cluster in a few places:
- Boundaries: empty, one, many, the limit and one past it, very large input.
- State already there: existing records, a duplicate, a half-finished earlier run, data from an older version.
- Dependencies failing: errors, timeouts, retries, a slow or partial response.
- Order and timing: concurrent requests, repeated or out-of-order actions, an interruption partway through a write.
- Text and environment: unicode, quotes and newlines in input, paths with spaces or symlinks, time zones and locales, missing permissions or tools.
There are two ways to get a test to the state an edge case starts from:
- Reproduce the steps a user takes to get there: click the button, fill the form, submit, have the API fail, then check what the UI shows.
- Set the state up directly: give the component or function the realistic data those steps would have produced (rows in the real database, files on disk, the props a parent would pass), have the API fail, then check what the UI shows.
Prefer the first. It survives a refactor of anything between the entry point and the state, because it knows only what the user does. Use the second w
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
- references/flaky-tests.md
- references/go.md
- references/python.md
- references/slow-tests.md
- references/test-value.md
- references/typescript-react.md
- references/waiting-python.md
- references/waiting-typescript.md
FAQ
Что делает скилл writing-tests?
Writes behavior-focused tests using the Testing Trophy model with real dependencies and minimal mocking. Use when writing or reviewing tests, choosing between unit, integration and E2E, deciding what to mock, replacing sleeps with proper waits, fixing flaky tests that fail only alongside other tests, speeding up slow tests that time out under load or on CI, or trimming a suite whose tests repeat each other.
Как установить скилл writing-tests?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл writing-tests запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
doc-coauthoring
AnthropicGuide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
academy-guide
AnthropicStop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
internal-comms
AnthropicA set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
skill-development
AnthropicThis skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…