Pytest: Monkeypatch + Session Scope

Created on 25 Aug 2016  路  6Comments  路  Source: pytest-dev/pytest

From the documentation

# content of conftest.py
@pytest.fixture(scope='session', autouse=True)
def enable_debugging(monkeypatch):
    monkeypatch.setenv("DEBUGGING_VERBOSITY", "4")

This auto-use fixture will set the DEBUGGING_VERBOSITY environment variable for the entire test session.

Note that the ability to use a monkeypatch fixture from a session-scoped fixture was added in pytest-3.0.

However when running pytest 3.0.1

============================================================ 
test session starts =============================================================
platform linux -- Python 3.5.2, pytest-3.0.1, py-1.4.31, pluggy-0.3.1
rootdir: /code, inifile: 
plugins: flask-0.10.0
collected 51 items 

This is the error thrown

ScopeMismatch: You tried to access the 'function' scoped fixture 'monkeypatch' with a 'session' scoped request object, involved factories
tests/conftest.py:6:  def local_es(monkeypatch)
../usr/local/lib/python3.5/site-packages/_pytest/monkeypatch.py:12:  def monkeypatch(request)

This is the fixture

   @pytest.fixture(scope="session", autouse=True)
   def local_es(monkeypatch):                                                                                                                                                                                                                                                                                                
       monkeypatch.setenv('ELASTICSEARCH_HOST', 'ES') 

Most helpful comment

What is the new way of monkey-patching in a session-scoped fixture? Do we have to patch and restore e.g. os.environ directly, can we use an instance of the MonkeyPatch class, or something else?

You can instantiate MonkeyPatch directly, or create a session scoped version for more general use:

@pytest.fixture(scope='session')
def monkeypatch_session():
    from _pytest.monkeypatch import MonkeyPatch
    m = MonkeyPatch()
    yield m
    m.undo()

But be aware that this may break in some feature release because it uses an internal API (_pytest.monkeypatch).

All 6 comments

Have tried without the flask plugin, on python 3.5 and 2.7+, and pytest versions 3.0.0 and 3.0.1

That is a unfortunate leftover, shortly before 3.0 we had to pull back on invocation scoped fixtures due to a regression

The plan is to bring them back in 3.1

Thanks for removing a misleading example. What is the new way of monkey-patching in a session-scoped fixture? Do we have to patch and restore e.g. os.environ directly, can we use an instance of the MonkeyPatch class, or something else?

currently not, so far we haven't been able to bring it back

What is the new way of monkey-patching in a session-scoped fixture? Do we have to patch and restore e.g. os.environ directly, can we use an instance of the MonkeyPatch class, or something else?

You can instantiate MonkeyPatch directly, or create a session scoped version for more general use:

@pytest.fixture(scope='session')
def monkeypatch_session():
    from _pytest.monkeypatch import MonkeyPatch
    m = MonkeyPatch()
    yield m
    m.undo()

But be aware that this may break in some feature release because it uses an internal API (_pytest.monkeypatch).

Was this page helpful?
0 / 5 - 0 ratings