Agent skill · software engineering · aiskillstore

pytest-mock-guide

Guide for using pytest-mock plugin to write tests with mocking. Use when writing pytest tests that need mocking, patching, spying, or stubbing. Covers mocker fixture usage, patch methods, spy/stub patterns, and assertion helpers.

Why this skill is useful

Provides detailed patterns and usage examples for mocking in pytest that the AI wouldn't reliably generate on its own.

What it needs

About 4k tokens when loaded. Last updated 2026-08-07. 405 stars on the source repository.

What this skill does

pytest-mock Usage Guide pytest-mock is a pytest plugin providing a mocker fixture as a thin wrapper around Python's unittest.mock patching API. It automatically undoes all mocking at the end of each test. The mocker Fixture The mocker fixture is the main interface. Request it in your test function: Available Fixture Scopes Fixture Scope Use Case --------- ------- ---------- mocker function Default, per-test mocking classmocker class Share mocks across test class modulemocker module Share mocks across test module packagemocker package Share mocks across package sessionmocker session Share mocks across entire session Patching Methods mocker.patch(target, ...) Patch a module-level object by its dotted path: mocker.patch.object(target, attribute, ...) Patch an attribute on an object directly: mocker.patch.dict(indict, values, clear=False) Patch a dictionary temporarily: mocker.patch.multiple(target, kwargs) Patch multiple attributes at once: mocker.patch.contextmanager(target, attribute, ...) Same as patch.object but doesn't warn when mock is used as context manager: Common Patch Parameters Parameter Description ----------- ------------- new Object to replace target with returnvalue Value returned when mock is called sideeffect Exception to raise or function to call autospec Create mock matching target's signature spec Object to use as specification specset Stricter spec that prevents setting new attributes create Allow patching non-existent attributes newcallable Callable to create the mock Spying with mocker.spy() Spy wraps the real method while tracking calls: Spy Attributes Attribute Description ----------- ------------- spyreturn Last return value from real method spyreturnlist List of all return values spyreturniter Iterator copy (when duplicateiterators=True) spyexception Last exception raised, if any Spying Iterators Creating Stubs mocker.stub(name=None) Create a stub that accepts any arguments: mocker.asyncstub(name=None) Create an async stub: Mock Helpers mock …

How to use it

Reference it in AdaL, Claude Code, Cursor or any coding agent — nothing to install:

@skills aiskillstore/pytest-mock-guide

View the source on GitHub

Browse the @skills marketplace