Skip to main content

pytest's monkeypatch subclass with extra functionalities

Project description

The monkkeyplus plugin is a funcarg that subclasses monkeypatch and adds a few extra features to it.

Extra features

monkeyplus has two extra methods: patch_osstat and patch_today.

patch_osstat(path, **osstat_attrs)

Patching os.stat is tricky because you usually want to patch only one file, not the whole filesystem. Moreover, to patch it properly, you have to return a full stat_result structure. patch_osstat takes care of this stuff. Just call it with a path to patch and keyword arguments for the stat attributes you want, for example:

monkeyplus.patch_osstat('foo/bar', st_mtime=42)

Arguments that are not specified will be assigned a default value.

patch_today(year, month, day)

Patching dates can be tricky because there’s a couple of time relates modules, namely datetime and time. If you want your patch to affect the whole app uniformly, you have to patch the time module, not the datetime module. The arithmetics to transform a year/month/day value in a suitable time.time value are non-trivial, so it can get boring to repeat. Call patch_today with the date you’d like the app to be patched to, example:

monkeyplus.patch_today(2011, 2, 16)

Replacing monkeypatch

monkeyplus is a subclass of monkeypatch, so it can be used everywhere monkeypatch is used. Instead of having to use a different funcarg, you can replace monkeypatch by putting this in your conftest.py file:

def pytest_funcarg__monkeypatch(request):
    monkeyplus = request.getfuncargvalue('monkeyplus')
    return monkeyplus

Contributions welcome

For now, monkeyplus is a plugin that reflect my own needs, but if you’d like to contribute new features to it, they’re welcome. The source is hosted at http://bitbucket.org/hsoft/pytest-monkeyplus

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-monkeyplus-1.0.0.tar.gz (3.0 kB view details)

Uploaded Source

File details

Details for the file pytest-monkeyplus-1.0.0.tar.gz.

File metadata

File hashes

Hashes for pytest-monkeyplus-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1460b94e9d0b891be38701c121028d0304648af8007d21122e9d180f6a3c203e
MD5 7abb27b8cc7741d3c83f79aa6cb7b58b
BLAKE2b-256 3fcb8fff3f0326c5ad67eeb67e3509a0d875f4ab1c78ea7b135b0d0df02b9f89

See more details on using hashes here.

Supported by

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