Utility makes mocing more readable
Project description
pytest-when
Pytest plugin for making mocking in python more readable. Inspired by https://github.com/mockito/mockito-scala
Installation
pip install pytest-when
Usage
After installing the package a new fixture when
will be available.
See the following example how to use it:
class Klass1:
def some_method(
self,
arg1: str,
arg2: int,
*,
kwarg1: str,
kwarg2: str,
) -> str:
return "Not mocked"
def test_should_properly_patch_calls(when):
when(Klass1, "some_method").called_with(
"a",
Markers.any,
kwarg1="b",
kwarg2=Markers.any,
).then_return("Mocked")
assert (
Klass1().some_method(
"a",
1,
kwarg1="b",
kwarg2="c",
)
== "Mocked"
)
assert (
Klass1().some_method(
"not mocked param",
1,
kwarg1="b",
kwarg2="c",
)
== "Not mocked"
)
It is possible to use 'when' with class methods and standalone functions (in this case cls parameter will become a python module).
You can patch multiple times the same object with different "called_with" parameters in a single test.
You can also patch multiple targets (cls, method)
See more examples at: test_integration
Setup for local developement
Requirements:
- pdm https://pdm.fming.dev/latest/#installation
- python3.8 (minimum supported by a tool)
pdm install
To run tests and linters use:
make test
make lint
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pytest_when-1.0.0.tar.gz
(7.3 kB
view hashes)
Built Distribution
Close
Hashes for pytest_when-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6211d8e1fc906e482d39d8c7b3728f95cbaf9e24f863bb311ce85b8a91e7f2e |
|
MD5 | b68a8564fb18b67826901aee959b20b8 |
|
BLAKE2b-256 | 91283f28b6db49c58fdbc3e61cda3d6c4b249a29514d65d9e529ba3b1c8c6246 |