February 2022
Intermediate to advanced
274 pages
6h 28m
English
Let’s say we have the fixture setup as we do now, with db at session scope and cards_db at function scope, but we’re worried about it. The cards_db fixture is empty because it calls delete_all(). But what if we don’t completely trust that delete_all() function yet, and want to put in place some way to completely set up the database for each test function?
We can do this by dynamically deciding the scope of the db fixture at runtime. First, we change the scope of db:
| | @pytest.fixture(scope=db_scope) |
| | def db(): |
| | """CardsDB object connected to a temporary database""" |
| | with TemporaryDirectory() as db_dir: |
| | db_path = Path(db_dir) |
| | db_ = cards.CardsDB(db_path) |
| | yield db_ |
| | db_.close() ... |
Read now
Unlock full access