Claude Code for Python Testing: pytest, Fixtures, Mocking, and Coverage — Claude Skills 360 Blog
Blog / Python / Claude Code for Python Testing: pytest, Fixtures, Mocking, and Coverage
Python

Claude Code for Python Testing: pytest, Fixtures, Mocking, and Coverage

Published: November 30, 2026
Read time: 9 min read
By: Claude Skills 360

Python’s pytest is the standard test framework — its fixture system, parametrize decorator, and plugin ecosystem make it far more powerful than unittest. unittest.mock provides patching for isolating dependencies. Hypothesis generates property-based tests that find edge cases you wouldn’t think to write. Claude Code generates pytest suites, fixture hierarchies, async test patterns, property-based tests, and the CI configuration for coverage enforcement.

CLAUDE.md for Python Testing

## Testing Stack
- Framework: pytest 8.x with pytest-asyncio for async tests
- Async runtime: anyio (works with both asyncio and trio)
- Mocking: unittest.mock (built-in) + pytest-mock for fixtures
- Factory: factory_boy for test data generation
- Property-based: Hypothesis for parsers, validators, data transforms
- Coverage: pytest-cov, minimum 85% enforced in CI
- Database: pytest-postgresql or testcontainers[postgres] for integration tests
- HTTP mocking: responses (for requests) or httpx-mock (for httpx)
- Mutation testing: mutmut — run weekly in CI

Fixtures and Parametrize

# tests/conftest.py — shared fixtures
import pytest
import pytest_asyncio
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from httpx import AsyncClient, ASGITransport

from myapp.main import app
from myapp.database import Base, get_db
from myapp.models import User, Order

# Database fixture: test database, auto-rollback per test
@pytest_asyncio.fixture(scope="function")
async def db():
    engine = create_async_engine("sqlite+aiosqlite:///:memory:")
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)
    
    async_session = async_sessionmaker(engine, expire_on_commit=False)
    
    async with async_session() as session:
        yield session
        await session.rollback()
    
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.drop_all)

# Override FastAPI dependency with test database
@pytest_asyncio.fixture
async def client(db: AsyncSession):
    app.dependency_overrides[get_db] = lambda: db
    async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
        yield ac
    app.dependency_overrides.clear()

# Factory fixtures using factory_boy
import factory

class UserFactory(factory.Factory):
    class Meta:
        model = User
    
    id = factory.Sequence(lambda n: f"user_{n:04d}")
    email = factory.LazyAttribute(lambda obj: f"{obj.id}@example.com")
    name = factory.Faker('name')
    is_active = True

class OrderFactory(factory.Factory):
    class Meta:
        model = Order
    
    id = factory.Sequence(lambda n: f"ord_{n:04d}")
    user = factory.SubFactory(UserFactory)
    status = "pending"
    total_cents = factory.Faker('random_int', min=100, max=100000)

@pytest.fixture
def user():
    return UserFactory()

@pytest.fixture
def order(user):
    return OrderFactory(user=user)
# tests/test_order_service.py
import pytest
from decimal import Decimal

# Parametrize: run test with multiple inputs
@pytest.mark.parametrize("total_cents,expected_discount", [
    (0, 0),
    (4999, 0),        # Under $50 — no discount
    (5000, 250),      # $50 — 5% discount
    (10000, 1000),    # $100 — 10% discount
    (50000, 7500),    # $500 — 15% discount
    (100000, 20000),  # $1000 — 20% discount (max)
    (200000, 20000),  # $2000 — capped at 20%
])
def test_calculate_discount(total_cents: int, expected_discount: int):
    result = calculate_discount(total_cents)
    assert result == expected_discount

# Parametrize with indirect fixture
@pytest.mark.parametrize("status", ["shipped", "delivered"])
def test_order_cannot_be_cancelled_when(status, order):
    order.status = status
    with pytest.raises(ValueError, match="Cannot cancel"):
        cancel_order(order)

@pytest.mark.parametrize("invalid_email", [
    "not-an-email",
    "@nodomain.com",
    "spaces [email protected]",
    "",
    None,
])
def test_invalid_email_raises(invalid_email):
    with pytest.raises((ValueError, TypeError)):
        validate_email(invalid_email)

Mocking with pytest-mock

# tests/test_email_service.py
import pytest
from unittest.mock import AsyncMock, patch, call

async def test_sends_order_confirmation(mocker, order):
    """Test that order confirmation email is sent with correct data."""
    mock_send = mocker.patch('myapp.services.email.send_email', new_callable=AsyncMock)
    
    await send_order_confirmation(order)
    
    # Assert called once with correct arguments
    mock_send.assert_called_once_with(
        to=order.user.email,
        subject=f"Order #{order.id} Confirmed",
        template="order_confirmation",
        context={"order_id": order.id, "total": order.total_cents / 100},
    )

async def test_does_not_send_email_for_cancelled_orders(mocker, order):
    order.status = "cancelled"
    mock_send = mocker.patch('myapp.services.email.send_email', new_callable=AsyncMock)
    
    await send_order_confirmation(order)
    
    mock_send.assert_not_called()

async def test_retries_on_smtp_error(mocker, order):
    """Test retry logic when SMTP fails."""
    call_count = 0
    
    async def flaky_send(*args, **kwargs):
        nonlocal call_count
        call_count += 1
        if call_count < 3:
            raise SMTPError("Connection refused")
    
    mocker.patch('myapp.services.email.send_email', side_effect=flaky_send)
    
    await send_order_confirmation(order)  # Should succeed on 3rd attempt
    assert call_count == 3

# Patching builtins and external services
async def test_stripe_charge_on_order_create(mocker, db, client):
    mock_stripe = mocker.patch('stripe.PaymentIntent.create')
    mock_stripe.return_value = {'id': 'pi_test', 'status': 'succeeded'}
    
    response = await client.post("/orders", json={
        "user_id": "user_0001",
        "items": [{"product_id": "prod_abc", "quantity": 1}],
    })
    
    assert response.status_code == 201
    mock_stripe.assert_called_once()
    assert mock_stripe.call_args.kwargs['amount'] == response.json()['total_cents']

Async Tests with anyio

# tests/test_async_service.py
import pytest
import anyio

@pytest.mark.anyio
async def test_concurrent_order_processing():
    """Test that concurrent orders don't interfere with each other."""
    results = []
    
    async def process(order_id: str):
        result = await process_order(order_id)
        results.append(result)
    
    async with anyio.create_task_group() as tg:
        for i in range(10):
            tg.start_soon(process, f"ord_{i:04d}")
    
    assert len(results) == 10
    assert all(r.status == "processed" for r in results)

# pytest-anyio config in pyproject.toml
# [tool.pytest.ini_options]
# anyio_mode = "auto"  # All async tests run with anyio

Property-Based Testing with Hypothesis

# tests/test_order_validation.py
from hypothesis import given, strategies as st, settings, assume

@given(
    amount=st.integers(min_value=1, max_value=10_000_000),
    currency=st.sampled_from(["USD", "EUR", "GBP", "JPY", "CAD"]),
)
def test_format_currency_never_raises(amount: int, currency: str):
    """format_currency must not raise for any valid amount and currency."""
    result = format_currency(amount, currency)
    assert isinstance(result, str)
    assert len(result) > 0

@given(
    email=st.emails(),
)
def test_valid_emails_accepted(email: str):
    """All emails generated by hypothesis should parse without error."""
    result = parse_email(email)
    assert result == email.lower()

@given(
    items=st.lists(
        st.fixed_dictionaries({
            'product_id': st.text(min_size=1, max_size=50),
            'quantity': st.integers(min_value=1, max_value=100),
            'price_cents': st.integers(min_value=1, max_value=1_000_000),
        }),
        min_size=1, max_size=50
    )
)
def test_calculate_total_is_sum_of_items(items):
    """Total must equal sum of (qty * price) for all items."""
    expected = sum(i['quantity'] * i['price_cents'] for i in items)
    assert calculate_total(items) == expected

@given(st.text())
@settings(max_examples=500)
def test_order_id_parser_never_panics(s: str):
    """Parser must not raise — return None for invalid input instead of raising."""
    result = parse_order_id(s)
    assert result is None or isinstance(result, str)

Coverage Configuration

# pyproject.toml
[tool.pytest.ini_options]
testpaths = ["tests"]
asyncio_mode = "auto"
addopts = [
    "--cov=myapp",
    "--cov-report=term-missing",
    "--cov-report=html:htmlcov",
    "--cov-fail-under=85",
]

[tool.coverage.run]
branch = true
omit = ["*/migrations/*", "tests/*", "*/conftest.py"]

[tool.coverage.report]
exclude_lines = [
    "pragma: no cover",
    "if TYPE_CHECKING:",
    "raise NotImplementedError",
]

For the FastAPI endpoints that these tests cover, see the FastAPI advanced guide for dependency injection and error handling patterns. For the Pydantic v2 validation models that parametrized tests exercise, the Pydantic v2 guide covers field validators and model validators. The Claude Skills 360 bundle includes Python testing skill sets covering pytest fixtures, Hypothesis property tests, and async test patterns. Start with the free tier to try pytest fixture generation.

Keep Reading

Python

Claude Code for Pydantic v2: Validation, Serialization, and Settings

Use Pydantic v2 with Claude Code — model definitions, field validators, model validators, computed fields, pydantic-settings for env config, JSON Schema generation, serialization modes, and FastAPI integration.

8 min read Nov 22, 2026
AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free