Advanced testing with pytest
Intermediate
Developer
Student
Visual Studio Code
Use parametrization, fixtures, and the monkeypatch fixture to write pytest tests with reusable setup and isolated state.
Learning objectives
By the end of this module, you'll be able to:
- Use
@pytest.mark.parametrizeto run one test function with multiple input values. - Create reusable fixtures to share setup and cleanup across tests.
- Explain pytest fixture scopes, use
yield-based teardown, and share fixtures throughconftest.py. - Use the built-in
monkeypatchfixture to safely replace functions, values, environment variables, and paths during tests.
Prerequisites
- Ability to write Python scripts
- Familiarity with command-line tools and executing scripts
- Basic familiarity with writing and running pytest tests, including test discovery and assertions
- Python 3.10 or later, which matches the minimum supported version for pytest 9.x, and pytest installed in a virtual environment. For Windows setup, see Set up your Python development environment on Windows. For Python packaging and pytest setup, see Python's Installing Python modules and pytest's getting started guide.