[py-dev] Using funcargs (monkeypatch, tmpdir) during setup

Floris Bruynooghe flub at devork.be
Tue Jan 4 09:40:35 CET 2011


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



More information about the Pytest-dev mailing list