From python-library-distribution
Designs pytest test suites for Python libraries with fixtures, parametrization, mocking, Hypothesis property-based testing, and CI configuration. For creating tests, improving coverage, or setting up testing infrastructure.
How this skill is triggered — by the user, by Claude, or both
Slash command
/python-library-distribution:testing-strategyThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
```bash
pytest # Run tests
pytest --cov=my_library # With coverage
pytest -x # Stop on first failure
pytest -k "test_encode" # Run matching tests
# pyproject.toml
[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = "-ra -q --cov=my_library --cov-fail-under=85"
[tool.coverage.run]
branch = true
source = ["src/my_library"]
tests/
├── conftest.py # Shared fixtures
├── test_encoding.py
└── test_decoding.py
Basic test:
def test_encode_valid_input():
result = encode(37.7749, -122.4194)
assert isinstance(result, str)
assert len(result) == 12
Parametrization:
@pytest.mark.parametrize("lat,lon,expected", [
(37.7749, -122.4194, "9q8yy"),
(40.7128, -74.0060, "dr5ru"),
])
def test_known_values(lat, lon, expected):
assert encode(lat, lon, precision=5) == expected
Fixtures:
@pytest.fixture
def sample_data():
return [(37.7749, -122.4194), (40.7128, -74.0060)]
def test_batch(sample_data):
results = batch_encode(sample_data)
assert len(results) == 2
Mocking:
def test_api_call(mocker):
mocker.patch("my_lib.client.fetch", return_value={"data": []})
result = my_lib.get_data()
assert result == []
Exception testing:
def test_invalid_raises():
with pytest.raises(ValueError, match="latitude"):
encode(91.0, 0.0)
For detailed patterns, see:
| Principle | Meaning |
|---|---|
| Independent | No shared state between tests |
| Deterministic | Same result every run |
| Fast | Unit tests < 100ms each |
| Focused | Test behavior, not implementation |
Testing:
- [ ] Tests exist for public API
- [ ] Edge cases covered (empty, boundary, error)
- [ ] No external service dependencies (mock them)
- [ ] Coverage > 85%
- [ ] Tests run in CI
This skill is based on the Code Quality section of the Guide to Developing High-Quality Python Libraries by Will McGinnis. See these posts for deeper coverage:
npx claudepluginhub wdm0006/python-skills --plugin python-library-completeImplements strict pytest configurations for Python projects, covering fixtures, parametrize, coverage thresholds, async tests with pytest-asyncio, Hypothesis property testing, nox/tox, CI matrices, snapshot testing with syrupy, mocking, and test organization mirroring source code.
Implements Python testing strategies with pytest, fixtures, mocking, and test-driven development. Use when writing Python tests or setting up test suites.
Guides Python testing with pytest: TDD cycle, fixture patterns, mocking, parametrization, and 80%+ coverage targets. Activates when writing Python tests or setting up coverage infrastructure.