pytest-dev / pytest-dev/pytest

pytest_fixture_setup hook doesn't run for session scoped fixture

Open
#8,000 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: fixtures type: bug
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior with test/conftest.py and test/test_1.py, comparing function- and session-scoped fixtures. Start at the fixture manager hook-proxy call shown in the report and trace how pytest_fixture_setup is selected for each scope. Done means the hook runs for the session-scoped fixture while the example test still passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.