Initial commit

This commit is contained in:
Zhongwei Li
2025-11-29 18:29:07 +08:00
commit 8b4a1b1a99
75 changed files with 18583 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
---
name: grey-haven-tdd-python
description: "Python Test-Driven Development expertise with pytest, strict red-green-refactor methodology, FastAPI testing patterns, and Pydantic model testing. Use when implementing Python features with TDD, writing pytest tests, testing FastAPI endpoints, developing with test-first approach, or when user mentions 'Python TDD', 'pytest', 'FastAPI testing', 'red-green-refactor', 'Python unit tests', 'test-driven Python', or 'Python test coverage'."
---
# TDD Python Skill
Python Test-Driven Development following strict red-green-refactor cycle with pytest and comprehensive coverage.
## Description
Systematic Python implementation using TDD methodology, ensuring tests written first and driving design decisions.
## What's Included
- **Examples**: Python TDD cycles, FastAPI TDD, Pydantic model TDD
- **Reference**: pytest patterns, Python testing best practices
- **Templates**: pytest templates, TDD workflows
## Use When
- Implementing Python features with TDD
- FastAPI development
- Pydantic model development
## Related Agents
- `tdd-python-implementer`
**Skill Version**: 1.0

View File

@@ -0,0 +1,116 @@
# TDD Python Implementer Examples
Real-world Test-Driven Development examples for Python using pytest, unittest, and Python-specific testing patterns.
## Available Examples
### [pytest-tdd-example.md](pytest-tdd-example.md)
Complete TDD workflow using pytest - fixtures, parametrize, and modern testing patterns.
**Implements**: E-commerce shopping cart with discount rules
**Techniques**: pytest fixtures, parametrize decorator, conftest.py, pytest-cov
**Duration**: 45-minute TDD session
**Coverage**: 95% line coverage, 92% branch coverage
**Tests**: 18 tests, all passing
---
### [unittest-tdd-example.md](unittest-tdd-example.md)
TDD using unittest framework - TestCase classes, setUp/tearDown, assertions.
**Implements**: User authentication system with password hashing
**Techniques**: TestCase classes, setUp/tearDown, mock library, test discovery
**Duration**: 30-minute TDD session
**Coverage**: 94% line coverage, 90% branch coverage
**Tests**: 12 tests, all passing
---
### [async-testing-example.md](async-testing-example.md)
Testing async/await code with pytest-asyncio - coroutines, async context managers.
**Implements**: Async HTTP client with retry logic
**Techniques**: pytest-asyncio, asyncio.gather, async with, aioresponses mocking
**Duration**: 35-minute TDD session
**Coverage**: 93% line coverage
**Tests**: 15 tests, all async
---
### [mocking-strategies-example.md](mocking-strategies-example.md)
Comprehensive mocking strategies - when to mock, mock vs stub vs spy, patching.
**Implements**: Email notification service with external API
**Techniques**: unittest.mock, patch decorator, MagicMock, assert_called_with
**Duration**: 40-minute TDD session
**Coverage**: 96% line coverage
**Mocks**: 8 different mocking strategies demonstrated
---
### [fixtures-and-parametrize-example.md](fixtures-and-parametrize-example.md)
Advanced pytest features - fixtures for reusable setup, parametrize for multiple test cases.
**Implements**: Data validation pipeline with custom validators
**Techniques**: pytest fixtures, parametrize, fixture scope, conftest organization
**Duration**: 50-minute TDD session
**Coverage**: 97% line coverage
**Tests**: 24 tests from 8 parametrized functions
---
## Usage Patterns
### Learning TDD with pytest
Start with [pytest-tdd-example.md](pytest-tdd-example.md) for modern Python testing workflow.
### Learning unittest
Review [unittest-tdd-example.md](unittest-tdd-example.md) for traditional Python testing approach.
### Async Code
Reference [async-testing-example.md](async-testing-example.md) when testing async/await code.
### External Dependencies
Study [mocking-strategies-example.md](mocking-strategies-example.md) for mocking patterns.
### Test Organization
Learn from [fixtures-and-parametrize-example.md](fixtures-and-parametrize-example.md) for fixture patterns.
---
## Quick Reference
### pytest vs unittest
| Feature | pytest | unittest |
|---------|--------|----------|
| **Test Discovery** | Auto-discovers `test_*.py` | Requires `unittest.main()` |
| **Assertions** | Plain `assert` statements | `self.assertEqual()` methods |
| **Setup/Teardown** | Fixtures | `setUp()`/`tearDown()` |
| **Parametrization** | `@pytest.mark.parametrize` | Manual loops or subTest |
| **Mocking** | `mocker` fixture | `unittest.mock` |
| **Plugins** | Rich ecosystem | Standard library only |
### Test Naming Conventions
```python
# pytest
def test_should_return_empty_list_when_no_items():
pass
# unittest
class TestShoppingCart(unittest.TestCase):
def test_should_return_empty_list_when_no_items(self):
pass
```
### Coverage Goals
- **Line Coverage**: 90%+ (pytest: 95%, unittest: 94%)
- **Branch Coverage**: 85%+ (pytest: 92%, unittest: 90%)
- **Edge Cases**: All boundary conditions tested
- **Error Handling**: All exceptions tested
---
Return to [tdd-python-implementer agent](../tdd-python.md) | [reference/](../reference/INDEX.md) | [templates/](../templates/INDEX.md) | [checklists/](../checklists/INDEX.md)

View File

@@ -0,0 +1,97 @@
# TDD Python Reference
Comprehensive reference materials for Python Test-Driven Development - pytest, unittest, mocking, coverage, and Python-specific testing patterns.
## Available References
### [pytest-guide.md](pytest-guide.md)
Complete pytest reference - fixtures, parametrize, marks, plugins, and configuration.
**When to use**: Modern Python projects, learning pytest patterns
**Covers**: Fixtures, parametrize, marks, plugins, conftest.py, pytest.ini
**Key Topics**: Dependency injection, test discovery, assertion introspection
---
### [unittest-guide.md](unittest-guide.md)
Comprehensive unittest reference - TestCase classes, assertions, setUp/tearDown, test discovery.
**When to use**: Legacy projects, corporate environments, no external dependencies
**Covers**: TestCase structure, assertion methods, test organization, discovery
**Key Topics**: Class-based testing, setUp/tearDown, unittest.main()
---
### [mocking-reference.md](mocking-reference.md)
Complete mocking guide - Mock, MagicMock, patch, side_effect, assert patterns.
**When to use**: Testing external dependencies, isolating units, verifying interactions
**Covers**: unittest.mock, pytest-mock, mocking strategies, anti-patterns
**Key Topics**: Mock vs stub vs spy, patching strategies, assertion methods
---
### [coverage-guide.md](coverage-guide.md)
Coverage analysis and interpretation - tools, metrics, thresholds, CI integration.
**When to use**: Measuring test effectiveness, finding gaps, enforcing quality gates
**Covers**: coverage.py, pytest-cov, branch coverage, HTML reports, exclusions
**Key Topics**: 80% line coverage target, critical path coverage, differential coverage
---
### [python-specific-testing.md](python-specific-testing.md)
Python-specific testing patterns - async/await, decorators, generators, context managers, type hints.
**When to use**: Testing Python-specific features
**Covers**: pytest-asyncio, testing decorators, generator testing, mypy integration
**Key Topics**: Async testing, decorator verification, StopIteration, type checking
---
## Quick Reference
### pytest vs unittest
| Feature | pytest | unittest |
|---------|--------|----------|
| **Style** | Functional | Class-based |
| **Assertions** | Plain `assert` | `self.assertEqual()` |
| **Setup** | Fixtures | `setUp()/tearDown()` |
| **Discovery** | Automatic | `unittest.main()` |
| **Dependencies** | pip install pytest | Standard library |
### Test Organization
```
project/
├── app/
│ └── module.py
└── tests/
├── conftest.py # Shared fixtures
├── test_module.py # Mirror source structure
└── integration/
└── test_api.py
```
### Coverage Goals
| Metric | Minimum | Target | Critical Path |
|--------|---------|--------|---------------|
| Line Coverage | 80% | 90%+ | 100% |
| Branch Coverage | 75% | 85%+ | 100% |
| Function Coverage | 85% | 90%+ | 100% |
### Mocking Decision Matrix
| Dependency Type | Strategy | Tool |
|----------------|----------|------|
| HTTP API | Stub responses | aioresponses, responses |
| Database | Test database | pytest fixtures |
| File I/O | Mock or tmp_path | unittest.mock, pytest |
| Time/Random | Patch | patch('time.time') |
| External Service | Mock | unittest.mock.Mock |
---
Return to [tdd-python-implementer agent](../tdd-python.md) | [examples/](../examples/INDEX.md) | [templates/](../templates/INDEX.md) | [checklists/](../checklists/INDEX.md)