Skip to main content

Utility which makes mocking more readable and controllable

Project description

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

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

pytest_when-1.0.10.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_when-1.0.10-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file pytest_when-1.0.10.tar.gz.

File metadata

  • Download URL: pytest_when-1.0.10.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.9.2 CPython/3.10.12

File hashes

Hashes for pytest_when-1.0.10.tar.gz
Algorithm Hash digest
SHA256 1290d4c2fe8a705ce568c286de4b1aee7dc5877b9ccfedd8e77e82c372774776
MD5 c69d754c16c2158d34e0db6a0422137b
BLAKE2b-256 3f7bd191c5ab108ea4050a7b1d4ae637d5ccef90b256e3836e892ec90931c0a4

See more details on using hashes here.

File details

Details for the file pytest_when-1.0.10-py3-none-any.whl.

File metadata

  • Download URL: pytest_when-1.0.10-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.9.2 CPython/3.10.12

File hashes

Hashes for pytest_when-1.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 d36d0dfb6d140b8ecbf84b4cbdb85a01d232539a25843489932698a487bc845c
MD5 e8df1a7a75ebfe3832f1becdc21bfabe
BLAKE2b-256 2a224bee4721b21019705eefb1f0abec507c9ec574143488465a14abb95f972e

See more details on using hashes here.

Supported by

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