Skip to content

Session scoped fixture run multiple times when adding fixture via metafunc #5738

Closed
@Drau

Description

@Drau

pytest v5.0.1

(All code can be found here: example repo)

When defining a session scoped fixture inside pytest_generate_tests hook,
while also using metafunc.parametrize to make all tests repeat N times,
Every fixture that uses the newly created job_id fixture, is called N times (Once before every test iteration)

conftest.py

import pytest

def pytest_generate_tests(metafunc):
      # Make all tests repeat 4 times
      metafunc.fixturenames.append('repeat')
      metafunc.parametrize('repeat', range(1,5))
      # Create a new session scoped fixture
      metafunc.parametrize("job_id", [100], scope='session')

@pytest.fixture(scope='session', autouse=True)
def fix1(job_id):
    print('IN session scope'

test_1.py

import pytest

def test_1():
    assert True

def test_2():
    assert True

Result: fix1 fixture is called, then the 2 tests are called, then again fix1 and so on...

python -m pytest -s
=== test session starts ===
platform darwin -- Python 3.7.3, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /Users/michaelr/tmp/issue
collected 8 items

test_1.py IN session scope
..IN session scope
..IN session scope
..IN session scope
..

=== 8 passed in 0.03 seconds ===

I would expect that all session scoped fixtures will run only once.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytopic: parametrizerelated to @pytest.mark.parametrize

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions