Generate Tests
Create comprehensive test cases for functions and modules to ensure correctness and improve code coverage.
When to Use
- Following test-driven development (TDD) approach
- Adding tests to increase coverage
- Testing edge cases and error conditions
- Validating refactoring doesn't break functionality
Quick Reference
# Test generation pattern
def generate_tests(function, test_cases: List[Tuple]):
"""Create test cases for a function"""
for inputs, expected_output in test_cases:
result = function(*inputs)
assert result == expected_output, f"Failed for {inputs}"
# Example: test matrix multiply
test_cases = [
(([[1, 2], [3, 4]], [[1, 0], [0, 1]]), [[1, 2], [3, 4]]), # Identity
(([], []), []), # Empty
]
Workflow
- Analyze function: Understand inputs, outputs, side effects
- Identify test cases: Normal cases, edge cases, error cases
- Write assertions: Create expected output for each case
- Implement tests: Create test functions in test file
- Verify coverage: Check that tests exercise all code paths
Output Format
Test suite:
- Test class/module with clear naming
- Test methods (test_normal_case, test_edge_case, test_error_case)
- Setup/teardown if needed
- Clear assertions with error messages
- Coverage report showing lines tested
References
- See
run-tests skill for executing tests
- See
calculate-coverage skill for coverage analysis
- See CLAUDE.md > TDD in Key Development Principles
1---2name: generate-tests-63description: Create test cases for functions and modules. Use when implementing TDD or improving coverage.4---56# Generate Tests78Create comprehensive test cases for functions and modules to ensure correctness and improve code coverage.910## When to Use1112- Following test-driven development (TDD) approach13- Adding tests to increase coverage14- Testing edge cases and error conditions15- Validating refactoring doesn't break functionality1617## Quick Reference1819```python20# Test generation pattern21def generate_tests(function, test_cases: List[Tuple]):22 """Create test cases for a function"""23 for inputs, expected_output in test_cases:24 result = function(*inputs)25 assert result == expected_output, f"Failed for {inputs}"2627# Example: test matrix multiply28test_cases = [29 (([[1, 2], [3, 4]], [[1, 0], [0, 1]]), [[1, 2], [3, 4]]), # Identity30 (([], []), []), # Empty31]32```3334## Workflow35361. **Analyze function**: Understand inputs, outputs, side effects372. **Identify test cases**: Normal cases, edge cases, error cases383. **Write assertions**: Create expected output for each case394. **Implement tests**: Create test functions in test file405. **Verify coverage**: Check that tests exercise all code paths4142## Output Format4344Test suite:4546- Test class/module with clear naming47- Test methods (test_normal_case, test_edge_case, test_error_case)48- Setup/teardown if needed49- Clear assertions with error messages50- Coverage report showing lines tested5152## References5354- See `run-tests` skill for executing tests55- See `calculate-coverage` skill for coverage analysis56- See CLAUDE.md > TDD in Key Development Principles