Introduction to parametrization

Test parametrization is a feature in pytest that allows us to write a single test function and test it with multiple input values. It is a way of writing concise tests that can cover many cases with minimal code. This feature is especially useful when we want to test a function with varying input parameters.

The main benefit of this is concise code. Instead of writing many separate test functions with the same code, we can write a single test function with parameters, which makes the test code more concise and easier to manage. Testing code with a variety of inputs can increase the coverage of the tests and help us find bugs more easily. This also makes test code easier to maintain in the future.

In the following sections, we will explore how to use test parametrization in pytest.

Basic test parametrization

To use test parametrization in pytest, we can use the @pytest.mark.parametrize decorator. This decorator takes the name of the parameterized argument(s) as a string or list, followed by the list of values to use for each parameter.

Get hands-on with 1200+ tech skills courses.