pytest
pytest copied to clipboard
pytest_fixture_setup hook doesn't run for session scoped fixture
test\conftest.py
import pytest
@pytest.fixture(scope='function')
# @pytest.fixture(scope='session')
def fixture_1():
return None
@pytest.hookimpl(tryfirst=True)
def pytest_fixture_setup(fixturedef, request):
print('Fixture setup: ', fixturedef.argname)
test/test_1.py
def test_1(fixture_1):
assert 1
If we run pytest test we get
$ pytest test -s
============================= test session starts ==============================
platform linux -- Python 3.6.9, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: /home/voronin/projects/sintez_addons, inifile: setup.cfg
collected 1 item
test/test_1.py Fixture setup: fixture_1
.
============================== 1 passed in 0.01s ==============================
If we change fixture_1 scope to session there is no hook running.
$ pytest test -s
============================= test session starts ==============================
platform linux -- Python 3.6.9, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: /home/voronin/projects/sintez_addons, inifile: setup.cfg
collected 1 item
test/test_1.py .
============================== 1 passed in 0.01s ===============================
IMHO problem in this code
hook = self._fixturemanager.session.gethookproxy(request.node.fspath)
In function scope case:
request.node.fspath == '/home/voronin/projects/sintez_addons'
hook is <_pytest.main.FSHookProxy object at 0x7f9200a69250>
In session scope case:
request.node.fspath == '/home/voronin/projects/sintez_addons/test/test_1.py'
hook is <pluggy.hooks._HookRelay object at 0x7f10c2952710>
I see, it's quite tricky to decide whether this is a feature or a bug
But im leaning towards bug since the location should matter
I agree, but not sure this is fixable unfortunately... the session fixture is attached to the Session node, which is in the rootdir, so it won't pick up that conftest.py... sigh.
A workaround is to change your rootdir to the test directory by creating a test/pytest.ini file in there.
This hook not working with the package scope either.
imho, sounds like a feature. Separate fixture from hook.
test/conftest.py # fixtures
conftest.py # hooks
pytest.ini