[py-dev] Advanced monkeypatching?

Ronny Pfannschmidt Ronny.Pfannschmidt at gmx.de
Mon Dec 20 10:41:19 CET 2010

On Mon, 2010-12-20 at 09:22 +0100, Virgil Dupras wrote:
> On 2010-12-19, at 3:38 PM, Floris Bruynooghe wrote:
> > 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
> This would kind of work, but it would become complicated to patch os.stat() for two or more files, wouldn't it?
there could be a monkey_stat object that controlls the set of
files/subtrees under control

> As for patch_today(), I created it because when you want to patch the date for a test, you can't just patch datetime because there might be a function somewhere using time.time(), so you'll have inconsistent date patching. However, to patch time.time() to a datetime of your choice, you have to do some arithmetics, hence the patch_today() helper.
> My question is: Does such helpers have their place in the official pytest package or should it be an external plugin?

a good starting place would be a external plugin to try different
approaches and stabilize a bit since py.test is supposed to be more
stable in the 2.x series.

regards Ronny
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20101220/56edb928/attachment.pgp>

More information about the Pytest-dev mailing list