Write a unit test suite for one function, method, or class, covering happy path, error paths, boundaries, and edge cases, with every external dependency mocked at the unit boundary. Use when the QA or a developer agent needs unit tests for a unit of logic, test-first against a planned interface or alongside implementation.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Spgr Write Unit Test?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/z4gunn-spgr-write-unit-test)
---
name: spgr-write-unit-test
description: Write a unit test suite for one function, method, or class, covering happy path, error paths, boundaries, and edge cases, with every external dependency mocked at the unit boundary. Use when the QA or a developer agent needs unit tests for a unit of logic, test-first against a planned interface or alongside implementation.
disable-model-invocation: true
---
# write-unit-test
## Purpose
Produce a unit test suite for a single unit of logic so a developer agent has a fast, local feedback loop that pins behavior to one unit. Tests are written test-first where an interface is planned but not yet implemented, so the failing test exists before the implementation. The 100% branch coverage target on business logic is the minimum bar for meaningful confidence, not perfectionism. The output is source code, not an envelope artifact, so it is verified by spgr-run-tests and CI rather than by a registered schema.
## Inputs
| Field | Description |
|-------|-------------|
| `unit-under-test` | The function, method, or class to test, or the planned interface signature when writing test-first. Read existing code via spgr-read-file. |
| `expected-behavior` | Documented behavior the unit must satisfy. Read story acceptance criteria via spgr-read-artifact (acceptance-criteria schema, produced by spgr-write-acceptance-criteria), plus docstrings or the relevant section of the API spec. |
| `dependency-list` | The external systems and collaborators the unit calls (databases, HTTP clients, message queues, clocks), naming each unit boundary that must be mocked. |
| `test-runner` | The project test framework and mocking library from the tech-stack-decision artifact, plus the coverage tool and the mutation-testing tool (Mutmut for Python, Stryker for JS/TS). |
## Outputs
| Artifact | Description |
|----------|-------------|
| Unit test file | A test suite written to disk via spgr-write-file, holding happy path, error path, boundary condition, and edge case tests, plus the mock definitions for every external dependency. Written next to or under the unit per project convention. |
| Coverage result | Branch coverage generated by the run, at 100% on business logic functions. |
## Procedure
1. Read the unit under test and the expected behavior. Use spgr-read-file for source and spgr-read-artifact for the acceptance-criteria artifact. If acceptance criteria are missing or do not specify the error and boundary behavior the unit must produce, stop and escalate (see Notes), do not infer the contract.
2. Build the test case matrix before writing test code. Enumerate the happy path cases, the error path cases (invalid inputs, missing required fields, type mismatches, out-of-range values), the boundary cases (zero, max length, first and last item, empty collection), and the edge cases the happy path does not cover (concurrent calls, retry behavior, partial success states). In assist mode, write the matrix out for the implementing agent to review for completeness before writing the test code.
3. Apply YAGNI. Test only the behavior the acceptance criteria and documented contract specify. Do not add tests for behavior the unit is not required to have.
4. For test-first work, write the tests against the planned interface first and confirm they fail for the right reason before the implementation exists. The failing test precedes the implementation.
5. Mock every external dependency at the unit boundary, not at the implementation. Replace the HTTP client, not the function that calls it, so mocks stay stable when internal implementation changes. A unit test that touches a real network or disk is a mock boundary error.
6. Name each test as a full sentence describing what the unit does, for example "returns 0 when the input collection is empty", not "test_empty_input".
7. Make every test assert at least one meaningful outcome. Reject assertion-free tests. Make every test independent and order-independent, with no shared mutable state between tests.
8. Prefer one parameterized test over copy-paste repetition for boundary and error path coverage. Five boundary conditions are one parameterized test, not five duplicate tests.
9. Run the suite via spgr-run-tests with coverage. Confirm the suite completes in seconds and that any single test under 100ms, a slower test signals a mock boundary error. Confirm 100% branch coverage on business logic. Do not target 100% on infrastructure adapters, those are covered at the integration level.
10. Lint and format the test file clean before the change is committed. For TypeScript or JavaScript, conform to `.claude/references/typescript-standards.md` and pass `tsc --noEmit`. Keep the test addition to one logical change per commit.
11. Write the test file via spgr-write-file. Record any consequential testing choice (a mock boundary decision, a deliberate coverage exclusion on an infrastructure adapter) via spgr-log-decision.
## Notes
- This skill produces source code. Verification is by spgr-run-tests and CI, including the mutation-testing quality gate (Mutmut or Stryker), not by an envelope schema. Coverage percentage reports which lines ran, mutation testing reports whether the assertions catch a failure.
- Escalate via spgr-escalate when the acceptance criteria are missing or do not specify required error and boundary behavior, when the documented contract and the implementation disagree on expected behavior, or when the unit cannot be tested in isolation because a dependency has no mockable boundary. Return the precise list of what is missing rather than guessing the contract.
- 100% branch coverage is the target for business logic only, meaning pure functions, domain objects, and service layer methods. Infrastructure adapters such as database and HTTP clients are tested at the integration level, where 100% unit coverage is neither meaningful nor achievable.