Posts

Showing posts with the label fixture

How to parametrize fixtures in pytest


How to parametrize fixtures in pytest

Parametrization of arguments in test functions can be achieved with the built-in pytest.mark.parametrize decorator. Maybe a bit less known is the fact that pytest fixtures can be parametrized as well. pytest.fixture accepts a params argument, a list of parameters. A fixture with params will be executed multiple times, one time for each parameter in params. And each time a fixture is executed all the dependent tests (the ones that use that fixture) will be executed as well.

In this example, test_dict is a fixture that is parametrized with 2 fixtures, min_dict and full_dict, meaning the test_dict fixture will be executed two times and therefore also our test_instantiation will be executed twice due to the fixture. On top of that, we have a parametrization of the arguments indentation and expected_linebreak in test_instantiation. So in total, our test_indentation will be called 4 times.

Github gist with code

dependencies: python3.9pytest==7.1.2