generating-unit-tests
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".
Best use case
generating-unit-tests is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
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".
Teams using generating-unit-tests should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/generating-unit-tests/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How generating-unit-tests Compares
| Feature / Agent | generating-unit-tests | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
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".
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
Related Guides
AI Agents for Coding
Browse AI agent skills for coding, debugging, testing, refactoring, code review, and developer workflows across Claude, Cursor, and Codex.
Best AI Skills for Claude
Explore the best AI skills for Claude and Claude Code across coding, research, workflow automation, documentation, and agent operations.
ChatGPT vs Claude for Agent Skills
Compare ChatGPT and Claude for AI agent skills across coding, writing, research, and reusable workflow execution.
SKILL.md Source
# 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
1. 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`).
2. 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.
3. 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.
4. 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.
5. Write the test file following project conventions:
- Match the existing test file naming pattern (`*.test.ts`, `*.spec.js`, `test_*.py`).
- Group tests in `describe`/`context` blocks by function name.
- Use `beforeEach`/`afterEach` for setup and teardown.
- Include inline comments explaining non-obvious test rationale.
6. Run the generated tests to verify they pass, then check coverage to confirm the target function is fully exercised.
7. Report coverage gaps and suggest additional test cases for uncovered branches.
## Output
- Test files placed alongside source files or in the project's test directory
- Mock/stub files for external dependencies
- Coverage report showing line, branch, and function coverage for tested modules
- List of remaining coverage gaps with suggested test cases
## Error Handling
| Error | Cause | Solution |
|-------|-------|---------|
| `Cannot find module` on import | Test file path does not match project module resolution | Check `tsconfig.json` paths and `moduleNameMapper` in Jest config |
| Mock not intercepting calls | Mock defined after module import caches the real implementation | Move `jest.mock()` calls to the top of the file before any imports |
| Async test timeout | Promise never resolves due to missing `await` or unhandled rejection | Add `await` before async calls; increase timeout with `jest.setTimeout()` |
| Tests pass alone but fail together | Shared mutable state leaking between tests | Reset state in `afterEach`; avoid module-level variables; use `jest.isolateModules()` |
| Snapshot mismatch on first run | No existing snapshot baseline | Run with `--updateSnapshot` on first execution to create the baseline |
## Examples
**Jest test for a string utility:**
```typescript
import { slugify } from '../src/utils/slugify';
describe('slugify', () => {
it('converts spaces to hyphens', () => {
expect(slugify('hello world')).toBe('hello-world');
});
it('lowercases all characters', () => {
expect(slugify('Hello World')).toBe('hello-world');
});
it('removes special characters', () => {
expect(slugify('hello@world!')).toBe('helloworld');
});
it('handles empty string', () => {
expect(slugify('')).toBe('');
});
it('trims leading and trailing whitespace', () => {
expect(slugify(' spaced ')).toBe('spaced');
});
});
```
**pytest test for a data validator:**
```python
import pytest
from myapp.validators import validate_email
class TestValidateEmail:
def test_accepts_valid_email(self):
assert validate_email("user@example.com") is True
def test_rejects_missing_at_sign(self):
assert validate_email("userexample.com") is False
def test_rejects_empty_string(self):
assert validate_email("") is False
def test_rejects_none(self):
with pytest.raises(TypeError):
validate_email(None)
```
## Resources
- Jest documentation: https://jestjs.io/docs/getting-started
- Vitest documentation: https://vitest.dev/guide/
- pytest documentation: https://docs.pytest.org/
- JUnit 5 User Guide: https://junit.org/junit5/docs/current/user-guide/
- Go testing package: https://pkg.go.dev/testing
- AAA pattern (Arrange-Act-Assert): https://automationpanda.com/2020/07/07/arrange-act-assert-pattern-for-python-unit-tests/Related Skills
generating-test-reports
Generate comprehensive test reports with metrics, coverage, and visualizations. Use when performing specialized testing. Trigger with phrases like "generate test report", "create test documentation", or "show test metrics".
generating-test-doubles
Generate mocks, stubs, spies, and fakes for dependency isolation. Use when creating mocks, stubs, or test isolation fixtures. Trigger with phrases like "generate mocks", "create test doubles", or "setup stubs".
generating-test-data
Generate realistic test data including edge cases and boundary conditions. Use when creating realistic fixtures or edge case test data. Trigger with phrases like "generate test data", "create fixtures", or "setup test database".
managing-snapshot-tests
Create and validate component snapshots for UI regression testing. Use when performing specialized testing. Trigger with phrases like "update snapshots", "test UI snapshots", or "validate component snapshots".
running-smoke-tests
Execute fast smoke tests validating critical functionality after deployment. Use when performing specialized testing. Trigger with phrases like "run smoke tests", "quick validation", or "test critical paths".
tracking-regression-tests
Track and manage regression test suites across releases. Use when performing specialized testing. Trigger with phrases like "track regressions", "manage regression suite", or "validate against baseline".
running-performance-tests
Execute load testing, stress testing, and performance benchmarking. Use when performing specialized testing. Trigger with phrases like "run load tests", "test performance", or "benchmark the system".
running-mutation-tests
Execute mutation testing to evaluate test suite effectiveness. Use when performing specialized testing. Trigger with phrases like "run mutation tests", "test the tests", or "validate test effectiveness".
running-integration-tests
Execute integration tests validating component interactions and system integration. Use when performing specialized testing. Trigger with phrases like "run integration tests", "test integration", or "validate component interactions".
running-e2e-tests
Execute end-to-end tests covering full user workflows across frontend and backend. Use when performing specialized testing. Trigger with phrases like "run end-to-end tests", "test user flows", or "execute E2E suite".
managing-database-tests
Test database testing including fixtures, transactions, and rollback management. Use when performing specialized testing. Trigger with phrases like "test the database", "run database tests", or "validate data integrity".
running-chaos-tests
Execute chaos engineering experiments to test system resilience. Use when performing specialized testing. Trigger with phrases like "run chaos tests", "test resilience", or "inject failures".