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

holger krekel holger at merlinux.eu
Mon Nov 19 19:38:34 CET 2012


Hi folks,

while writing tests on a new project using pytest-2.3 i noticed again an
inconvience: fixtures such as tmpdir or monkeypatch could implementation-wise
easily support being called from non-function scoped fixtures.  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 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.

If there are any questions or comments, please shoot.

best,
holger




More information about the Pytest-dev mailing list