Skip to content

Session level scoped fixture being run twice. #13144

Closed as not planned
Closed as not planned
@andyDoucette

Description

@andyDoucette

Please see this minimal example to reproduce it:

__init__.py:

#Add this directory to the path so we can import things in it directly.
import sys, os; sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

conftest.py:

from test_a import a_fixture

test_a.py:

import pytest
import os

@pytest.fixture(scope='session')
def a_fixture():
    print('Running a_fixture()...')
    has_run=os.environ.get('FIXTURE_HAS_RUN', 'False')=='True'

    print(f'\n{has_run=}')

    assert not has_run, \
        'a_fixture has already been run it should only run once.'

    # Mark the function as having been run already:
    os.environ['FIXTURE_HAS_RUN']='True'

def test_a(a_fixture):
    pass

test_b.py:

def test_b(a_fixture):
    pass

When I run pytest in this directory, I see that pytest is running the fixture twice, despite the fact that it has a session scope.

[root@opt app]$ pytest --version
pytest 8.3.4


[root@opt app]$ pytest -s --setup-show
============================================== test session starts ==============================================
platform linux -- Python 3.11.2, pytest-8.3.4, pluggy-1.5.0
rootdir: /optHome/app
collected 2 items                                                                                               

test_a.py Running a_fixture()...

has_run=False

SETUP    S a_fixture
        test_a.py::test_a (fixtures used: a_fixture).
test_b.py Running a_fixture()...

has_run=True

SETUP    S a_fixtureE
TEARDOWN S a_fixture
TEARDOWN S a_fixture

==================================================== ERRORS =====================================================
___________________________________________ ERROR at setup of test_b ____________________________________________

    @pytest.fixture(scope='session')
    def a_fixture():
        print('Running a_fixture()...')
        has_run=os.environ.get('FIXTURE_HAS_RUN', 'False')=='True'
    
        print(f'\n{has_run=}')
    
>       assert not has_run, \
            'a_fixture has already been run it should only run once.'
E       AssertionError: a_fixture has already been run it should only run once.
E       assert not True

test_a.py:11: AssertionError
============================================ short test summary info ============================================
ERROR test_b.py::test_b - AssertionError: a_fixture has already been run it should only run once.
========================================== 1 passed, 1 error in 0.01s ===========================================

In my application, this is causing duplicate database entries that are screwing up other parts of the system. Is there a bug in pytest?

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytype: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions