Skip to main content

An automagic `patch` fixture that can patch objects directly or by name.

Project description

Pytest Patch

An automagic replacement for monkeypatch or unittest.mock.patch that can be used on objects either directly or by name.

tests

Usage

patching objects directly

from pytest import fixture
from mock import MagicMock, sentinel

from mymodule import callee, caller  # `caller` returns `callee()`


@fixture
def callee_mock(patch):
  # returns the second argument if provided, `MagicMock()` otherwise
  return patch(callee, MagicMock(return_value=sentinel.callee))


def test_caller(callee_mock):
  assert caller() == sentinel.callee

patching objects by name

@fixture
def callee_mock(patch):
  return patch('callee')  # assumes `callee` is in `mymodule`

patching by full path

This behavior is similar to unittest.mock.patch.

@fixture
def callee_mock(patch):
  return patch('mymodule.callee')

Configuration

By default, pytest-patch assumes the following repository structure:

mymodule/
  .git/
    ...
  mymodule/
    wat/
      ermelon.py
    ...
  tests/
    */  # e.g. unit/
      mymodule/
        wat/
          test_ermelon.py
    ...

No configuration is needed if your repository matches this structure where the repository name is the same as the name of the module and your tests are in their corresponding subdirectories mirroring your module's path structure.

If the above is not the case, e.g. you have a repository with a nonmatching module name or a monorepo with multiple modules, you may specify the corresponding flags either in the pytest command line or the ini file, glob-style:

pytest --module-names=mymodule,othermodule --test-paths=tests.unit,tests.e2e.*

or (in pytest.ini)

[pytest]
module_names = mymodule othermodule
test_paths = tests.unit tests.e2e.*

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-patch-0.2.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

pytest_patch-0.2.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file pytest-patch-0.2.0.tar.gz.

File metadata

  • Download URL: pytest-patch-0.2.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for pytest-patch-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ce5e2ff6bd7f4fb06dda2eedb2b78dfbe218a2fd02ce445c8678542f8309f0b5
MD5 9401a55bd2ee90122aff113781e916c7
BLAKE2b-256 561ce01f8ba5902cf51e6b3dfb61a779e864b848ff0c30b74cca4f896687a8f1

See more details on using hashes here.

File details

Details for the file pytest_patch-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_patch-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca9e83547dca47136bf241aeca1a94af06608a8c9d584396334513d84a7b0f80
MD5 ea9862542df4911a5c6f19737c87db88
BLAKE2b-256 91166e9c9aed15308382d457daed06db5a277519997a1d83a63765d83f650387

See more details on using hashes here.

Supported by

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