mcpField
P
python-testing-patterns

python-testing-patterns

github.com/jurgendnTesting & quality1138 installsUpdated 6w ago
Repository

About python-testing-patterns

A Python testing-patterns skill for developers writing focused tests that make behavior safer to change. It covers test structure, boundary isolation, fixtures, mocks, and a balanced test-suite shape.

Key features

  1. 01Uses focused Arrange/Act/Assert tests
  2. 02Covers boundaries and error cases
  3. 03Guides fixtures and mocking
  4. 04Outlines a test-suite pyramid
  5. 05Provides a focused-test checklist

Use cases

  1. 01Writing tests for a specific behavior
  2. 02Testing error paths and input variations
  3. 03Isolating network or database effects
  4. 04Planning unit, integration, and end-to-end coverage

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/jurgendn/agent-skills --skill python-testing-patterns

After installing, type/python-testing-patternsin Claude Code to confirm it loaded.

Bundled filesreferences/advanced-patterns.md

Details

From the project README

Python Testing Patterns

Write tests that make behavior safer to change. Prefer focused, readable tests over clever fixtures or coverage theater.

For advanced patterns such as async tests, monkeypatching, property-based tests, database testing, CI, and coverage tooling, read references/advanced-patterns.md.

Core Principles

  • One behavior per test. A failure should point to one broken behavior.
  • Arrange / Act / Assert. Keep setup, execution, and verification distinct.
  • Test boundaries and errors. Happy-path tests alone are weak.
  • Isolate external effects. Mock network, time, randomness, and services when they are not the behavior under test.
  • Use meaningful coverage. Cover important behavior and risks, not every line.

Basic Pytest Pattern

def test_adds_two_numbers():
    result = add(2, 3)
    assert result == 5

For exceptions:

import pytest

def test_rejects_zero_division():
    with pytest.raises(ValueError, match="zero"):
        divide(1, 0)

For parametrized cases:

import pytest

@pytest.mark.parametrize("value, expected", [
    ("[email protected]", True),
    ("not-an-email", False),
])
def test_email_validation(value, expected):
    assert is_valid_email(value) is expected

Fixtures

Use fixtures for reusable setup with clear lifetime:

@pytest.fixture
def user():
    return User(id=1, email="[email protected]")

Avoid hidden fixture webs. If a test needs many fixtures, the unit may be too coupled or the test may be too broad.

Mocking

Mock at system boundaries:

  • HTTP clients;
  • filesystem or object storage;
  • database sessions when not testing persistence;
  • time and randomness;
  • expensive model/API calls.

Prefer fakes over mocks when behavior is complex enough that call assertions become brittle.

Test Suite Shape

Use a small pyramid:

  • many unit tests for pure logic;
  • fewer integration tests for boundaries;
  • a few end-to-end tests for critical workflows.

Name tests by behavior:

test_<unit>_<scenario>_<expected_behavior>

Output

When advising or writing tests, return:

  • target behavior;
  • test levels needed;
  • fixtures/mocks;
  • concrete test cases;
  • missing edge/error paths;
  • command to run the focused tests.

Comments

Markdown supported