agentleFS
peopleforresterpeopleforrester/claude-dotfiles/skills/development/python-testing/SKILL.md

python-testing

Skill3 starsChanged 8 months agoMITView on GitHub

Comprehensive Python testing patterns using pytest. Covers fixtures, parametrize, async testing, mocking, property-based testing, and coverage analysis.

33 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use swift-protocol-di-testing, swift-actor-persistence and 5 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • InfopibeSkill

    swift-protocol-di-testing

    Protocol-based dependency injection for testable Swift code — mock file system, network, and external APIs using focused protocols and Swift Testing.

    Also in 6 projects that use this

  • InfopibeSkill

    swift-actor-persistence

    Thread-safe data persistence in Swift using actors — in-memory cache with file-backed storage, eliminating data races by design.

    Also in 6 projects that use this

  • joneqianSkill

    postgres-patterns

    PostgreSQL database patterns for query optimization, schema design, indexing, and security. Based on Supabase best practices.

    Also in 12 projects that use this

  • saxon-ySkill

    configure-ecc

    Interactive installer for Everything Claude Code — guides users through selecting and installing skills and rules to user-level or project-level directories, verifies paths, and optionally optimizes installed files.

    Also in 5 projects that use this

  • +3 more from Infopibe

The file

Files
SKILL.md

Python Testing Patterns

Framework: pytest

Project Setup

# pyproject.toml
[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = ["test_*.py"]
python_functions = ["test_*"]
addopts = "-v --tb=short --strict-markers"
markers = [
    "slow: marks tests as slow",
    "integration: marks integration tests",
]

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

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

Fixtures

Basic Fixtures

@pytest.fixture
def sample_user() -> User:
    return User(name="Test User", email="test@example.com")

@pytest.fixture
def db_session():
    session = create_test_session()
    yield session
    session.rollback()
    session.close()

Factory Fixtures

@pytest.fixture
def make_user():
    def _make_user(name: str = "Test", email: str | None = None) -> User:
        return User(
            name=name,
            email=email or f"{name.lower()}@example.com",
        )
    return _make_user

Shared Fixtures (conftest.py)

# tests/conftest.py
@pytest.fixture(scope="session")
def app():
    """Create application for testing."""
    app = create_app(testing=True)
    yield app

@pytest.fixture
def client(app):
    """Create test client."""
    return app.test_client()

Parametrize

@pytest.mark.parametrize("input_val, expected", [
    ("hello@example.com", True),
    ("invalid-email", False),
    ("", False),
    ("user@.com", False),
    ("user@domain.co.uk", True),
])
def test_validate_email(input_val: str, expected: bool) -> None:
    assert validate_email(input_val) == expected

Async Testing

import pytest_asyncio

@pytest_asyncio.fixture
async def async_client():
    async with AsyncClient(app=app, base_url="http://test") as client:
        yield client

@pytest.mark.asyncio
async def test_fetch_user(async_client: AsyncClient) -> None:
    response = await async_client.get("/api/users/1")
    assert response.status_code == 200
    assert response.json()["name"] == "Test User"

Mocking

from unittest.mock import AsyncMock, patch

def test_send_email(mocker):
    mock_send = mocker.patch("myapp.email.send_email")
    mock_send.return_value = True

    result = notify_user("user@example.com", "Hello")

    assert result is True
    mock_send.assert_called_once_with("user@example.com", "Hello")

Property-Based Testing

from hypothesis import given, strategies as st

@given(st.lists(st.integers()))
def test_sort_is_idempotent(xs: list[int]) -> None:
    assert sorted(sorted(xs)) == sorted(xs)

@given(st.text(min_size=1))
def test_uppercase_preserves_length(s: str) -> None:
    assert len(s.upper()) == len(s)

Exception Testing

def test_invalid_input_raises() -> None:
    with pytest.raises(ValueError, match="must be positive"):
        calculate(-1)

def test_file_not_found_error() -> None:
    with pytest.raises(FileNotFoundError):
        load_config(Path("/nonexistent/path"))

Test Organization

tests/
  conftest.py              # Shared fixtures
  unit/
    test_models.py         # Model unit tests
    test_services.py       # Service unit tests
    test_utils.py          # Utility unit tests
  integration/
    test_api.py            # API integration tests
    test_database.py       # Database integration tests
  e2e/
    test_workflows.py      # End-to-end workflows

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.