Skip to main content

pytest-when

Plugin provides a when fixture, which enables the following way of mocking the python objects:

(
    when(some_object, "attribute")
    .called_with(1, 2, when.markers.any)
    .then_return("mocked")
)

It is readable and gives you a way to enable the mock only for a specific argument's values. In this the attribute will be mocked, for specific first two arguments and any third argument.

Inspired by https://github.com/mockito/mockito-scala

.called_with method arguments are compared with the real callable signature and gives additional protection against changing the real callable interface.

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 which we're going to mock in the test
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",
        when.markers.any,
        kwarg1="b",
        kwarg2=when.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"
    )

# if you need to patch a function
def test_patch_a_function(when):
    when(example_module, "some_normal_function").called_with(
        "a",
        when.markers.any,
        kwarg1="b",
        kwarg2=when.markers.any,
    ).then_return("Mocked")

    assert (
            example_module.some_normal_function(
                "a",
                1,
                kwarg1="b",
                kwarg2="c",
            )
            == "Mocked"
    )
    assert (
            example_module.some_normal_function(
                "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:

  1. pdm https://pdm.fming.dev/latest/#installation
  2. python3.8 (minimum supported by a tool)
pdm install

To run tests and linters use:

make test
make lint

Release files for pytest-when 1.1.3

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

Source distribution (sdist)

Source distribution for pytest-when 1.1.3
File Size Uploaded
pytest_when-1.1.3.tar.gz 11.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-when 1.1.3
File Interpreter ABI Platform
pytest_when-1.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 20.9 kB

Release files / pytest_when-1.1.3.tar.gz

Download URL pytest_when-1.1.3.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
aa892b208745a4fbc0c3f0468b6bb6763e752cd4f0013ea454fca3f6f520d36e
BLAKE2b-256 checksum
How to use checksums
ff9f26de77096201a3ef8ac07a5a1f52c0c8e2c1dc3b46786a149468f0df3ed1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.12.2 CPython/3.10.12

Release files / pytest_when-1.1.3-py3-none-any.whl

Download URL pytest_when-1.1.3-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
046cf9bd96c636626565101161497ad917dd2f6d73c0575fa6af2a4b4c198760
BLAKE2b-256 checksum
How to use checksums
1e49ea214360815e14b2e5e0b4a20726db801ca69e8ba26c5b015a81b2fa5d5a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.12.2 CPython/3.10.12

Release history Release notifications | RSS feed

2.3.0

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.1.5

2 release files

1.1.4

2 release files

This release

1.1.3 This release

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.1

2 release files

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