[pytest-dev] [py-dev] using tmpdir/monkeypatch/... from non-function scopes

Tom Viner tom at viner.tv
Mon Feb 23 18:58:16 CET 2015


Ok thanks Holger, sounds like a good plan.

I found I could work around the problem by implementing my own session
versions like this:

@pytest.fixture(scope='session')
def tmpdir_session(request):
    # code copied from _pytest/tmpdir.py
    """return a temporary directory path object
    which is unique to each test function invocation,
    created as a sub directory of the base temporary
    directory.  The returned object is a `py.path.local`_
    path object.
    """
    name = request.node.name
    name = re.sub("[\W]", "_", name)
    MAXVAL = 30
    if len(name) > MAXVAL:
        name = name[:MAXVAL]
    x = request.config._tmpdirhandler.mktemp(name, numbered=True)
    return x

@pytest.yield_fixture(scope='session')
def monkeypatch_session(request):
    from _pytest.monkeypatch import monkeypatch
    mp = _monkeypatch()
    yield mp
    request.addfinalizer(mp.undo)


Do you see any inherent problem with these?



On 23 February 2015 at 17:40, holger krekel <holger at merlinux.eu> wrote:

> On Mon, Feb 23, 2015 at 16:48 +0000, Tom Viner wrote:
> > Re this thread from 2012:
> > https://mail.python.org/pipermail/pytest-dev/2012-November/002157.html
> >
> > Was there ever any resolution or workaround to using monkeypatch / tmpdir
> > with scopes other than function?
> >
> > Example:
> >
> >     @pytest.fixture(scope="module")        def something(monkeypatch):
> >            ...
> > you get a ScopeMismatchError
>
> The plan is to introduce a new scope for fixture functions
> but it's not done yet.  For now, you can create a tempdir
> fixture using the canonical tempdir std library module.
>
> best,
> holger
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20150223/27522d0a/attachment.html>


More information about the pytest-dev mailing list