Dillon Miller
Dillon Miller

Reputation: 1113

Pytest: Inherit fixture parameters

I have a fixture that acts as a switch to parameterize tests to run in two states (online/offline). This additionally applies custom marks to the test if the test is in offline mode.

@pytest.fixture(
    params=['online', pytest.param('offline', marks=pytest.mark.jira('388', '828', '833', '918'))]
)
def network(request):
    """ A switch parameter that parametrizes test for testing online and offline functionality. """
    return request.param

I have a test that I wish to append additional paramters to if the test is running in offline mode as well. Since I'm not using the network fixture, the marks are not included (but I want them to be).

@pytest.mark.smoke
@pytest.mark.jira('387', '772', '1009')
@pytest.mark.parametrize('network', ['online', pytest.param('offline', marks=pytest.mark.jira('1036'))])
def test_crud(network):
    ...

My question is, how do I apply both the fixture marks and @pytest.mark.parametrize marks to the test?

Upvotes: 0

Views: 524

Answers (1)

Dillon Miller
Dillon Miller

Reputation: 1113

I solved this by adding

def pytest_runtest_setup(item):
    if item.callspec.params.get('network') == 'offline':
        item.add_marker(pytest.mark.jira('388', '828', '833', '918'))

to conftest.py and removing the network fixture in favor of adding @pytest.mark.parametrize('network', ...) to each test.

Upvotes: 1

Related Questions