Using Indirect Parametrization

The final parametrization technique we’re going to look at is indirect parametrization. An indirect parameter is one that gets passed to a fixture before it gets sent to the test function. Indirect parametrization allows us to perform work based on the parameter value.

The way it works is to set indirect to a list of parameter names you want to be indirect, like indirect=["param1", "param2"]. You can also set indirect=True if you want all parameters to be indirect. Then you need a fixture with the same name as the parameter.

As an example, let’s say we have expanded Cards to have different access rights for different user roles. We can parametrize a test with a user parameter:

 @pytest.mark.parametrize( ...

Get Python Testing with pytest now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.