Module 08 / Project 03 — Fixtures Advanced¶
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | — |
Focus¶
conftest.pyfor sharing fixtures across test files- Fixture scopes:
function,module,session tmp_pathfor temporary file operations in testsmonkeypatchfor safely modifying environment variables and attributes
Why this project exists¶
Real applications read configuration from environment variables, process files on disk, and combine multiple components. Testing these requires setup and teardown that would be tedious to repeat in every test function. Pytest fixtures let you define reusable setup code that runs automatically before each test. conftest.py lets you share fixtures across multiple test files. tmp_path gives you a fresh temporary directory for each test, and monkeypatch lets you safely override environment variables without affecting other tests.
Run¶
Expected output¶
tests/test_processor.py::test_load_config_reads_env_vars PASSED
tests/test_processor.py::test_load_config_uses_defaults PASSED
tests/test_processor.py::test_process_file_uppercase PASSED
tests/test_processor.py::test_process_file_strips_whitespace PASSED
tests/test_processor.py::test_process_file_missing_file PASSED
tests/test_processor.py::test_save_results_creates_file PASSED
tests/test_processor.py::test_save_results_content PASSED
tests/test_processor.py::test_full_pipeline PASSED
All tests should pass. Each test gets its own temporary directory and environment, so they never interfere with each other.
Alter it¶
- Add a new fixture in
conftest.pythat creates a temporary directory with three text files of different sizes. Use it in a new test. - Change the
sample_configfixture to usescope="module"instead of the defaultscope="function". Run with-vand notice the setup/teardown happens less often. - Add a
monkeypatchfixture that setsAPP_DEBUG=trueand verify thatload_configreads it.
Break it¶
- Remove
conftest.pyand run the tests. What errors do you get? Why? - Create two fixtures with the same name in different files and see which one wins.
- Use
monkeypatch.setenvin a test, then check the environment variable in a different test. Is it still set? Why or why not?
Fix it¶
- Put
conftest.pyback and verify all tests pass again. - Rename one of the duplicate fixtures to avoid the conflict.
- Understand why
monkeypatchchanges are automatically undone after each test (that is the whole point).
Explain it¶
- What is
conftest.pyand why is it special to pytest? - What is the difference between
scope="function"andscope="session"for a fixture? - Why is
tmp_pathbetter than creating your own temporary directory manually? - How does
monkeypatchdiffer from directly settingos.environ?
Mastery check¶
You can move on when you can:
- Create a
conftest.pywith shared fixtures from memory. - Explain the difference between function, module, and session scope.
- Use
tmp_pathto test code that reads and writes files. - Use
monkeypatchto test code that depends on environment variables.