unit-test-generator
Automatically generate comprehensive unit tests from source code with multiple testing framework support
View on GitHubTable of content
Automatically generate comprehensive unit tests from source code with multiple testing framework support
Installation
npx claude-plugins install @jeremylongshore/claude-code-plugins-plus/unit-test-generator
Contents
Folders: commands, skills
Files: LICENSE, README.md
Documentation
Automatically generate comprehensive unit tests from source code with intelligent framework detection and best practices.
Features
- Multi-framework support - Jest, pytest, JUnit, Go testing, RSpec, and more
- Intelligent test generation - Analyzes code to create relevant test cases
- Complete coverage - Happy paths, edge cases, error handling
- Mock generation - Automatic mocking of external dependencies
- Best practices - Arrange-Act-Assert, descriptive names, proper structure
Installation
/plugin install unit-test-generator@claude-code-plugins-plus
Usage
Generate tests for a file
/generate-tests src/utils/validator.js
Specify framework explicitly
/generate-tests --framework pytest src/api/users.py
Use shortcut
/gut models/UserModel.ts
What Gets Generated
The plugin creates test files with:
- Proper imports and setup - Framework-specific boilerplate
- Test suite organization - Logical grouping of related tests
- Comprehensive test cases:
- Valid inputs (typical scenarios)
- Invalid inputs (null, undefined, wrong types)
- Boundary conditions (limits, empty collections)
- Error scenarios (exceptions, failures)
- State changes (when applicable)
- Mocks and stubs - For external dependencies
- Clear assertions - Validating expected outcomes
- Helpful comments - Explaining complex scenarios
Supported Languages & Frameworks
| Language | Frameworks |
|---|---|
| JavaScript/TypeScript | Jest, Mocha, Vitest, Jasmine |
| Python | pytest, unittest, nose2 |
| Java | JUnit 5, TestNG |
| Go | testing package |
| Ruby | RSpec, Minitest |
| C# | xUnit, NUnit, MSTest |
| PHP | PHPUnit |
| Rust | cargo test |
Example Output
For a JavaScript validation function:
// Input: src/utils/validator.js
function validateEmail(email) {
if (!email) return false;
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
// Generated: src/utils/validator.test.js
describe('validateEmail', () => {
it('should return true for valid email addresses', () => {
expect(validateEmail('[email protected]')).toBe(true);
expect(validateEmail('[email protected]')).toBe(true);
});
it('should return false for invalid email addresses', () => {
expect(validateEmail('notanemail')).toBe(false);
expect(validateEmail('@example.com')).toBe(false);
expect(validateEmail('user@')).toBe(false);
});
it('should return false for null or undefined inputs', () => {
expect(validateEmail(null)).toBe(false);
expect(validateEmail(undefined)).toBe(false);
expect(validateEmail('')).toBe(false);
});
it('should handle edge cases', () => {
expect(validateEmail('a@b.c')).toBe(true); // Minimal valid email
expect(validateEmail('user@domain.co.uk')).toBe(true); // Multiple dots
});
});
Best Practices
The plugin follows testing best practices:
- Descriptive names - Clear what is tested and expected result
- Arrange-Act-Assert - Clear test structure
- Test isolation - No shared state between tests
- Single responsibility - One concept per test
- Mock external dependencies - Database calls, API requests, etc.
- Test both paths - Success and failure scenarios
- Edge cases - Boundaries, nulls, empty values
Requirements
- Claude Code CLI
- Testing framework installed in project (e.g.,
npm install --save-dev jest)
Tips
- Review generated tests and adjust for your specific needs
- Add integration tests for complex workflows
- Update tests when code changes
- Aim for high coverage but prioritize meaningful tests
- Use generated tests as a starting point, not final solution
License
MIT
Included Skills
This plugin includes 1 skill definition:
generating-unit-tests
|
View skill definition
Unit Test Generator
This skill provides automated assistance for unit test generator tasks.
Prerequisites
Before using this skill, ensure you have:
- Source code files requiring test coverage
- Testing framework installed (Jest, Mocha, pytest, JUnit, etc.)
- Understanding of code dependencies and external services to mock
- Test directory structure established (e.g.,
tests/,__tests__/,spec/) - Package configuration updated with test scripts
Instructions
Step 1: Analyze Source Code
Examine code structure and identify test requirements:
- Use Read tool to load source files from {baseDir}/src/
- Identify all functions, classes, and methods requiring tests
- Document function signatures, parameters, return types, and side effects
- Note external dependencies requiring mocking or stubbing
Step 2: Determine Testing Framework
Select appropriate testing framework based on language:
- JavaScript/TypeScript: Jest, Mocha, Jasmine, Vitest
- Python: pytest, unittest, nose2
- Java: JUnit 5, TestNG
- Go: testing package with testify assertions
- Ruby: RSpec, Minitest
Step 3: Generate Test Cases
Create comprehensive test suite covering:
- Happy path tests with valid inputs and expected outputs
- Edge case tests with boundary values (empty arrays, null, zero, max values)
- Error condition tests with invalid inputs
- Mock external dependencies (databases, APIs, file systems)
- Setup and teardown fixtures for test isolation
Step 4: Write Test Fil
…(truncated)