Monkeypatch + Session Scope
See original GitHub issueFrom 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:
- Created 7 years ago
- Comments:7 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can instantiate
MonkeyPatch
directly, or create a session scoped version for more general use:But be aware that this may break in some feature release because it uses an internal API (
_pytest.monkeypatch
).One can also use pytest-mock’s
session_mocker
: https://github.com/pytest-dev/pytest-mock