Claude-code-plugins-plus-skills generating-unit-tests
install
source · Clone the upstream repo
git clone https://github.com/jeremylongshore/claude-code-plugins-plus-skills
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/jeremylongshore/claude-code-plugins-plus-skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/plugins/testing/unit-test-generator/skills/generating-unit-tests" ~/.claude/skills/jeremylongshore-claude-code-plugins-plus-skills-generating-unit-tests-5b78ed && rm -rf "$T"
manifest:
plugins/testing/unit-test-generator/skills/generating-unit-tests/SKILL.mdsource content
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
, or Gojunit-jupiter
)testing - Source code with clear function signatures, type annotations, or JSDoc comments
- Test directory structure established (
,__tests__/
,tests/
, orspec/
)*_test.go - Mocking library available (
,jest.mock
,unittest.mock
, orMockito
)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.,
withoutsrc/utils/parser.ts
).__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
, maximum values.undefined - 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:
- Match the existing test file naming pattern (
,*.test.ts
,*.spec.js
).test_*.py - Group tests in
/describe
blocks by function name.context - Use
/beforeEach
for setup and teardown.afterEach - Include inline comments explaining non-obvious test rationale.
- Match the existing test file naming pattern (
- Run the generated tests to verify they pass, then check coverage to confirm the target function is fully exercised.
- 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 |
|---|---|---|
on import | Test file path does not match project module resolution | Check paths and in Jest config |
| Mock not intercepting calls | Mock defined after module import caches the real implementation | Move calls to the top of the file before any imports |
| Async test timeout | Promise never resolves due to missing or unhandled rejection | Add before async calls; increase timeout with |
| Tests pass alone but fail together | Shared mutable state leaking between tests | Reset state in ; avoid module-level variables; use |
| Snapshot mismatch on first run | No existing snapshot baseline | Run with on first execution to create the baseline |
Examples
Jest test for a string utility:
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:
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/