February 2022
Intermediate to advanced
274 pages
6h 28m
English
So far in this chapter, all of the fixtures used by tests were named by the tests or another fixture in a parameter list. However, you can use autouse=True to get a fixture to run all of the time. This works well for code you want to run at certain times, but tests don’t really depend on any system state or data from the fixture.
Here’s a rather contrived example:
| | import pytest |
| | import time |
| | |
| | |
| | @pytest.fixture(autouse=True, scope="session") |
| | def footer_session_scope(): |
| | """Report the time at the end of a session.""" |
| | yield |
| | now = time.time() |
| | print("--") |
| | print( |
| | "finished : {}".format( |
| | time.strftime("%d %b %X", time.localtime(now)) ... |
Read now
Unlock full access