{"id":16574203,"url":"https://github.com/zhukovgreen/pytest-when","last_synced_at":"2025-10-08T16:08:44.475Z","repository":{"id":167239229,"uuid":"642786552","full_name":"zhukovgreen/pytest-when","owner":"zhukovgreen","description":"Pytest plugin for more readable mocking","archived":false,"fork":false,"pushed_at":"2025-05-06T18:59:26.000Z","size":278,"stargazers_count":25,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-20T19:37:53.517Z","etag":null,"topics":["bdd","mocking","pytest","pytest-plugin"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zhukovgreen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-05-19T10:45:28.000Z","updated_at":"2025-09-12T13:13:21.000Z","dependencies_parsed_at":"2024-05-31T00:04:35.583Z","dependency_job_id":"9500cccb-e751-47e4-88d0-9d3142b342d1","html_url":"https://github.com/zhukovgreen/pytest-when","commit_stats":null,"previous_names":["zhukovgreen/pytest-when"],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/zhukovgreen/pytest-when","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhukovgreen%2Fpytest-when","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhukovgreen%2Fpytest-when/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhukovgreen%2Fpytest-when/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhukovgreen%2Fpytest-when/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zhukovgreen","download_url":"https://codeload.github.com/zhukovgreen/pytest-when/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhukovgreen%2Fpytest-when/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278972851,"owners_count":26078115,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bdd","mocking","pytest","pytest-plugin"],"created_at":"2024-10-11T21:44:00.401Z","updated_at":"2025-10-08T16:08:44.470Z","avatar_url":"https://github.com/zhukovgreen.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![PyPI](https://img.shields.io/pypi/v/pytest-when.svg)](https://pypi.python.org/pypi/pytest-when)\n[![Build](https://github.com/Tinche/tightwrap/workflows/CI/badge.svg)](https://github.com/zhukovgreen/pytest-when/actions/workflows/test-lint.yml?query=workflow:%22Test+Action%22)\n[![Supported Python Versions](https://img.shields.io/python/required-version-toml?tomlFilePath=https://raw.githubusercontent.com/zhukovgreen/pytest-when/main/pyproject.toml)](https://raw.githubusercontent.com/zhukovgreen/pytest-when/main/pyproject.toml)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n\n# pytest-when\n\n\u003cimg src=\"./logo.png\" width=\"250\"/\u003e\n\nInspired\nby [mokito-scala](https://github.com/mockito/mockito-scala), `pytest-when`\nprovides a fixture for `pytest` to simplify the mocking of python objects:\n\n## Purpose\n\nMore readable than the full `Given...When...Then` pattern, `pytest-when` is\nmeant for developers who want to test for behaviour, without any extra\noverhead.\nEnable the mock only for a specific argument's values to make code more\nreadable.\n\n## Benefits\n\nIn this example, when you specify the first two arguments and *any* third\nargument, the attribute will be mocked,\n\n```python\n(\n    when(some_object, \"attribute\")\n    .called_with(1, 2, when.markers.any)\n    .then_return(\"attribute mocked\")\n    # or\n    # .then_call(some_callable)\n    # or\n    # .then_raise(SomeException())\n)\n```\n\nNote that the `.called_with` method arguments are compared with the real\ncallable signature.\nThis gives additional protection against changing the real callable interface.\n\nWith `when` fixture, the following expression:\n\n```python\nwhen(example_module, \"some_normal_function\").called_with(\n    \"a\",\n    1,\n    kwarg1=\"b\",\n    kwarg2=2,\n).then_return(\"Mocked\")\n\n```\n\nis roughly equal to:\n\n```python\n\nmock_only_for_calls = (\n    call(\"a\", 1, kwarg1=\"b\", kwarg2=2),\n    \"Mocked\",\n)\n\ndef matches(\n        call: Call,\n        mocked_calls_registry: tuple[tuple[call, ReturnType], ...],\n) -\u003e bool:\n    ...\n\ndef create_call(*args, **kwargs) -\u003e Call:\n    ...\n\ndef side_effect_callback(*args, **kwargs):\n    if matches(create_call(*args, **kwargs), mock_only_for_calls):\n        return mock_only_for_calls[1]\n    return unittest.mock.DEFAULT\n\n\nmocker.patch.object(\n    example_module,\n    \"some_normal_function\",\n    autospec=True,\n    side_effect=side_effect_callback,\n)\n```\n\nwhere logic of `matches` and `create_call` is not trivial\n\n## Installation\n\nInstall the package into your development environment, from\n[pypi](https://pypi.org/project/pytest-when/), using `pip`, for example:\n\n```bash\npip install pytest-when\n```\n\n## Implementation\n\nOnced installed, the `when` fixture will be available just like the rest of\nthe `pytest` plugins.\nSee the following example of how to use it:\n\n```python\n# class which we're going to mock in the test\nclass Klass1:\n    def some_method(\n            self,\n            arg1: str,\n            arg2: int,\n            *,\n            kwarg1: str,\n            kwarg2: str,\n    ) -\u003e str:\n        return \"some_method not mocked\"\n\n\ndef test_should_properly_patch_calls(when):\n    when(Klass1, \"some_method\").called_with(\n        \"a\",\n        when.markers.any,\n        kwarg1=\"b\",\n        kwarg2=when.markers.any,\n    ).then_return(\"some method mocked\")\n\n    assert (\n            Klass1().some_method(\n                \"a\",\n                1,\n                kwarg1=\"b\",\n                kwarg2=\"c\",\n            )\n            == \"some method mocked\"\n    )\n    assert (\n            Klass1().some_method(\n                \"not mocked param\",\n                1,\n                kwarg1=\"b\",\n                kwarg2=\"c\",\n            )\n            == \"some method not mocked\"\n    )\n\n\n# if you need to patch a function\ndef test_patch_a_function(when):\n    when(example_module, \"some_normal_function\").called_with(\n        \"a\",\n        when.markers.any,\n        kwarg1=\"b\",\n        kwarg2=when.markers.any,\n    ).then_return(\"some_normal_function mocked\")\n\n    assert (\n            example_module.some_normal_function(\n                \"a\",\n                1,\n                kwarg1=\"b\",\n                kwarg2=\"c\",\n            )\n            == \"some_normal_function mocked\"\n    )\n    assert (\n            example_module.some_normal_function(\n                \"not mocked param\",\n                1,\n                kwarg1=\"b\",\n                kwarg2=\"c\",\n            )\n            == \"some_normal_function not mocked\"\n    )\n```\n\nIt is possible to use `when` with class methods and standalone functions\n(in this case cls parameter will become a python module).\n\nYou can patch the same object multiple times using different `called_with`\nparameters in a single test.\n\nYou can also patch multiple targets (cls, method)\n\nSee more examples at:\n[test_integration](tests/test_integration.py)\n\n## Setup for local developement\n\nThe project can be extended by cloning the repo and\ninstalling [the `PDM` build tool](https://pdm-project.org/latest/#recommended-installation-method)\nSo, the development environment requires:\n\n1. pdm \u003chttps://pdm.fming.dev/latest/#installation\u003e\n2. python3.8 or greater\n\n```bash\npdm install\n```\n\nTo run tests and linters use:\n\n```bash\nmake test\nmake lint\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhukovgreen%2Fpytest-when","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhukovgreen%2Fpytest-when","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhukovgreen%2Fpytest-when/lists"}