Package: chainmock
Chainmock- Mocking library for Python and Pytest.DocumentationChainmock 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.InstallationInstall with pip:pip install chainmockFeaturesChainmock supports all the same features that Python standard library unittest
mocks support and adds some convenient extra functionality.Mocking: Createmocksand assert call counts and arguments or replace
return values.Spying:Spyingproxies the calls to the original function or method.
With spying you can assert call counts and arguments without mocking.Stubs: Easily createstubobjects that can be used in tests as fake data
or to replace real objects.Async support: Chainmock supports mocking and spyingasyncfunctions 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 withPython 3.8+ and PyPy3.Supportspytest,unittest, anddoctesttest runners.ExamplesThe entrypoint to Chainmock is themockerfunction. Import themockerfunction as follows:fromchainmockimportmockerMockingTo mock you just give the object that you want to mock to themockerfunction.
After this you can start mocking individual attributes and methods as follows:# Assert that a certain method has been called exactly oncemocker(Teapot).mock("add_tea").called_once()# Provide a return value and assert that method has been called twicemocker(Teapot).mock("brew").return_value("mocked").called_twice()# Assert that a method has been called with specific arguments at most twicemocker(Teapot).mock("add_tea").all_calls_with("green").call_count_at_most(2)SpyingSpying is not any harder than mocking. You just need to call thespymethod
instead of themockmethod. 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 oncemocker(Teapot).spy("add_tea").called()# Check that a method has been called at most twice and has# at least one call with the given argumentmocker(Teapot).spy("add_tea").any_call_with("green").call_count_at_most(2)StubsTo create a stub object, just callmockerfunction without any arguments.# Create a stub with a method called "my_method"stub=mocker().mock("my_method").return_value("it works!").self()assertstub.my_method()=="it works!"# You can give keyword arguments to the mocker function to quickly set propertiesstub=mocker(my_property=10)assertstub.my_property==10For more details and examples, see theAPI reference.Similar projectsIf 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.ContributingDo 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.