September 2017
Intermediate to advanced
222 pages
5h 1m
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():
| | """Demonstrate fixture renaming.""" |
| | |
| | import pytest |
| | |
| | |
| | @pytest.fixture(name='lue') |
| | def ultimate_answer_to_life_the_universe_and_everything(): |
| | """Return ultimate answer.""" |
| | return 42 |
| | |
| | |
| | def test_everything(lue): |
| | """Use the shorter name.""" |
| | assert lue == 42 |
Here, lue is now the fixture name, instead of fixture_with_a_name_much_longer_than_lue. That name even shows up if we run it with --setup-show:
| | $ pytest --setup-show test_rename_fixture.py ... |
Read now
Unlock full access