Hello Holger,

On 19 November 2012 18:38, holger krekel <hol...@merlinux.eu> wrote:
> But currently if you do::
>
>     @pytest.fixture(scope="module")
>     def something(monkeypatch):
>         ...
>
> you get a ScopeMismatchError because the function-scoped monkeypatch
> fixture cannot be called from a module-scoped fixture.

I agree this is a problem, I've worked around this using a hack in
pytest-django for the live_server fixture:
https://github.com/flub/pytest_django/blob/pytest23/pytest_django/fixtures.py#L180.
 In particular see the helper autouse fixture defined just below the
fixture.  For builtin fixtures I believe it is still possible to split
up the session-scoped fixture into a session- and a function-scoped
part to hack around this.  It is ugly though.

>  I am considering
> introducing an "any" scope for a fixture declaration that would avoid
> this error.  The "monkeypatch" and "something" fixture would then look
> like this::
>
>     @pytest.fixture(scope="any")
>     def monkeypatch(...):
>         # unmodified builtin monkeypatch implementation
>
>     @pytest.fixture(scope="module")
>     def something(monkeypatch):
>         ...
>
> This would not raise a ScopeMismatchError but just work:
> monkeypatch-finalizers would be called when the last test in a module
> using the "something" fixture has run.
>
> However, if we additionally have a function-scoped fixture::
>
>     @pytest.fixture(scope="function")
>     def other(monkeypatch):
>         ...
>
> The "monkeypatch" instance could obviously not be the same object as
> the one in ``something(monkeypatch)`` above.  monkeypatch-finalizers
> would raher be called after a test function using the "other"
> fixture has finalized.  I am not sure if there is confusion potential
> about this.

For monkeypatch this would not be too bad as you can have two
instances which don't cause harm to each other.  But what happens with
e.g. tmpdir?  How can you avoid a temporary directory being created at
the session-level and then later one at the function level?  It's
almost as the any-scoped fixture needs to be able to specify how
things should be handled when it's requested from different scopes.

Regards,
Floris


--
Debian GNU/Linux -- The Power of Freedom
www.debian.org | www.gnu.org | www.kernel.org
_______________________________________________
py-dev mailing list
py-dev@codespeak.net
http://codespeak.net/mailman/listinfo/py-dev

Reply via email to