py-testinglisted
Install: claude install-skill AllThingsSmitty/agent-skills
# Python Testing
pytest is the standard. Its fixture system is more composable than unittest's setUp/tearDown, its output is more readable, and parametrize eliminates test duplication cleanly. Use it.
## Fixtures
Fixtures are pytest's dependency injection system. A test declares what it needs; pytest provides it.
```python
# conftest.py — fixtures available to all tests in the directory
import pytest
from myapp.db import Database
@pytest.fixture
def db():
database = Database(':memory:')
database.migrate()
yield database # yield instead of return for teardown
database.close() # runs after the test completes
# test_users.py
def test_create_user(db): # pytest injects the db fixture
user = db.create_user('Alice')
assert user.name == 'Alice'
```
**Scope**: fixtures run once per test by default. Use `scope='module'` or `scope='session'` for expensive resources (database connections, containers) that are safe to share:
```python
@pytest.fixture(scope='session')
def db_connection():
conn = create_connection()
yield conn
conn.close()
```
**conftest.py** — put shared fixtures here, not in individual test files. pytest discovers conftest.py automatically at every directory level.
## Parametrize
Replace repeated test functions with `@pytest.mark.parametrize`:
```python
# Bad: three functions testing the same logic with different inputs
def test_valid_email_with_plus(): ...
def test_valid_email_simple(): ...
def test_invali