Pytest Mocking Strategy

VerifiedSafe

Master mocking strategy for pytest: autospec, AsyncMock, mock factories, and what to mock vs real objects.

Sby Skills Guide Bot
TestingIntermediate
8/18/2026
Claude CodeCursorCopilotCodex
#pytest#mocking#unit-testing#asyncmock#test-isolation

Recommended for

Automated summary

This skill provides a decision framework for choosing what to mock in pytest tests, using autospec, AsyncMock and reusable mock factories.

Strengths

  • Explicit decision matrix separating external boundaries to mock from domain objects to keep real
  • Mandates autospec/create_autospec to prevent permissive mocks and misspelled method calls
  • Covers AsyncMock patterns, including returning a fake async generator
  • Recommends centralizing mock factories in conftest.py for reuse

Limitations

  • Examples are tightly tied to an e-commerce domain (Shopify, Kafka, ClickHouse) and need adaptation
  • Does not cover pytest configuration or test data factories, which it defers to separate skills
  • The included conftest.py snippet is truncated and must be completed by the user
When to use it

Use it when deciding what to mock in a unit test or when setting up typed, isolated mocked dependencies.

When not to use it

Avoid it for testing domain business logic itself, configuring pytest, or building test datasets.

Security analysis

Safe
Quality score95/100

The skill provides guidance on writing unit tests with mocks in Python, including code examples and best practices. It does not contain any destructive commands, data exfiltration, or obfuscated payloads. The allowed tools (Read, Bash, Write) are standard for code generation and testing, and the content is purely educational.

No concerns found

How this analysis is made

Examples

Autospec fixture for a gateway
Write a pytest fixture that uses create_autospec on my ShopifyGateway with instance=True, makes fetch_orders return two test orders, and then show a test of ExtractOrdersUseCase using that fixture.
Mock or real object?
I'm unit testing my use case. Should I mock the Kafka producer and the ClickHouse repository, or use real objects? Run through the mocking decision matrix and justify each choice.
AsyncMock for async code
My gateway method returns an async generator of orders. Show me the AsyncMock pattern to fake it, including how to assert the awaited calls were made.

name: pytest-mocking-strategy description: | Master mocking strategy for pytest: when to use autospec=True vs manual mocks, AsyncMock patterns for async code, mock builders and factories, and what should NEVER be mocked. Includes type-safe mock creation patterns and decision matrix for mocking vs real objects.

Use when: Creating unit tests with mocked dependencies, deciding whether to mock or use real objects, setting up test isolation, testing use cases with external service boundaries, or building reusable mock factories. allowed-tools: Read, Bash, Write

Pytest Mocking Strategy

Purpose

Mocking is essential for unit testing, but over-mocking creates brittle tests that fail on refactoring. This skill provides a comprehensive framework for deciding what to mock, how to mock it safely, and when to use real objects instead.

When to Use This Skill

Use when deciding what to mock in tests with "create mock", "mock external service", "AsyncMock pattern", or "what should I mock".

Do NOT use for domain testing (never mock domain objects), pytest configuration (use pytest-configuration), or test factories (use pytest-test-data-factories).

Quick Start

The golden rule: Mock external boundaries, test the unit in isolation.

from unittest.mock import AsyncMock, create_autospec
import pytest

# ✅ GOOD: Mock external dependency
@pytest.fixture
def mock_shopify_gateway() -> AsyncMock:
    mock = create_autospec(ShopifyGateway, instance=True)
    mock.fetch_orders.return_value = [create_test_order()]
    return mock

# Test uses mocked dependency
async def test_use_case(mock_shopify_gateway: AsyncMock) -> None:
    use_case = ExtractOrdersUseCase(gateway=mock_shopify_gateway)
    result = await use_case.execute()
    assert result.orders_count == 1

Instructions

Step 1: Decide What "The Unit" Is

For function-based code: A single function For class-based code: A single method or the entire class For use cases: The use case orchestration logic (not its dependencies)

Key principle: The unit is what you're testing, everything else should be mocked.

Step 2: Always Use autospec=True (or create_autospec())

autospec prevents typos and ensures you're only mocking real methods:

# ❌ BAD: Without autospec, allows invalid calls
def test_bad():
    mock = Mock()
    mock.typo_method_name()  # No error! Dangerous!

# ✅ GOOD: With autospec, enforces interface
def test_good():
    mock = create_autospec(ShopifyGateway, instance=True)
    # mock.typo_method_name() raises AttributeError
    mock.fetch_orders.return_value = []  # Only real methods work

Step 3: Use AsyncMock for Async Methods

from unittest.mock import AsyncMock

@pytest.fixture
def mock_async_gateway() -> AsyncMock:
    mock = AsyncMock()
    mock.fetch_orders.return_value = [order1, order2]
    return mock

# For async generators:
@pytest.fixture
def mock_async_generator() -> AsyncMock:
    mock = AsyncMock()

    async def fake_generator():
        yield order1
        yield order2

    mock.fetch_orders.return_value = fake_generator()
    return mock

Step 4: Apply the Mocking Decision Matrix

| What? | Mock? | Reasoning | |-------|-------|-----------| | Shopify API calls | ✅ YES | External HTTP service | | Kafka producers/consumers | ✅ YES | Message queue boundary | | ClickHouse queries | ✅ YES | Database boundary | | Order domain entity | ❌ NO | Pure business logic, no deps | | ProductTitle value object | ❌ NO | Simple immutable value | | Use case orchestration | ❌ NO | The unit under test | | DTOs | ❌ NO | Simple data containers | | Third-party library functions | ❌ NO | Test your code, not Kafka |

Step 5: Create Reusable Mock Factories

Store mock factories in conftest.py for reuse across tests:

# tests/unit/conftest.py

from unittest.mock import AsyncMock, create_autospec

@pytest.fixture
def mock_shopify_gateway() -> AsyncMock:
    """Reusable mock for ShopifyGateway."""
    mock = create_autospec(ShopifyGateway, instance=True)

    async def fake_orders():
        yield create_test_order(order_id="1")
        yield create_test_order(order_id="2")

    mock.fetch_orders.return_value = fake_orders()
    return mock

@pytest.fixture
def mock_kafka_publisher() -> AsyncMock:
    """Reusable mock for Kafka publisher."""
    mock = create_autospec(PublisherPort, instance=True)
    mock.publish_order.return_value = None
    mock.close.return_value = None
    return mock

Step 6: Mock Complex Side Effects Carefully

Use side_effect for error scenarios, but keep it simple:

# ✅ GOOD: Simple side effect for retry testing
mock_gateway.fetch_orders.side_effect = [
    ShopifyApiException("Temporary error"),
    [order1, order2],  # Succeeds on second call
]

# ❌ BAD: Over-complex side effect (use parametrization instead)
def complex_side_effect(*args, **kwargs):
    if args[0] == "123":
        return order1
    elif args[0] == "456":
        return order2
    else:
        raise ValueError()

mock_gateway.get_order.side_effect = complex_side_effect

Step 7: Know What NOT to Mock

Never mock:

  • Domain entities (Order, ProductRanking)
  • Value objects (ProductTitle, Money, OrderId)
  • Domain validation logic and business rules
  • Simple data structures and DTOs
  • The unit under test itself

Instead:

  • Create real instances
  • Test their behavior directly
  • Use factories for sensible defaults
# ❌ DON'T mock domain objects
def test_bad(mocker):
    mock_order = mocker.Mock()  # Wrong!
    assert mock_order.is_valid()  # Testing the mock, not domain logic

# ✅ DO create real domain objects
def test_good():
    order = Order(
        order_id=OrderId("123"),
        customer_name="John",
        line_items=[create_test_line_item()],
        total_price=Money.from_float(99.99),
    )
    order.validate()  # Testing real business logic
    assert order.is_valid()

Step 8: Verify Mock Interactions Properly

Use mock assertion methods to verify the unit called its dependencies correctly:

async def test_use_case_interactions(mock_gateway: AsyncMock) -> None:
    """Verify use case calls dependencies as expected."""
    use_case = ExtractOrdersUseCase(gateway=mock_gateway)

    await use_case.execute()

    # Verify the gateway was called
    mock_gateway.fetch_orders.assert_awaited_once()  # For async

    # Verify it was called with specific arguments
    mock_gateway.fetch_orders.assert_called_once_with(
        start_date=expected_date,
        end_date=expected_date
    )

    # Verify call count for loops
    assert mock_gateway.publish.call_count == 3

Examples

Example 1: Mock External HTTP API

from unittest.mock import AsyncMock, create_autospec
import pytest
from app.extraction.application.use_cases import ExtractOrdersUseCase
from app.extraction.adapters.shopify import ShopifyGateway

@pytest.fixture
def mock_shopify_gateway() -> AsyncMock:
    """Mock Shopify API calls."""
    mock = create_autospec(ShopifyGateway, instance=True)

    async def fake_orders():
        yield {"id": "1", "total": 100.0}
        yield {"id": "2", "total": 200.0}

    mock.fetch_orders.return_value = fake_orders()
    return mock

@pytest.mark.asyncio
async def test_extract_orders_success(mock_shopify_gateway: AsyncMock) -> None:
    """Test order extraction with mocked API."""
    use_case = ExtractOrdersUseCase(gateway=mock_shopify_gateway)

    result = await use_case.execute()

    assert result.orders_count == 2
    mock_shopify_gateway.fetch_orders.assert_awaited_once()

Example 2: Mock with Error Scenarios

from unittest.mock import AsyncMock
import pytest

@pytest.mark.asyncio
async def test_extract_orders_with_retry(
    mock_shopify_gateway: AsyncMock
) -> None:
    """Test retry logic on temporary failure."""
    # First call fails, second succeeds
    mock_shopify_gateway.fetch_orders.side_effect = [
        RuntimeError("Temporary error"),
        [{"id": "1", "total": 100.0}],
    ]

    use_case = ExtractOrdersUseCase(
        gateway=mock_shopify_gateway,
        max_retries=3
    )

    result = await use_case.execute()

    assert result.orders_count == 1
    assert mock_shopify_gateway.fetch_orders.call_count == 2

Example 3: Builder Pattern for Complex Mocks

from unittest.mock import AsyncMock, create_autospec

class MockGatewayBuilder:
    """Builder for creating configured mocks with sensible defaults."""

    def __init__(self):
        self.mock = create_autospec(ShopifyGateway, instance=True)
        self.orders = []

    def with_orders(self, orders: list) -> "MockGatewayBuilder":
        """Configure mock to return specific orders."""
        async def fake_fetch():
            for order in orders:
                yield order

        self.mock.fetch_orders.return_value = fake_fetch()
        return self

    def with_error(self, error: Exception) -> "MockGatewayBuilder":
        """Configure mock to raise error."""
        self.mock.fetch_orders.side_effect = error
        return self

    def build(self) -> AsyncMock:
        """Return configured mock."""
        return self.mock

# Usage
@pytest.mark.asyncio
async def test_with_builder():
    mock = MockGatewayBuilder()\
        .with_orders([order1, order2])\
        .build()

    use_case = ExtractOrdersUseCase(gateway=mock)
    result = await use_case.execute()
    assert result.orders_count == 2

Example 4: Type-Safe Mocking with Protocols

from typing import Protocol
from unittest.mock import AsyncMock, create_autospec

class OrderRepository(Protocol):
    """Protocol defining repository interface."""

    async def create(self, order_data: dict) -> Order:
        """Create order in storage."""
        ...

@pytest.fixture
def mock_repository() -> AsyncMock:
    """Create type-safe mock repository."""
    mock = create_autospec(OrderRepository, instance=True)
    mock.create = AsyncMock(return_value=Order(id="123"))
    return mock

Requirements

  • Python 3.11+
  • pytest >= 7.0
  • unittest.mock (standard library)
  • Optional: pytest-mock for mocker fixture
  • pytest-asyncio for async test support

See Also

From the file to a first result

The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.

View the source and supporting files

This download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.

1. Download the file

Download SKILL.md

From the root of a test project, save the file as .skill-downloads/pytest-mocking-strategy-msy76azv/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.

Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.

mkdir -p '.skill-downloads/pytest-mocking-strategy-msy76azv' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/pytest-mocking-strategy-msy76azv/download' --output '.skill-downloads/pytest-mocking-strategy-msy76azv/SKILL.md.part' && mv '.skill-downloads/pytest-mocking-strategy-msy76azv/SKILL.md.part' '.skill-downloads/pytest-mocking-strategy-msy76azv/SKILL.md'

2. Check prerequisites

Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.

Tools declared by the author (check availability in your agent): Read, Bash, Write

Original excerpt: Requirements
  • Python 3.11+
  • pytest >= 7.0
  • unittest.mock (standard library)
  • Optional: pytest-mock for mocker fixture
  • pytest-asyncio for async test support

The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.

3. Load the skill

After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.

Claude Code
test -f '.skill-downloads/pytest-mocking-strategy-msy76azv/SKILL.md' && test -s '.skill-downloads/pytest-mocking-strategy-msy76azv/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/pytest-mocking-strategy' && cp -R '.skill-downloads/pytest-mocking-strategy-msy76azv' '.claude/skills/pytest-mocking-strategy'

Cursor
test -f '.skill-downloads/pytest-mocking-strategy-msy76azv/SKILL.md' && test -s '.skill-downloads/pytest-mocking-strategy-msy76azv/SKILL.md' && mkdir -p '.cursor/skills' && test ! -e '.cursor/skills/pytest-mocking-strategy' && cp -R '.skill-downloads/pytest-mocking-strategy-msy76azv' '.cursor/skills/pytest-mocking-strategy'

Codex
test -f '.skill-downloads/pytest-mocking-strategy-msy76azv/SKILL.md' && test -s '.skill-downloads/pytest-mocking-strategy-msy76azv/SKILL.md' && mkdir -p '.agents/skills' && test ! -e '.agents/skills/pytest-mocking-strategy' && cp -R '.skill-downloads/pytest-mocking-strategy-msy76azv' '.agents/skills/pytest-mocking-strategy'

OpenSkills

OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.

Official OpenSkills installation
openskills install './.skill-downloads/pytest-mocking-strategy-msy76azv'

openskills read 'pytest-mocking-strategy-msy76azv'

Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.

For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.

openskills sync

Manual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.

4. Try a first task

Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:

Read .skill-downloads/pytest-mocking-strategy-msy76azv/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.

Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.

Related skills