February 2022
Intermediate to advanced
274 pages
6h 28m
English
Here’s a simple fixture that returns a number:
| | import pytest |
| | |
| | |
| | @pytest.fixture() |
| | def some_data(): |
| | """Return answer to ultimate question.""" |
| | return 42 |
| | def test_some_data(some_data): |
| | """Use fixture return value in a test.""" |
| | assert some_data == 42 |
The @pytest.fixture() decorator is used to tell pytest that a function is a fixture. When you include the fixture name in the parameter list of a test function, pytest knows to run it before running the test. Fixtures can do work, and can also return data to the test function.
You don’t need to have a complete understanding of Python decorators to use the decorators included with pytest. pytest uses decorators to add ...
Read now
Unlock full access