agentleFS
ludo-technologiesludo-technologies/python-best-practices/skills/testing/SKILL.md

testing

Skill27 starsChanged 5 months agoMITView on GitHub

Python test-writing best practices covering test structure, fixtures, parametrization, and mocking with pytest. Use when writing, reviewing, or refactoring tests, adding test coverage, or working in tests directories and conftest.py.

  • Reads credentials

123 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use oss-pr, pr-automation and 10 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.
  • CES-LtdSkill

    oss-pr

    Use when creating a pull request, after committing changes, or when user invokes /oss-pr. Covers branch management, quality checks, commit, push, and PR creation.

    Also in 3 projects that use this

  • CES-LtdSkill

    pr-automation

    PR Automation Orchestrator: poll open PRs, check CI, run review, fix, and merge eligible PRs. Use when: (1) Invoked by daemon via scripts/pr-automation.sh, (2) User says "/pr-automation".

    Also in 3 projects that use this

  • ffsshhttiikkSkill

    cicd

    Continuous integration and delivery pipelines

    Also in 4 projects that use this

  • ffsshhttiikkSkill

    networking

    Practical application and coding skills for Networking

    Also in 3 projects that use this

  • JohnJBorenSkill

    ci-cd

    CI/CD pipeline design, GitHub Actions, testing automation, and deployment strategies

    Also in 9 projects that use this

  • Cogni-AI-OUSkill

    dotfiles

    Reference for repository dotfiles, including their purposes, standard configurations, and usage guidelines. You MUST load this skill when configuring, troubleshooting, or understanding repository dotfiles.

    Also in 3 projects that use this

  • +2 more from CES-Ltd
  • +1 more from ffsshhttiikk

The file

Files
SKILL.md

Python Testing

A collection of test-writing best practices for pytest. Designed for AI agents and LLMs to write readable, maintainable, and trustworthy tests.

Categories

Mocking [CRITICAL]

Mock external boundaries only, and keep mocks faithful to real interfaces.

RuleDescription
mock-boundariesMock external boundaries, not internal logic
mock-autospecUse autospec to keep mocks faithful to real signatures
mock-monkeypatchUse monkeypatch for env vars and attributes

Test Structure [HIGH]

Structure tests so failures are easy to diagnose.

RuleDescription
struct-aaa-patternStructure tests as Arrange-Act-Assert
struct-test-namingName tests after the behavior they verify
struct-one-behaviorTest one behavior per test
struct-no-logicKeep conditionals and loops out of tests

Fixtures [HIGH]

Share setup without hiding it.

RuleDescription
fixture-scopeUse the narrowest fixture scope that works
fixture-conftestPlace fixtures in the nearest conftest.py
fixture-factoryUse factory fixtures for varied test data

Parametrization [MEDIUM]

Eliminate duplicated tests without hiding cases.

RuleDescription
param-parametrizeReplace duplicated tests with parametrize
param-idsGive parametrized cases readable ids

Quick Reference

Structure Patterns

def test_expired_token_is_rejected() -> None:
    # Arrange
    token = make_token(expires_at=YESTERDAY)

    # Act
    result = validate(token)

    # Assert
    assert result.valid is False

Fixture Patterns

# Narrow scope by default
@pytest.fixture
def user() -> User:
    return User(name="alice", email="alice@example.com")

# Factory fixture for varied data
@pytest.fixture
def make_user() -> Callable[..., User]:
    def _make(name: str = "alice", active: bool = True) -> User:
        return User(name=name, email=f"{name}@example.com", active=active)
    return _make

Parametrize Patterns

@pytest.mark.parametrize(
    ("email", "valid"),
    [
        pytest.param("a@example.com", True, id="simple-address"),
        pytest.param("a@sub.example.com", True, id="subdomain"),
        pytest.param("no-at-sign", False, id="missing-at"),
        pytest.param("", False, id="empty-string"),
    ],
)
def test_email_validation(email: str, valid: bool) -> None:
    assert is_valid_email(email) is valid

Mocking Patterns

# Mock the external boundary with a faithful interface.
# `mocker` requires the pytest-mock plugin; with stdlib only,
# use unittest.mock.patch as a decorator or context manager.
def test_notifies_on_signup(mocker: MockerFixture) -> None:
    send = mocker.patch("app.signup.email_client.send", autospec=True)

    signup(email="alice@example.com")

    send.assert_called_once_with(to="alice@example.com", template="welcome")

# monkeypatch for environment variables
def test_reads_api_key(monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setenv("API_KEY", "test-key")
    assert load_config().api_key == "test-key"

See Also

  • tooling / test-pytest - pytest configuration, fixtures setup, and coverage

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.