| name | generating-unit-tests |
| description | Test automatically generate comprehensive unit tests from source code covering happy paths, edge cases, and error conditions.
Use when creating test coverage for functions, classes, or modules.
Trigger with phrases like "generate unit tests", "create tests for", or "add test coverage".
|
| allowed-tools | Read, Write, Edit, Grep, Glob, Bash(test:unit-*) |
| version | 1.21.0 |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
| license | MIT |
| tags | ["testing","unit-tests"] |
| compatibility | Designed for Claude Code, also compatible with Codex and OpenClaw |
Unit Test Generator
Overview
Automatically generate comprehensive unit tests from source code analysis covering happy paths, edge cases, boundary conditions, and error handling. Supports Jest, Vitest, Mocha (JavaScript/TypeScript), pytest (Python), JUnit 5 (Java), and Go testing with testify.
Prerequisites
- Testing framework installed and configured (
jest, vitest, pytest, junit-jupiter, or Go testing)
- Source code with clear function signatures, type annotations, or JSDoc comments
- Test directory structure established (
__tests__/, tests/, spec/, or *_test.go)
- Mocking library available (
jest.mock, unittest.mock, Mockito, or gomock)
- Package scripts configured to run tests (
npm test, pytest, go test)
Instructions
- Scan the codebase with Glob to locate source files that lack corresponding test files (e.g.,
src/utils/parser.ts without __tests__/parser.test.ts).
- Read each untested source file and extract:
- All exported functions and class methods with their signatures.
- Parameter types, return types, and thrown exceptions.
- External dependencies (imports from other modules, third-party libraries, I/O).
- Pure vs. impure function classification.
- For each function, generate test cases in these categories:
- Happy path: Valid inputs producing expected outputs (at least 2 cases).
- Edge cases: Empty strings, empty arrays, zero, negative numbers,
null, undefined, maximum values.
- Error conditions: Invalid types, missing required fields, network failures, permission errors.
- Boundary values: Off-by-one, integer overflow, string length limits.
- Create mock declarations for all external dependencies:
- Database calls return predictable fixture data.
- HTTP clients return canned responses with configurable status codes.
- File system operations use in-memory buffers or temp directories.
- Write the test file following project conventions: