September 2017
Intermediate to advanced
222 pages
5h 1m
English
So far in this chapter, all of the fixtures used by tests were named by the tests (or used usefixtures for that one class example). 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:
| | """Demonstrate autouse fixtures.""" |
| | |
| | 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( ... |
Read now
Unlock full access