
Security News
Node.js Homepage Adds Paid Support Link, Prompting Contributor Pushback
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Chainmock - Mocking library for Python and Pytest.
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.
Install with pip:
pip install chainmock
Chainmock supports all the same features that Python standard library unittest mocks support and adds some convenient extra functionality.
pytest
, unittest
, and doctest
test runners.The entrypoint to Chainmock is the mocker
function. Import the mocker
function as follows:
from chainmock import mocker
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 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)
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.
If chainmock is not what you need, check out also these cool projects:
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.
FAQs
Mocking library for Python and Pytest
We found that chainmock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.
Research
Security News
The Socket Research Team investigates a malicious Python typosquat of a popular password library that forces Windows shutdowns when input is incorrect.