Deep dive into Pytest parametrization

submited by
Style Pass
2024-11-23 21:30:04

Pytest is an amazing testing framework for Python. In this article I will focus on how fixture parametrization translates into test parametrization in Pytest.

Pytest has two nice features: parametrization and fixtures. They serve completely different purposes, but you can use fixtures to do parametrization.

A fixture is a function, which is automatically called by Pytest when the name of the argument (argument of the test function or of the another fixture) matches the fixture name. In another words:

In this example fixture1 is called at the moment of execution of test_foo. The return value of fixture1 is passed into test_foo as an argument with a name fixture1. There are many, many nuances to fixtures (e.g. they have scope, they can use yield instead of return to have some cleanup code, etc, etc), but in this chapter I focus on a single powerful feature, a possible argument params to the pytest.fixture decorator. It is used for parametrization.

Roughly speaking, parametrization is a process of varying (changing) one or more coefficients in a mathematical equation. In the context of testing, parametrization is a process of running the same test with different values from a prepared set. Each combination of a test and data is counted as a new test case.

Leave a Comment