Skip to main content

chainmock

Chainmock - Mocking library for Python and Pytest.

pypi ci documentation license


Documentation


Chainmock is a mocking library for Python and pytest. Under the hood it uses Python standard library mocks providing an alternative syntax to create mocks and assertions. Chainmock also comes with some additional features to make testing faster and more straightforward. The syntax works especially well with pytest fixtures.

Installation

Install with pip:

pip install chainmock

Features

Chainmock supports all the same features that Python standard library unittest mocks support and adds some convenient extra functionality.

  • Mocking: Create mocks and assert call counts and arguments or replace return values.
  • Spying: Spying proxies the calls to the original function or method. With spying you can assert call counts and arguments without mocking.
  • Stubs: Easily create stub objects that can be used in tests as fake data or to replace real objects.
  • Async support: Chainmock supports mocking and spying async functions and methods. Most of the time it also recognizes automatically when async mocking should be used so it is not any harder than mocking sync code.
  • Fully type annotated: The whole codebase is fully type annotated so Chainmock works well with static analysis tools and editor autocomplete.
  • Works with Python 3.11+ and PyPy3.
  • Supports pytest, unittest, and doctest test runners.

Examples

The entrypoint to Chainmock is the mocker function. Import the mocker function as follows:

from chainmock import mocker

Mocking

To mock you just give the object that you want to mock to the mocker function. After this you can start mocking individual attributes and methods as follows:

# Assert that a certain method has been called exactly once
mocker(Teapot).mock("add_tea").called_once()

# Provide a return value and assert that method has been called twice
mocker(Teapot).mock("brew").return_value("mocked").called_twice()

# Assert that a method has been called with specific arguments at most twice
mocker(Teapot).mock("add_tea").all_calls_with("green").call_count_at_most(2)

Spying

Spying is not any harder than mocking. You just need to call the spy method instead of the mock method. After spying a callable, it works just like before spying and you can start making assertions on it.

# Assert that a certain method has been called at least once
mocker(Teapot).spy("add_tea").called()

# Check that a method has been called at most twice and has
# at least one call with the given argument
mocker(Teapot).spy("add_tea").any_call_with("green").call_count_at_most(2)

Stubs

To create a stub object, just call mocker function without any arguments.

# Create a stub with a method called "my_method"
stub = mocker().mock("my_method").return_value("it works!").self()
assert stub.my_method() == "it works!"

# You can give keyword arguments to the mocker function to quickly set properties
stub = mocker(my_property=10)
assert stub.my_property == 10

For more details and examples, see the API reference.

Similar projects

If Chainmock is not what you need, check out also these cool projects:

  • flexmock: Chainmock's API is heavily inspired by flexmock. Flexmock doesn't use standard library unicttest and it has fully custom mocking implementation. Compared to flexmock, Chainmock has more familiar API if you have been using standard library unittest and Chainmock also supports async mocking and partial argument matching.
  • pytest-mock: Similar to Chainmock, pytest-mock is a wrapper for standard library unittest. Compared to pytest-mock, Chainmock has a more ergonomic API for mocking and spying.

Contributing

Do you like this project and want to help? If you need ideas, check out the open issues and feel free to open a new pull request. Bug reports and feature requests are also very welcome.

Metadata

Release files for chainmock 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chainmock 3.0.0
File Size Uploaded Provenance Actions
chainmock-3.0.0.tar.gz 41.9 kB With provenance Details for chainmock-3.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for chainmock 3.0.0
File Interpreter ABI Platform Actions
chainmock-3.0.0-py3-none-any.whl Python 3 none any Details for chainmock-3.0.0-py3-none-any.whl

Total release size: 60.5 kB

Release files / chainmock-3.0.0.tar.gz

Download URL chainmock-3.0.0.tar.gz
Size 41.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e296cc97e9d6d0aa37ab582eba2996943bb4a47be344af80fb3c9fca962e2c75
BLAKE2b-256 checksum
How to use checksums
aa5d8785807031370f53137e3c78cf0c18d23f2110fc08753b9c63054bda5522
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 7, 2026.

Transparency log

Release files / chainmock-3.0.0-py3-none-any.whl

Download URL chainmock-3.0.0-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c977670af9f25095f00f1ff2ed60fcaf6ffed53016410b5cd95a03810ff926fc
BLAKE2b-256 checksum
How to use checksums
f94e30e2d98bd1e7a7f569a49f6dc098ec7aa2a138a7453835cd237a9fa215d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 7, 2026.

Transparency log

Security for chainmock 3.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for chainmock-3.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 7, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page