question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Monkeypatch + Session Scope

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

41reactions
nicoddemuscommented, Mar 21, 2018

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

0reactions
cbensimoncommented, Aug 17, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using monkeypatch in non function scoped fixture #363
Hi, I'd like to be able to create a module or session scoped fixture that uses monkeypatch. #!python @pytest.fixture(scope='session') def usermanager( ...
Read more >
Python monkeypatch.setattr() with pytest fixture at module scope
fixture(scope='module') , I get the following error. ScopeMismatch: You tried to access the 'function' scoped fixture 'monkeypatch' with a ' ...
Read more >
How to monkeypatch/mock modules and environments - Pytest
Use monkeypatch.context to apply patches only in a specific scope, which can help control teardown of complex fixtures or patches to the stdlib....
Read more >
Efficient use of pytest fixtures
Built-in monkeypatch fixture lets you e.g. set environment variables and ... @pytest.fixture(scope='session') def session_fixture(): print('\nsession').
Read more >
using tmpdir/monkeypatch/... from non-function scopes
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...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found