Skip to main content

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 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"
    )

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.1.tar.gz (7.3 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.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_when-1.0.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.6.1 CPython/3.10.6

File hashes

Hashes for pytest_when-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ffd19f36d6f98710b923c444f81ce83d094a09a693d38fda99ea66d99fd2606a
MD5 99eb6a5f84ad8fe5a5d5bea91a770b81
BLAKE2b-256 8f105f3777f2d68ebda1252b8a8315b515f18461b043e3865e99a4fd8108ce07

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytest_when-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.6.1 CPython/3.10.6

File hashes

Hashes for pytest_when-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b723a3e21e273ce4baa44801144622c3f4519214f3a2e11034c6d61b02d2b11e
MD5 86e7522bd0ddc6e8ec5d4371e0b12b5c
BLAKE2b-256 540fb54e08997d37c4c4406ce32d0eb7e6186bd48da39d055ebb1fc37ec0ca7b

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