---
title: Writing Unit Tests Skill for Claude Code
canonical: https://skillselion.com/skills/riekelt/principal-engineer/writing-unit-tests
content_type: text/markdown
description: "writing-unit-tests is a skill that guides how to write or refactor unit tests: behavior-first claims, one behavior per test, deterministic setup and mocks."
---

# Writing Unit Tests Skill

Title: Writing Unit Tests Skill for Claude Code

> writing-unit-tests is a skill that guides how to write or refactor unit tests: behavior-first claims, one behavior per test, deterministic setup and mocks.

Canonical: https://skillselion.com/skills/riekelt/principal-engineer/writing-unit-tests

writing-unit-tests is a skill that guides how to write or refactor unit tests: behavior-first claims, one behavior per test, deterministic setup and mocks limited to boundaries you do not own.

Writing unit tests is a rulebook for the craft of a single unit test. It asks for tests that check the public contract, one behavior per test, and names that state the claim. It covers deterministic setup with injected clocks and seeded randomness, mocks only at boundaries you do not own, value-based assertions, and a list of common mistakes. It relies on the principal-engineering skill as background.

## Key facts

- Installs: 31,044
- GitHub stars: 8
- Author: riekelt
- Repository: riekelt/principal-engineer
- Install command: `npx skills add https://github.com/riekelt/principal-engineer --skill writing-unit-tests`
- Last updated: December 1, 2026

## What Writing Unit Tests does

Guides the craft of unit tests: one behavior per test, claim-style names, deterministic setup, and mocks only at boundaries you do not own.

Tests that assert implementation details, sleep to hide flakiness or mock the code under test break on harmless refactors and prove little.

Unit tests that read as named behavioral claims, run deterministically, and fail with a clear reason when the contract breaks.

Best for: Developers who are writing a new test file, adding cases, or fixing a flaky or unreadable test and want a consistent standard for names, mocks and assertions.

Not for: Anyone who needs to decide which tests a change owes, or who is writing integration or end-to-end tests, since this skill covers only the craft of unit tests.

- One behavior per test, with a name that states subject, scenario and expected outcome
- Expected values are literals worked out independently, never copied from production math
- No real time, randomness, network or filesystem, and no sleeps in a unit test
- Mock only boundaries you do not own, and treat every mock as an assumption
- Lists common mistakes such as the mirror test, the mega-test and the mock echo chamber

## SKILL.md

## Writing unit tests

**REQUIRED BACKGROUND:** the `principal-engineering` skill. `testing-changes` governs which tests a change owes; this skill is the craft of the tests themselves.

### Overview

A unit test is a behavioral claim with a name, read by the next engineer during a red build. Core principle: **test the contract, not the implementation.** The name carries the claim, and the test stays simple enough that it cannot itself be wrong.

### Contract over implementation

- Test through the public contract of the unit. A refactor that preserves behavior should not break tests; when it does, the tests were asserting the implementation.
- Do not assert call sequences, internal state, or that method A called method B, unless the interaction IS the contract (a required side effect on a boundary).
- Never derive the expected value from the production arithmetic, neither by reimplementing the formula nor by invoking the shared helper that computes it. Expected values are literals worked out independently (by hand, from a spec, from real data), with the derivation in a comment. Assert the rule the code must keep, never a capture of what it produced today.

### One behavior per test, named as the claim

- One behavior per test; splitting is cheaper than archaeology on a multi-assert failure.
- The name states subject, scenario, and expected outcome: `expired_token_is_rejected_with_401`, not `test_auth_3`. Test names describe behavior, state transitions, and invariants; never delivery order, ticket keys, or phases.
- Arrange, act, assert, visibly and in that order. No branching, loops, or logic in a test: a test with logic needs its own test. Shared setup earns a builder or a role-named fixture; a mystery blob fixture hides which arranged fact the assertion depends on. Generation and iteration live in builders and helpers, not in the test body. Property-based tests are the accepted form for invariants and follow their framework's shape; example-based tests stay logic-free.

### Determinism

- No real time, real randomness, real network, or real filesystem inside a unit test: inject every clock the code reads (the wall clock behind a timestamp as well as the simulation clock), seed or inject the randomness, mock the boundary.
- No sleeps. Waiting for async work is condition-based (poll the observable outcome with a deadline), never duration-based.
- A flaky test is red: fix it, or quarantine it under the exception in the red-test rule in `testing-changes`; re-running until green is silencing a detector.

### Mocks are assumptions

- Mock the boundaries you do not own (network, clock, filesystem, third-party services); prefer real collaborators for code you do own within the unit's reach. For owned wrappers around unowned resources (your repository class fronting the database), mock at the seam where owned code last touches the unowned resource, and keep the test data role-named and visible either way. Every mock hardcodes an assumption about a contract; a stale mock is how a suite stays green while the real integration is broken.
- When a test is mostly mock wiring, it is testing the mocks. Either widen the unit to something with real behavior or accept that this seam needs an integration test instead (and say which).
- **A double refuses what the real party refuses.** A fake that accepts more than the real thing hides exactly those defects, so list what each would reject in production (a shape, a precision, a missing field). Each external boundary gets at least one test fed real-shaped data, such as a captured sample, shown red against the defect it guards.
- **Inject where production reads.** A fake the code under test never reads makes every assertion vacuous; confirm the production path consumes the injected object.
- Fixtures are labeled snapshots of reality: minimal, role-named for their part in the scenario, updated deliberately when the contract changes, never regenerated blindly to make red go green. A fact the repository owns is read from its owner or pinned to it by a test (`keeping-one-source-of-truth`). A fixture that pauses time or starts past the interesting state gets a companion run from the real initial state, clocks moving.

### Assertions and failure paths

- Assert outcomes with values, not absence of exceptions. "It did not throw" claims almost nothing.
- Failure paths are first-class test subjects: the typed failure surfaces, the degraded mode is entered loudly, the guard actually guards (see `handling-failures`).
- Tests themselves follow the no-silent-swallows contract: no catch-and-ignore in test code, no conditional assertions that skip silently when a precondition is absent. A test that cannot run must fail or be visibly skipped with the reason.

### Common mistakes

- The mirror test: reimplementing the production logic to compute the expectation.
- The mock echo chamber: mocking your own class and asserting the mock.
- The mega-test: twelve assertions, one name, no way to know which claim broke.
- Shared mutable fixtures that make test order matter; every test builds or receives its own state.
- The sleep that "fixes" flakiness by making it rarer.
- Green-checking the fixture: editing expected values to match actual output without deriving why the new value is right.

## Writing Unit Tests FAQ

### What does writing-unit-tests actually enforce?

It enforces testing the public contract, one behavior per test, names that state the claim, no real time or network, no sleeps, mocks only at boundaries you do not own, and assertions on values.

### Does it need another skill installed?

The doc names the principal-engineering skill as required background and refers to testing-changes for which tests a change owes. This skill covers the test craft itself.

### How does it treat flaky tests?

A flaky test is treated as red. You fix it or quarantine it visibly with an owner. Re-running until green is called silencing a detector, and sleeps are rejected in favor of condition-based waits.

### Is Writing Unit Tests safe to install?

skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.

## Machine access

- [Sitemap](https://skillselion.com/sitemap.xml)
- [llms.txt](https://skillselion.com/llms.txt)
- [llms-full.txt](https://skillselion.com/llms-full.txt)
- [Developer resources](https://skillselion.com/developers)
- [OpenAPI spec](https://skillselion.com/openapi.json)
- [MCP handshake](https://skillselion.com/.well-known/mcp)
- [API catalog](https://skillselion.com/.well-known/api-catalog)
- [AI catalog](https://skillselion.com/.well-known/ai-catalog.json)
- [Pricing](https://skillselion.com/pricing.md)
- [Auth](https://skillselion.com/auth.md)
- [AGENTS.md](https://skillselion.com/AGENTS.md)
- [Contact](https://skillselion.com/contact)
- [Home](https://skillselion.com/)
