Skip to content

Monkeypatch + Session Scope #1872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mlcamilli opened this issue Aug 25, 2016 · 7 comments · Fixed by #1876
Closed

Monkeypatch + Session Scope #1872

mlcamilli opened this issue Aug 25, 2016 · 7 comments · Fixed by #1876

Comments

@mlcamilli
Copy link

mlcamilli commented Aug 25, 2016

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') 
@mlcamilli
Copy link
Author

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

@RonnyPfannschmidt
Copy link
Member

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

@RonnyPfannschmidt
Copy link
Member

The plan is to bring them back in 3.1

nicoddemus added a commit to nicoddemus/pytest that referenced this issue Aug 26, 2016
This is a leftover when invocation-scoped fixtures
were pulled back.

Fix pytest-dev#1872
@merwok
Copy link
Contributor

merwok commented Mar 20, 2018

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?

@RonnyPfannschmidt
Copy link
Member

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

@nicoddemus
Copy link
Member

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).

@cbensimon
Copy link

One can also use pytest-mock's session_mocker : https://github.com/pytest-dev/pytest-mock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants