API Reference

pytest-mock

A pytest plugin providing a mocker fixture as a thin wrapper over unittest.mock.

Official Documentation ↗
CompatibilityThis documentation covers version v3.12. Check the official docs for the latest API surface.

Installation

pippip install pytest-mock

Methods

pytest-mock exposes mocking helpers as a fixture so patches are automatically undone.

Key Features

  • mocker.patch with automatic cleanup
  • mocker.spy to wrap real methods
  • Access to call history and assert_called helpers

Usage Notes

Pin a version and check the official docs for the API surface you depend on before upgrading.

FAQ

How is it different from unittest.mock?
It removes the need for with blocks and decorators by undoing patches automatically at test teardown.
When should I spy instead of patch?
Spy when you want to observe calls but keep the real behavior; patch when the real dependency must not run.
How do I patch a class method?
Use mocker.patch.object(Class, "method") with a return_value, targeting the attribute on the class object.