Pytest Test Creator Skill
Auto-generate comprehensive unit tests for Python codebases, run tests with pytest, and generate coverage reports using uv for package management.
Workflow
Step 1: Analyze Codebase
- Find Python files with Glob tool
- Read source code to identify functions, classes, type signatures, docstrings
- Check for existing tests to avoid duplication
Step 2: Generate Tests
For each module, create tests/test_<module_name>.py covering:
- Happy path: Normal expected usage
- Edge cases: Boundary conditions, empty inputs
- Error cases: Invalid inputs, exceptions
- Integration: How components work together
Use fixtures for sample data, mock objects, and shared setup.
Step 3: Setup Test Environment
Ensure dependencies are in pyproject.toml:
[project.optional-dependencies]
test = [
"pytest>=7.4.0",
"pytest-cov>=4.1.0",
"pytest-mock>=3.11.0",
]
uv add --dev pytest pytest-cov pytest-mock
Step 4: Run Tests
uv run pytest --cov=src --cov-report=html --cov-report=term-missing
Configuration (pyproject.toml)
[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = ["test_*.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
addopts = ["--strict-markers", "--strict-config", "-ra"]
[tool.coverage.run]
source = ["src"]
omit = ["*/tests/*", "*/__pycache__/*"]
[tool.coverage.report]
precision = 2
show_missing = true
skip_covered = false
Best Practices
- Naming:
test_function_name_with_valid_input(), not test_1()
- Organization: One test file per source module, group related tests in classes
- Coverage: Aim for >80%, focus on critical business logic first
- Independence: No shared mutable state between tests
- What to test: Business logic, data transformations, error handling, edge cases
- What to skip: Third-party library internals, auto-generated code, trivial getters/setters
Scripts
- scripts/generate_tests.py - Analyzes source files and generates test templates with fixtures and parametrize decorators
- scripts/run_tests.py - Runs pytest with coverage and generates reports (
--coverage, --html flags)
References
- references/pytest-patterns.md - Comprehensive pytest patterns and examples
- references/mocking-guide.md - Guide to mocking with pytest-mock
- references/coverage-config.md - Coverage configuration and best practices
- references/uv-commands.md - UV package management commands
- references/databricks-notebook-testing.md - Testing Databricks Python notebooks
1---2name: pytest-test-creator3description: Auto-generate comprehensive unit tests for Python codebases with pytest, coverage reports, and uv package management. Use when creating tests, setting up pytest, analyzing test coverage, or testing Databricks notebooks with local Spark. Generates fixtures, parametrized tests, mocking, and coverage reports.4---56# Pytest Test Creator Skill78Auto-generate comprehensive unit tests for Python codebases, run tests with pytest, and generate coverage reports using `uv` for package management.910## Workflow1112### Step 1: Analyze Codebase13141. Find Python files with Glob tool152. Read source code to identify functions, classes, type signatures, docstrings163. Check for existing tests to avoid duplication1718### Step 2: Generate Tests1920For each module, create `tests/test_<module_name>.py` covering:21- **Happy path**: Normal expected usage22- **Edge cases**: Boundary conditions, empty inputs23- **Error cases**: Invalid inputs, exceptions24- **Integration**: How components work together2526Use fixtures for sample data, mock objects, and shared setup.2728### Step 3: Setup Test Environment2930Ensure dependencies are in `pyproject.toml`:3132```toml33[project.optional-dependencies]34test = [35 "pytest>=7.4.0",36 "pytest-cov>=4.1.0",37 "pytest-mock>=3.11.0",38]39```4041```bash42uv add --dev pytest pytest-cov pytest-mock43```4445### Step 4: Run Tests4647```bash48uv run pytest --cov=src --cov-report=html --cov-report=term-missing49```5051## Configuration (pyproject.toml)5253```toml54[tool.pytest.ini_options]55testpaths = ["tests"]56python_files = ["test_*.py"]57python_classes = ["Test*"]58python_functions = ["test_*"]59addopts = ["--strict-markers", "--strict-config", "-ra"]6061[tool.coverage.run]62source = ["src"]63omit = ["*/tests/*", "*/__pycache__/*"]6465[tool.coverage.report]66precision = 267show_missing = true68skip_covered = false69```7071## Best Practices7273- **Naming**: `test_function_name_with_valid_input()`, not `test_1()`74- **Organization**: One test file per source module, group related tests in classes75- **Coverage**: Aim for >80%, focus on critical business logic first76- **Independence**: No shared mutable state between tests77- **What to test**: Business logic, data transformations, error handling, edge cases78- **What to skip**: Third-party library internals, auto-generated code, trivial getters/setters7980## Scripts8182- **scripts/generate_tests.py** - Analyzes source files and generates test templates with fixtures and parametrize decorators83- **scripts/run_tests.py** - Runs pytest with coverage and generates reports (`--coverage`, `--html` flags)8485## References8687- [references/pytest-patterns.md](references/pytest-patterns.md) - Comprehensive pytest patterns and examples88- [references/mocking-guide.md](references/mocking-guide.md) - Guide to mocking with pytest-mock89- [references/coverage-config.md](references/coverage-config.md) - Coverage configuration and best practices90- [references/uv-commands.md](references/uv-commands.md) - UV package management commands91- [references/databricks-notebook-testing.md](references/databricks-notebook-testing.md) - Testing Databricks Python notebooks