Skip to main content

Mocking library for Python and Pytest

Project description

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.8+ 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 unittest 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. However, pytest-mock doesn't provide any extra functionality as it just exposes unittest mocks directly to the user.

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chainmock-0.11.0.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

chainmock-0.11.0-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file chainmock-0.11.0.tar.gz.

File metadata

  • Download URL: chainmock-0.11.0.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.12 Linux/6.5.0-10027-tuxedo

File hashes

Hashes for chainmock-0.11.0.tar.gz
Algorithm Hash digest
SHA256 601be8a7f33789f67a4e3ae1795bd59cca91b6a3bd00f7472619fa05a8ac4866
MD5 0a6587894a1819fe1788ad1f35d92965
BLAKE2b-256 84118befdcbdfdc5c67247e7d50d8371cdd9ac68c005d2b1fc3ade4e465892b1

See more details on using hashes here.

File details

Details for the file chainmock-0.11.0-py3-none-any.whl.

File metadata

  • Download URL: chainmock-0.11.0-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.12 Linux/6.5.0-10027-tuxedo

File hashes

Hashes for chainmock-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 71bdfd8938159e2c504c2de78f9cc74aab0d39a1531c3f132b12548ef3e3e7ae
MD5 fdfea54a65d016ae68164e07a21c12a3
BLAKE2b-256 24382e06b7b1ec9a4448e5a83f78b10653c7ab42cd98959ae300cfc36cea8769

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page