Test Writer
Generate comprehensive unit and integration tests for a given file or function, auto-detecting the project test framework and matching the existing test style.
Instructions
When invoked via /test-writer:
Identify the target. Ask the user: which file or function should tests be written for? Accept:
- An absolute or relative file path (e.g.
src/utils/parser.ts)
- A function or method name (ask which file it lives in if ambiguous)
Read the target file using Read. Understand:
- All exported/public functions, classes, and methods
- Their parameter types, return types, and documented or inferred behavior
- Dependencies (imports/requires) that will need to be mocked
Detect the test framework using Glob and Read:
- Jest: look for
jest.config.*, or "jest" key in package.json
- Vitest: look for
vitest.config.*, or "vitest" in package.json devDependencies
- pytest: look for
pytest.ini, pyproject.toml containing [tool.pytest.ini_options], or conftest.py
- Go test: look for
go.mod and any existing *_test.go files
- PHPUnit: look for
phpunit.xml or phpunit.xml.dist, or phpunit/phpunit in composer.json
- Mocha: look for
.mocharc.*, or "mocha" in package.json devDependencies
- RSpec: look for
.rspec, spec/spec_helper.rb, or rspec in Gemfile
- JUnit: look for
junit in pom.xml or build.gradle/build.gradle.kts
- xUnit / NUnit / MSTest: look for
xunit, NUnit, or MSTest package references in *.csproj
- If multiple are present, ask the user which to use.
Match existing test style. Use Glob to find existing test files matching **/*.test.*, **/*.spec.*, **/*_test.*, or tests/**/*. Read 1–2 representative test files to capture:
- Describe/context block structure
- Assertion library and style (
expect, assert, should)
- How mocks, stubs, and fixtures are set up
- Import paths and module resolution patterns
Determine the test file location and name:
- If existing tests are co-located (e.g.
src/foo.ts → src/foo.test.ts), follow that pattern.
- If existing tests live in a
tests/ or __tests__/ directory, mirror the source path there.
- If no existing tests exist, default to co-located.
Generate tests covering:
- Happy path: normal, valid inputs produce the expected output
- Edge cases: empty string/array, zero,
null/undefined/None, maximum values, boundary conditions (e.g. off-by-one)
- Invalid input: wrong type, missing required fields, malformed data — verify errors are thrown or returned correctly
- Error conditions: simulate dependency failures using mocks/stubs; verify the function handles them gracefully
- All public functions/methods in the target file — do not skip any exported surface
Follow the detected framework's idioms exactly. Use the same describe block nesting, assertion style, and mock setup patterns found in existing tests.
Write the test file:
- If the test file does not exist, use Write to create it.
- If the test file already exists, use Read to review it, then use Edit to append new test cases — never overwrite existing tests.
Run the tests using Bash. For JS/TS frameworks, prefer the project's own test script or local binary — do not use npx, which can download and execute a package from the network if it isn't installed locally:
- Jest/Vitest/Mocha:
npm test -- <testfile> if package.json defines a test script; otherwise node_modules/.bin/jest <testfile> (or vitest run / mocha). If neither exists, ask the user before falling back to npx.
- pytest:
python -m pytest <testfile> -v
- Go:
go test ./... -run <TestName>
- PHPUnit:
vendor/bin/phpunit <testfile>
- RSpec:
bundle exec rspec <testfile>
- JUnit:
mvn test -Dtest=<TestClass> or gradle test --tests <TestClass>
- xUnit/NUnit/MSTest:
dotnet test --filter <TestClass>
- Report pass/fail counts and any error output.
- If tests fail, diagnose the root cause (missing mock, wrong import path, API mismatch) and fix before finishing. Do not leave the user with a broken test file.
Output Format
After writing and running tests, summarize:
## Test Writer — <target file> — <date>
### Framework Detected
<framework name and version if available>
### Test File Written
`<path/to/test-file>`
### Coverage
| Function / Method | Tests Written |
|-------------------|---------------|
| functionName | 4 (happy, edge, invalid, error) |
| anotherFunction | 3 (happy, edge, error) |
### Run Results
Tests: X passed, Y failed, Z total
### Notes
<Any caveats — e.g. "mock for ExternalService is approximate; update if the interface changes">
Notes
- This skill writes tests only — it does not modify production code.
- If the target file has no exports or public surface (e.g. it is a CLI entry point), generate integration-style tests that exercise the module end-to-end via its public interface or subprocess.
- Prefer deterministic tests. Avoid
Date.now(), Math.random(), or other non-deterministic values in assertions without mocking them first.
1---2name: test-writer3description: Generates comprehensive unit and integration tests for a given file or function, auto-detecting the project test framework and matching existing test style.4---56# Test Writer78Generate comprehensive unit and integration tests for a given file or function, auto-detecting the project test framework and matching the existing test style.910## Instructions1112When invoked via `/test-writer`:13141. **Identify the target.** Ask the user: which file or function should tests be written for? Accept:15 - An absolute or relative file path (e.g. `src/utils/parser.ts`)16 - A function or method name (ask which file it lives in if ambiguous)17182. **Read the target file** using Read. Understand:19 - All exported/public functions, classes, and methods20 - Their parameter types, return types, and documented or inferred behavior21 - Dependencies (imports/requires) that will need to be mocked22233. **Detect the test framework** using Glob and Read:24 - **Jest**: look for `jest.config.*`, or `"jest"` key in `package.json`25 - **Vitest**: look for `vitest.config.*`, or `"vitest"` in `package.json` devDependencies26 - **pytest**: look for `pytest.ini`, `pyproject.toml` containing `[tool.pytest.ini_options]`, or `conftest.py`27 - **Go test**: look for `go.mod` and any existing `*_test.go` files28 - **PHPUnit**: look for `phpunit.xml` or `phpunit.xml.dist`, or `phpunit/phpunit` in `composer.json`29 - **Mocha**: look for `.mocharc.*`, or `"mocha"` in `package.json` devDependencies30 - **RSpec**: look for `.rspec`, `spec/spec_helper.rb`, or `rspec` in `Gemfile`31 - **JUnit**: look for `junit` in `pom.xml` or `build.gradle`/`build.gradle.kts`32 - **xUnit / NUnit / MSTest**: look for `xunit`, `NUnit`, or `MSTest` package references in `*.csproj`33 - If multiple are present, ask the user which to use.34354. **Match existing test style.** Use Glob to find existing test files matching `**/*.test.*`, `**/*.spec.*`, `**/*_test.*`, or `tests/**/*`. Read 1–2 representative test files to capture:36 - Describe/context block structure37 - Assertion library and style (`expect`, `assert`, `should`)38 - How mocks, stubs, and fixtures are set up39 - Import paths and module resolution patterns40415. **Determine the test file location and name:**42 - If existing tests are co-located (e.g. `src/foo.ts` → `src/foo.test.ts`), follow that pattern.43 - If existing tests live in a `tests/` or `__tests__/` directory, mirror the source path there.44 - If no existing tests exist, default to co-located.45466. **Generate tests** covering:47 - **Happy path**: normal, valid inputs produce the expected output48 - **Edge cases**: empty string/array, zero, `null`/`undefined`/`None`, maximum values, boundary conditions (e.g. off-by-one)49 - **Invalid input**: wrong type, missing required fields, malformed data — verify errors are thrown or returned correctly50 - **Error conditions**: simulate dependency failures using mocks/stubs; verify the function handles them gracefully51 - **All public functions/methods** in the target file — do not skip any exported surface5253 Follow the detected framework's idioms exactly. Use the same describe block nesting, assertion style, and mock setup patterns found in existing tests.54557. **Write the test file:**56 - If the test file does not exist, use Write to create it.57 - If the test file already exists, use Read to review it, then use Edit to append new test cases — never overwrite existing tests.58598. **Run the tests** using Bash. For JS/TS frameworks, prefer the project's own test script or local binary — do not use `npx`, which can download and execute a package from the network if it isn't installed locally:60 - Jest/Vitest/Mocha: `npm test -- <testfile>` if `package.json` defines a test script; otherwise `node_modules/.bin/jest <testfile>` (or `vitest run` / `mocha`). If neither exists, ask the user before falling back to `npx`.61 - pytest: `python -m pytest <testfile> -v`62 - Go: `go test ./... -run <TestName>`63 - PHPUnit: `vendor/bin/phpunit <testfile>`64 - RSpec: `bundle exec rspec <testfile>`65 - JUnit: `mvn test -Dtest=<TestClass>` or `gradle test --tests <TestClass>`66 - xUnit/NUnit/MSTest: `dotnet test --filter <TestClass>`67 - Report pass/fail counts and any error output.68 - If tests fail, diagnose the root cause (missing mock, wrong import path, API mismatch) and fix before finishing. Do not leave the user with a broken test file.6970## Output Format7172After writing and running tests, summarize:7374```75## Test Writer — <target file> — <date>7677### Framework Detected78<framework name and version if available>7980### Test File Written81`<path/to/test-file>`8283### Coverage84| Function / Method | Tests Written |85|-------------------|---------------|86| functionName | 4 (happy, edge, invalid, error) |87| anotherFunction | 3 (happy, edge, error) |8889### Run Results90Tests: X passed, Y failed, Z total9192### Notes93<Any caveats — e.g. "mock for ExternalService is approximate; update if the interface changes">94```9596## Notes9798- This skill writes tests only — it does not modify production code.99- If the target file has no exports or public surface (e.g. it is a CLI entry point), generate integration-style tests that exercise the module end-to-end via its public interface or subprocess.100- Prefer deterministic tests. Avoid `Date.now()`, `Math.random()`, or other non-deterministic values in assertions without mocking them first.