python-test-doubles

Creates Python test doubles using unittest.mock, pytest fixtures, and the Mother pattern.

138|5|Updated Sep 19, 2024
One-click install
npx skills add https://github.com/macalbert/envilder --skill python-test-doubles-macalbert
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-test-doubles
Source: https://github.com/macalbert/envilder/tree/main/.github/skills/python-test-doubles
Command: npx skills add https://github.com/macalbert/envilder --skill python-test-doubles-macalbert

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires pytest.

What problem does it solve? Writing consistent, maintainable Python tests requires knowing when and how to use mocks, stubs, spies, and patches. Without clear conventions, tests become brittle, mocks silently accept typos, and test data setup gets duplicated across files. ## Core Features & Use Cases - Test Double Patterns: Standardized usage of Mock, AsyncMock, return_value, side_effect, wraps, and patch for replacing dependencies in tests. - Verification Recipes: Assertion patterns for call counts, arguments, awaited calls, and not-called checks, including async verification with assert_awaited. - Mother Pattern Factories: Reusable factory classes for building complex test data objects with sensible defaults. - Use Case: When writing a pytest suite for a service that calls AWS SSM via boto3, use this Skill to patch boto3.client, stub get_parameter responses, simulate ClientError failures, and verify interactions. ## Quick Start Write a pytest test for my class that mocks its ISecretProvider dependency with spec, stubs the get_secret return value, and verifies the call.

Frequently Asked Questions about python-test-doubles

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I mock a dependency in Python pytest tests?

Create a Mock with spec set to the interface class inside a pytest fixture, then inject it into the system under test. Configure responses with return_value or side_effect in the Arrange step and verify calls in Assert.

What is the difference between Mock and AsyncMock in unittest.mock?

Mock replaces synchronous callables, while AsyncMock replaces async functions and coroutines. AsyncMock supports await-specific assertions like assert_awaited_once_with, which Mock does not provide.

How do I mock boto3 or module-level imports in Python tests?

Use the patch decorator targeting the module path where the object is imported, such as @patch("boto3.client"). Configure the returned mock's methods inside the test to simulate API responses.

Why should I use spec when creating a Mock in Python?

The spec argument restricts the mock to attributes existing on the specified class, so typos in method names raise AttributeError at test time instead of silently passing. This catches interface drift between tests and real code.

How do I simulate exceptions with unittest.mock side_effect?

Assign an exception instance to the mock's side_effect attribute, for example a ClientError for AWS failures. When the code under test calls the mock, the exception is raised, letting you test error handling paths.

When should I use wraps instead of replacing a mock entirely?

Use Mock(wraps=real_object) when you want to observe calls on a real implementation without changing its behavior. This creates a spy that executes the real logic while recording interactions for verification.