February 2022
Intermediate to advanced
274 pages
6h 28m
English
The third way to parametrize is by using a hook function called pytest_generate_tests. Hook functions are often used by plugins to alter the normal operation flow of pytest. But we can use many of them in test files and conftest.py files.
Implementing the same flow as before with pytest_generate_tests looks like this:
| | from cards import Card |
| | |
| | |
| | def pytest_generate_tests(metafunc): |
| | if "start_state" in metafunc.fixturenames: |
| | metafunc.parametrize("start_state", ["done", "in prog", "todo"]) |
| | |
| | |
| | def test_finish(cards_db, start_state): |
| | c = Card("write a book", state=start_state) |
| | index = cards_db.add_card(c) |
| | cards_db.finish(index) |
| | card = cards_db.get_card(index) ... |
Read now
Unlock full access