Claude Code for pytest: Testing Framework in Python — Claude Skills 360 Blog
Blog / AI / Claude Code for pytest: Testing Framework in Python
AI

Claude Code for pytest: Testing Framework in Python

Published: June 22, 2028
Read time: 5 min read
By: Claude Skills 360

pytest is the standard Python testing framework. pip install pytest. Test function: def test_add(): assert add(2, 3) == 5. Run: pytest (auto-discovers test_*.py). Verbose: pytest -v. Run one: pytest tests/test_math.py::test_add. Fixture: @pytest.fixture def db(): conn = connect(); yield conn; conn.close(). Scope: @pytest.fixture(scope="module") — session/module/class/function. conftest.py: shared fixtures loaded automatically. parametrize: @pytest.mark.parametrize("a,b,expected", [(1,2,3),(0,0,0)]); def test_add(a,b,expected): assert add(a,b)==expected. mark: @pytest.mark.slow; @pytest.mark.skip(reason="..."); @pytest.mark.xfail. raises: with pytest.raises(ValueError, match="negative"): sqrt(-1). approx: assert 0.1+0.2 == pytest.approx(0.3). monkeypatch: def test_env(monkeypatch): monkeypatch.setenv("KEY","val"); monkeypatch.setattr(module, "fn", mock_fn). tmp_path: def test_file(tmp_path): f = tmp_path/"out.txt"; f.write_text("hi"); assert f.exists(). capfd: def test_print(capfd): print("hi"); out,_ = capfd.readouterr(); assert "hi" in out. capsys: text capture. coverage: pytest --cov=src --cov-report=term-missing. xdist: pytest -n auto parallel. pyproject.toml: [tool.pytest.ini_options] testpaths=["tests"] markers=["slow: ..."]. Claude Code generates pytest test suites, fixtures, parametrize tables, and coverage reports.

CLAUDE.md for pytest

## pytest Stack
- Version: pytest >= 8.0 | pip install pytest pytest-cov
- Test: def test_name(): assert expr | with pytest.raises(ExcType, match="..."): ...
- Fixture: @pytest.fixture[scope="..."] def name(dep): ... yield value ... (teardown)
- Param: @pytest.mark.parametrize("a,b", [(1,2),(3,4)]) def test(a,b): ...
- Mock: monkeypatch.setattr/setenv/setitem | pytest-mock: mocker.patch("mod.fn")
- Run: pytest -v -x --tb=short --cov=src --cov-report=term-missing

pytest Test Pipeline

# tests/ — pytest fixtures, parametrize, marks, monkeypatch, tmp_path, custom helpers
# This file shows patterns; typically split across test files and conftest.py
from __future__ import annotations

import json
import os
import time
from dataclasses import dataclass
from pathlib import Path
from typing import Any

import pytest


# ─────────────────────────────────────────────────────────────────────────────
# 1. System under test (example module — normally in src/)
# ─────────────────────────────────────────────────────────────────────────────

class Calculator:
    """Simple calculator for demonstration."""

    def add(self, a: float, b: float) -> float:
        return a + b

    def divide(self, a: float, b: float) -> float:
        if b == 0:
            raise ZeroDivisionError("Cannot divide by zero")
        return a / b

    def sqrt(self, x: float) -> float:
        if x < 0:
            raise ValueError(f"Cannot take sqrt of negative number: {x}")
        return x ** 0.5


@dataclass
class User:
    id:    int
    name:  str
    email: str
    active: bool = True


class UserService:
    def __init__(self, db: dict | None = None) -> None:
        self._db: dict[int, User] = db or {}

    def create(self, name: str, email: str) -> User:
        if not email or "@" not in email:
            raise ValueError(f"Invalid email: {email!r}")
        uid  = max(self._db, default=0) + 1
        user = User(id=uid, name=name, email=email)
        self._db[uid] = user
        return user

    def get(self, uid: int) -> User | None:
        return self._db.get(uid)

    def deactivate(self, uid: int) -> bool:
        user = self._db.get(uid)
        if user is None:
            return False
        user.active = False
        return True

    def list_active(self) -> list[User]:
        return [u for u in self._db.values() if u.active]


def load_config(path: str) -> dict:
    """Load JSON config from file."""
    return json.loads(Path(path).read_text())


# ─────────────────────────────────────────────────────────────────────────────
# 2. conftest.py patterns (would normally be in tests/conftest.py)
# ─────────────────────────────────────────────────────────────────────────────

# --- Function-scope fixture (default) ---
@pytest.fixture
def calc() -> Calculator:
    """Fresh Calculator for each test."""
    return Calculator()


# --- Module-scope fixture: setup once per test file ---
@pytest.fixture(scope="module")
def user_service_module() -> UserService:
    """Shared UserService instance for the module (prepopulated)."""
    svc = UserService()
    svc.create("Alice", "[email protected]")
    svc.create("Bob",   "[email protected]")
    return svc


# --- Session-scope fixture: setup once per pytest session ---
@pytest.fixture(scope="session")
def shared_data_dir(tmp_path_factory) -> Path:
    """Create a temp directory shared across all tests."""
    d = tmp_path_factory.mktemp("shared_data")
    (d / "sample.json").write_text('{"key": "value", "count": 42}')
    return d


# --- Fixture with teardown via yield ---
@pytest.fixture
def tmp_config_file(tmp_path) -> Path:
    """Write a temp JSON config and clean up after the test."""
    cfg_path = tmp_path / "config.json"
    cfg_path.write_text(json.dumps({"debug": True, "port": 8080}))
    yield cfg_path
    # Teardown: nothing needed here since tmp_path is auto-cleaned

# --- Fixture with factory pattern ---
@pytest.fixture
def make_user():
    """Factory fixture: returns a function to create users."""
    created = []
    def _make(name: str = "Test", email: str = "[email protected]") -> User:
        user = User(id=len(created)+1, name=name, email=email)
        created.append(user)
        return user
    return _make


# ─────────────────────────────────────────────────────────────────────────────
# 3. Basic tests
# ─────────────────────────────────────────────────────────────────────────────

class TestCalculator:
    def test_add_integers(self, calc):
        assert calc.add(2, 3) == 5

    def test_add_floats(self, calc):
        assert calc.add(0.1, 0.2) == pytest.approx(0.3)

    def test_divide(self, calc):
        assert calc.divide(10, 2) == 5.0

    def test_divide_by_zero(self, calc):
        with pytest.raises(ZeroDivisionError, match="Cannot divide by zero"):
            calc.divide(1, 0)

    def test_sqrt_positive(self, calc):
        assert calc.sqrt(9) == pytest.approx(3.0)

    def test_sqrt_negative(self, calc):
        with pytest.raises(ValueError, match="negative number"):
            calc.sqrt(-4)

    def test_sqrt_zero(self, calc):
        assert calc.sqrt(0) == 0.0


# ─────────────────────────────────────────────────────────────────────────────
# 4. Parametrize
# ─────────────────────────────────────────────────────────────────────────────

@pytest.mark.parametrize("a, b, expected", [
    (2,   3,   5),
    (0,   0,   0),
    (-1,  1,   0),
    (100, 200, 300),
    (0.5, 0.5, 1.0),
])
def test_add_parametrized(calc, a, b, expected):
    assert calc.add(a, b) == pytest.approx(expected)


@pytest.mark.parametrize("email, valid", [
    ("[email protected]", True),
    ("[email protected]",   True),
    ("not-an-email",      False),
    ("",                  False),
    ("missing-at.com",    False),
])
def test_email_validation(email, valid):
    svc = UserService()
    if valid:
        user = svc.create("Test", email)
        assert user.email == email
    else:
        with pytest.raises(ValueError):
            svc.create("Test", email)


# ─────────────────────────────────────────────────────────────────────────────
# 5. Fixtures in action
# ─────────────────────────────────────────────────────────────────────────────

class TestUserService:
    @pytest.fixture(autouse=True)
    def svc(self) -> UserService:
        """Fresh service per test method."""
        self.svc = UserService()

    def test_create_user(self):
        user = self.svc.create("Alice", "[email protected]")
        assert user.id == 1
        assert user.name == "Alice"
        assert user.active is True

    def test_get_existing_user(self):
        created = self.svc.create("Bob", "[email protected]")
        found   = self.svc.get(created.id)
        assert found == created

    def test_get_missing_user(self):
        assert self.svc.get(9999) is None

    def test_deactivate(self):
        user = self.svc.create("Carol", "[email protected]")
        assert self.svc.deactivate(user.id) is True
        assert self.svc.get(user.id).active is False

    def test_list_active_filters_inactive(self):
        u1 = self.svc.create("A", "[email protected]")
        u2 = self.svc.create("B", "[email protected]")
        self.svc.deactivate(u1.id)
        active = self.svc.list_active()
        assert len(active) == 1
        assert active[0].id == u2.id


# ─────────────────────────────────────────────────────────────────────────────
# 6. monkeypatch patterns
# ─────────────────────────────────────────────────────────────────────────────

def get_greeting(name: str) -> str:
    """Function that varies by environment variable."""
    prefix = os.getenv("GREETING_PREFIX", "Hello")
    return f"{prefix}, {name}!"


def fetch_current_time() -> float:
    """Returns current timestamp."""
    return time.time()


def test_monkeypatch_env(monkeypatch):
    """Patch an environment variable."""
    monkeypatch.setenv("GREETING_PREFIX", "Hi")
    assert get_greeting("Alice") == "Hi, Alice!"


def test_monkeypatch_default_env(monkeypatch):
    """Ensure default value when env var absent."""
    monkeypatch.delenv("GREETING_PREFIX", raising=False)
    assert get_greeting("Bob").startswith("Hello")


def test_monkeypatch_function(monkeypatch):
    """Patch a function on a module."""
    fixed_time = 1_700_000_000.0
    monkeypatch.setattr("time.time", lambda: fixed_time)
    assert fetch_current_time() == fixed_time


# ─────────────────────────────────────────────────────────────────────────────
# 7. File I/O and tmp_path
# ─────────────────────────────────────────────────────────────────────────────

def test_load_config(tmp_config_file):
    """Test loading a real config file via tmp_path fixture."""
    cfg = load_config(str(tmp_config_file))
    assert cfg["debug"] is True
    assert cfg["port"] == 8080


def test_write_and_read(tmp_path):
    """tmp_path is a pytest-managed temp directory."""
    output = tmp_path / "results.json"
    data   = {"score": 0.95, "label": "positive"}
    output.write_text(json.dumps(data))

    loaded = json.loads(output.read_text())
    assert loaded["score"] == pytest.approx(0.95)
    assert loaded["label"] == "positive"


def test_shared_data_dir(shared_data_dir):
    """Read from session-scope temp fixture."""
    data = json.loads((shared_data_dir / "sample.json").read_text())
    assert data["count"] == 42


# ─────────────────────────────────────────────────────────────────────────────
# 8. Marks
# ─────────────────────────────────────────────────────────────────────────────

@pytest.mark.slow
def test_slow_operation():
    """Marked slow — skip with: pytest -m 'not slow'."""
    time.sleep(0.01)  # simulated slow work
    assert True


@pytest.mark.skip(reason="Feature not yet implemented")
def test_future_feature():
    assert False, "should be skipped"


@pytest.mark.xfail(reason="Known bug in edge case", strict=False)
def test_known_bug():
    calc = Calculator()
    # This would fail in a real scenario; xfail marks it as expected failure
    assert calc.divide(0, 0) == 0  # ZeroDivisionError — expected


# ─────────────────────────────────────────────────────────────────────────────
# 9. Output capture
# ─────────────────────────────────────────────────────────────────────────────

def test_stdout_capture(capsys):
    print("Hello, pytest!")
    captured = capsys.readouterr()
    assert "Hello, pytest!" in captured.out


def test_fd_capture(capfd):
    os.write(1, b"raw bytes\n")
    out, _ = capfd.readouterr()
    assert b"raw bytes" in out.encode() or "raw bytes" in out


# ─────────────────────────────────────────────────────────────────────────────
# Example pyproject.toml configuration (not executed — for reference)
# ─────────────────────────────────────────────────────────────────────────────

PYPROJECT_PYTEST_CONFIG = """
[tool.pytest.ini_options]
testpaths     = ["tests"]
addopts       = "-v --tb=short"
markers = [
    "slow: marks tests as slow (deselect with -m 'not slow')",
    "integration: requires external services",
    "unit: pure unit tests",
]

[tool.coverage.run]
source = ["src"]
omit   = ["*/tests/*", "*/conftest.py"]

[tool.coverage.report]
show_missing = true
fail_under   = 80
"""

For the unittest stdlib alternative — Python’s built-in unittest module provides a JUnit-style test framework with TestCase classes, setUp/tearDown methods, and a unittest.mock library; pytest can discover and run both unittest.TestCase and plain function-based tests, and adds fixtures, parametrize, concise assertion output, and a richer plugin ecosystem — use unittest when you need to run in environments with no third-party dependencies or are extending existing TestCase-based suites, pytest for all new test code where fixture reuse and parametrize readability matter. For the hypothesis alternative — Hypothesis is a property-based testing library that generates random inputs to find edge cases — instead of writing @pytest.mark.parametrize tables by hand, you declare properties (@given(st.integers())) and Hypothesis searches for inputs that violate them; pytest handles specific example-based tests — use hypothesis when testing algorithms, parsers, or serializers where you want to discover unexpected edge cases, pytest.parametrize when you have known concrete examples and want deterministic test runs. The Claude Skills 360 bundle includes pytest skill sets covering fixture patterns (function/module/session scope, yield teardown, factory fixtures, autouse), parametrize tables with edge cases, monkeypatch.setattr/setenv patterns, tmp_path file I/O tests, capsys/capfd output capture, @pytest.mark.slow/skip/xfail, and pyproject.toml configuration. Start with the free tier to try automated testing and pytest suite code generation.

Keep Reading

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
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 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