skillfed

Pytest Mocking Strategy

Learn a decision-driven framework for mocking in pytest that separates external boundaries from business logic. This skill covers autospec enforcement, async mock patterns, reusable mock factories, and a clear matrix for what to mock versus what to test directly. Master the golden rule—mock external dependencies, test the unit in isolation—to write tests that survive refactoring.

Pytest Mocking Strategy teaches when and how to mock dependencies in unit tests, covering autospec, AsyncMock, and what should never be mocked.

AI-generated summary based on this skill's SKILL.md

1 0 unlicensed — metadata only updated by dawiddutoit

Install

dawiddutoit/custom-claude/pytest-mocking-strategy · repository language: Python

git clone https://github.com/dawiddutoit/custom-claude
cp -r custom-claude ~/.claude/skills/pytest-mocking-strategy

generated, unverified - the skill's exact subdirectory could not be determined; check the repository on GitHub

npx skillfed install dawiddutoit/custom-claude/pytest-mocking-strategy

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

What mocking strategies should I use in pytest?

Pytest Mocking Strategy teaches a decision-driven framework that separates external boundaries from business logic. The core approach: mock external dependencies, test the unit in isolation. Key strategies include using autospec enforcement to catch interface changes, async mock patterns for concurrent code, and reusable mock factories to reduce duplication. This golden rule ensures your tests survive refactoring by validating behavior without coupling to implementation details.

How do I mock in pytest effectively?

Pytest Mocking Strategy covers best practices for mocking that center on isolating dependencies during testing. Use autospec to enforce mock contracts, apply async mock patterns when testing concurrent operations, and build reusable mock factories for consistency. The skill emphasizes a clear matrix for what to mock versus what to test directly—mock external boundaries (APIs, databases, file systems) while testing your business logic in isolation.

What are pytest mock best practices?

Pytest Mocking Strategy outlines best practices including autospec enforcement to prevent interface drift, async mock patterns for modern async code, and mock fixtures for reusable test doubles. The framework prioritizes mocking external dependencies while testing core logic directly. This approach ensures tests remain maintainable, catch real integration issues, and don't break when internal implementation changes.

How do I implement effective test doubles and mock objects?

Pytest Mocking Strategy teaches you to implement test doubles through reusable mock factories and a structured decision matrix. Use autospec to create mocks that enforce the real interface, apply async mock patterns for concurrent scenarios, and organize mock setup in fixtures. This skill ensures your mock objects accurately represent dependencies without becoming brittle or overly complex.

How do I isolate dependencies during pytest testing?

Pytest Mocking Strategy provides patterns for isolating dependencies by mocking external boundaries—APIs, databases, file systems—while keeping your unit logic under test. Use mock fixtures for reusable test doubles, autospec to enforce contracts, and async mock patterns for concurrent code. This isolation ensures your tests validate business logic independently and catch real integration issues separately.

What is the difference between pytest monkeypatch and mock?

Pytest Mocking Strategy covers both tools within its framework. Monkeypatch is lighter-weight and ideal for simple replacements in the same process; mock (via unittest.mock) is more powerful for creating sophisticated test doubles with call tracking and autospec enforcement. The skill teaches when to use each: monkeypatch for quick local patches, mock for complex external dependency isolation and contract validation.

Related skills

Tags

test-doubles unit-testing mock-patterns test-fixtures dependency-isolation pytest-plugins testing-methodology code-quality