[py-dev] Using funcargs (monkeypatch, tmpdir) during setup
Virgil Dupras
hsoft at hardcoded.net
Tue Jan 4 10:58:51 CET 2011
On 2011-01-04, at 9:40 AM, Floris Bruynooghe wrote:
> Hello Virgil
>
> On 2 January 2011 16:25, Virgil Dupras <hsoft at hardcoded.net> wrote:
>> I'm wondering what's the common pattern of pytest users regarding the use of monkey-patching and temporary directories in setup functions. I already have my own "home-brewed" (again!) mechanism for that, but I want to "pytest-ify" the way I write tests.
>>
>> So far, the only solution I can come up with is to have all tests use the funcarg(s) and pass them to the setup function when they're called. Example:
>>
>> def my_setup(monkeypatch, tmpdir):
>> moneypatch.setattr(module, 'something', foo)
>> tmpdir.mkdir('bar')
>
> I usually do this by creating a new funcarg for the required setup:
>
> def pytest_funcarg__my_setup(request):
> monkey = request.getfuncargvalue(monkeypatch)
> monkey.setattr(module, 'something', foo)
> tmpdir = request.getfuncargvalue(tmpdir)
> return tmpdir.mkdir('bar')
>
> def test1(my_setup):
> pass
>
> This seems a fairly neat solution to me.
>
> Regards
> Floris
>
> --
> Debian GNU/Linux -- The Power of Freedom
> www.debian.org | www.gnu.org | www.kernel.org
I didn't know about request.getfuncargvalue(). Yup, that seems like a nice way to solve my problem, thanks!
Quick note though (for other people reading): I made a quick test and the argument to getfuncargvalue() is a string, so the correct way to call it is request.getfuncargvalue('monkeypatch').
Thanks again,
Virgil
More information about the Pytest-dev
mailing list