Write unit tests in your project's framework covering happy paths, boundaries, errors and mocked dependencies, with assumed behavior flagged and tests actually run.
---
name: test-generator
description: Writes unit tests (and focused integration tests) for existing functions or modules in the project's own framework - Jest, Vitest, pytest, Go testing, RSpec and others - covering the happy path, boundaries, error handling and mocked external calls, using the Arrange-Act-Assert pattern. States the behavior each test assumes and runs the tests when possible. Use when asked to write tests, add test coverage, generate unit tests, or test a function or edge cases.
compatibility: Writing tests needs read access to the code. Running them needs the project's test toolchain installed.
metadata:
version: "1.3.0"
---
# Test Generator
Good tests encode intended behavior. The code alone shows what a function does, not what it should do, so this skill separates the two: tests for documented or confirmed behavior, and questions for behavior that is unspecified.
## Inputs to collect
- The function, class or module to test, and how it is used (callers, docs, types, existing tests).
- The test framework and conventions already in the repo: config files (`vitest.config.*`, `jest.config.*`, `pytest.ini`/`pyproject.toml`, `*_test.go`, `spec/`), file naming and location, assertion and mocking style. Never introduce a second framework without asking.
- The spec for unclear cases (empty input, negative values, invalid types, errors). If there is no spec, ask, or write the test against current behavior and label it "characterizes current behavior".
## Workflow
1. **Read the code under test** and list its behaviors: inputs, outputs, side effects, thrown errors, external calls (HTTP, database, filesystem, clock, randomness).
2. **Draft a case list** before writing code:
- happy path(s)
- boundaries: empty, zero, one, max, off-by-one edges
- invalid input and error paths (with the error type/message the code really uses)
- state and side effects (what was written, called, or emitted)
Mark each case as *specified* (docs, types, user) or *assumed*. Show assumed cases to the user if they decide pass/fail.
3. **Isolate external dependencies** with the framework's own tools (`vi.mock`/`vi.fn`, `jest.mock`/`jest.fn`, `unittest.mock.patch`/pytest `monkeypatch`, Go interfaces with fakes). Mock at the boundary (HTTP client, repository), not the code under test. Freeze time and seed randomness when outputs depend on them.
4. **Write tests** in Arrange-Act-Assert form, one behavior per test, with names that state the behavior. Prefer exact assertions over snapshots for logic.
5. **Run them**: the project's test command (for example `npx vitest run src/cart.test.ts`, `npx jest cart`, `pytest -q tests/test_cart.py`, `go test ./...`). Report actual results. A new test that fails may have found a bug: report it with the input and output and ask whether the code or the test is wrong. Do not change the code under test to make tests pass unless asked.
6. **Report coverage honestly**: which behaviors are covered, which are not. Run the coverage tool only if configured; do not quote coverage numbers you did not measure.
## Example (Vitest; Jest is identical apart from the import)
Spec stated in the function's doc comment: `calculateTotal(items)` returns the sum of `price * qty`; `qty` must be a non-negative integer and `price` a non-negative finite number, otherwise it throws `RangeError`; an empty list returns 0. Prices are integer cents.
```ts
import { describe, it, expect } from 'vitest';
import { calculateTotal } from './cart';
describe('calculateTotal', () => {
it('sums price * qty across lines', () => {
expect(calculateTotal([{ price: 10, qty: 2 }, { price: 5, qty: 1 }])).toBe(25);
});
it('returns 0 for an empty list', () => {
expect(calculateTotal([])).toBe(0);
});
it('allows qty 0 (line contributes nothing)', () => {
expect(calculateTotal([{ price: 10, qty: 0 }])).toBe(0);
});
it('throws RangeError for a negative qty', () => {
expect(() => calculateTotal([{ price: 10, qty: -1 }])).toThrow(RangeError);
});
it('throws RangeError for a fractional qty', () => {
expect(() => calculateTotal([{ price: 10, qty: 1.5 }])).toThrow(RangeError);
});
it('keeps cents exact when prices are integers in cents', () => {
expect(calculateTotal([{ price: 1999, qty: 3 }])).toBe(5997);
});
});
```
If the doc comment did not say what happens for a negative quantity, the two RangeError tests would be *assumed* cases: ask first, or test what the code currently returns and flag it. (A plain `items.reduce((s, i) => s + i.price * i.qty, 0)` returns -10 for qty -1 and fails those two tests.)
## Output format
- Test file(s) at the conventional path, ready to run.
- Case list marked specified / assumed / characterizes current behavior.
- Command run and its real output, or "not run" with the reason.
- Bugs or spec questions found.
## Limitations
- Generated tests can only be as correct as the spec they encode; review assumed cases.
- Integration tests against real databases or services need a test environment the user provides; do not point tests at production.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A Vitest project has a calculateTotal(items) helper with a doc comment describing valid input, but no tests.
Write Vitest tests for calculateTotal in src/cart.ts. Doc comment: returns sum of price*qty; qty must be a non-negative integer and price a non-negative finite number, otherwise RangeError; empty list returns 0. Prices are in cents.
src/cart.test.ts with six tests like the skill's example: sum of two lines = 25, empty list = 0, qty 0 contributes nothing, RangeError for qty -1, RangeError for qty 1.5, 1999 x 3 = 5997. A case list marking all six as specified by the doc comment, the run command (npx vitest run src/cart.test.ts) with its real output or 'not run', and no coverage percentage unless measured.
AI Agent Skills are reusable workflow packages that extend your AI coding assistant with multi-step capabilities. Install test-generator by following these steps.
SKILL.md inside, in the location documented by your client. Do not assume every AI application reads the same directory.Requires an Agent Skills-compatible client; supported locations and activation vary by version. Read the SKILL.md setup guide. MCP connects tools and data and does not, by itself, install a skill.