Skip to content

Class scope fixture not working with indirect parametrization since 5.4.0 #9198

Open
@palocziarpad

Description

@palocziarpad

Pytest - 5.4.0

pytest_version_test/
├── conftest.py
├── __init__.py
└── test_a.py

conftest.py

import pytest

def pytest_addoption(parser):
    parser.addoption("--indirect_params", action="store", default=None)

def pytest_generate_tests(metafunc):
    if 'indirect_param' in metafunc.fixturenames:
        alarms = metafunc.config.getoption("--indirect_params").split(',')
        metafunc.parametrize("indirect_param", alarms, indirect=True, scope='session')

@pytest.fixture(scope='session')
def indirect_param(request):
    return request.param

@pytest.fixture(scope='session', autouse=True)
def ses_fix():
    print('session fixture start')
    yield
print('session fixture end')

@pytest.fixture(scope='package', autouse=True)
def pak_fix():
    print('package fixture start')
    yield
print('package fixture end')

@pytest.fixture(scope='module', autouse=True)
def mod_fix():
    print('module fixture start')
    yield
print('module fixture end')

@pytest.fixture(scope='function', autouse=True)
def func_fix():
    print('function fixture start')
    yield
print('function fixture end')

test_a.py

import pytest

@pytest.fixture(scope='class')
def cls_fix(indirect_param):
    print(f'class fixture start with param {​​​​​​​​​indirect_param}​​​​​​​​​')
    yield
print(f'class fixture ends with param {​​​​​​​​​indirect_param}​​​​​​​​​')

@pytest.fixture(scope='class')
def another_cls_fix():
    print(f'Another class fixture start')
    yield
print(f'Another class fixture ends ')


class TestA:
    def test_a1(self, cls_fix):
        assert True
    def test_a2(self, cls_fix):
        assert True
    def test_a3(self, cls_fix):
        assert True
class TestB:
    def test_b1(self, another_cls_fix):
        assert True
    def test_b2(self, another_cls_fix):
        assert True
    def test_b3(self, another_cls_fix):
        assert True

Incorrect behavior

Expectation is that the "class fixture start with param ABC" written out only once.

*****Output from TestA****************
session fixture start
package fixture start
module fixture start
class fixture start with param ABC
function fixture start
PASSED                                    [ 11%]function fixture end
class fixture ends with param ABC
class fixture start with param ABC
function fixture start
PASSED                                    [ 22%]function fixture end
class fixture ends with param ABC
class fixture start with param ABC
function fixture start
PASSED                                    [ 33%]function fixture end
class fixture ends with param ABC
class fixture start with param DEF
function fixture start
PASSED                                    [ 44%]function fixture end
class fixture ends with param DEF
class fixture start with param DEF
function fixture start
PASSED                                    [ 55%]function fixture end


test_a.py::TestA::test_a3[DEF] class fixture ends with param DEF
class fixture start with param DEF
function fixture start
PASSED                                    [ 66%]function fixture end
class fixture ends with param DEF

Without the indirect parametization:
The indirect parametization version should work in a similar way.


*****Output from TestB****************


Another class fixture start
function fixture start
PASSED                                         [ 77%]function fixture end
function fixture start
PASSED                                         [ 88%]function fixture end
function fixture start
PASSED                                         [100%]function fixture end
Another class fixture ends 
module fixture end
package fixture end
session fixture end

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytopic: parametrizerelated to @pytest.mark.parametrizetype: regressionindicates a problem that was introduced in a release which was working previously

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions