[py-dev] Advanced monkeypatching?

Floris Bruynooghe flub at devork.be
Sun Dec 19 15:38:01 CET 2010


Hello

On 18 December 2010 14:51, Virgil Dupras <hsoft at hardcoded.net> wrote:
> Hi there,
>
> Monkeypatching is great and all, but some type of patching is a little bit more complex to do, such as patching the system time and patching os.stat for specific filenames. I have my own "homebrewed" solution for this, but I was wondering if such solution had their place in the monkeypatch module of pytest itself. Does it? Or should a "pytest-monkeypatch++" package be created?
>
> The "homebrewed" code I'm talking about is at https://bitbucket.org/hsoft/hsutil/src/43014954c6a8/hsutil/testutil.py#cl-83 ( patch_osstat() and patch_today() ).

Looking at the patch_osstat() it seems to me that it just fakes
os.stat() for one specific path but uses the original in place
otherwise.  This sounds like it could be just a specialised object
used to patch with.  I can imagine the monkeypatch plugin to provide a
funcarg which provides this functionality, e.g.:

def test_foo(monkeypatch, monkey_osstat):
    monkeypatch.setattr(monkey_ossstat('/tmp/some_file'))
    ...

Though probably with better names ;-).

I'm not sure I understand the use case for patch_today() so won't
comment on that part.

Regards
Floris


-- 
Debian GNU/Linux -- The Power of Freedom
www.debian.org | www.gnu.org | www.kernel.org



More information about the Pytest-dev mailing list