February 2022
Intermediate to advanced
274 pages
6h 28m
English
The name of a fixture, listed in the parameter list of tests and other fixtures using it, is usually the same as the function name of the fixture. However, pytest allows you to rename fixtures with a name parameter to @pytest.fixture():
| | import pytest |
| | |
| | |
| | @pytest.fixture(name="ultimate_answer") |
| | def ultimate_answer_fixture(): |
| | return 42 |
| | |
| | |
| | def test_everything(ultimate_answer): |
| | assert ultimate_answer == 42 |
I’ve run across a few examples where renaming is desirable. As in this example, some people like to name their fixtures with a _fixture suffix or fixture_ prefix or similar.
One instance where renaming is useful is when the most obvious fixture name already exists as an existing ...
Read now
Unlock full access