pytest in practice: fixtures, parametrize, mocking, coverage
12 steps · about 5 h 40 min · Intermediate
- Get Started - pytest documentation (doc, pytest docs, 15 min)
The official quick start shows how to install pytest, write a first test, and read a failure report.
- How to Use pytest: A Simple Guide to Testing in Python (article, freeCodeCamp, 20 min)
It teaches how to run single tests, filter with -k and read pytest's output, which you need before fixing failing tests.
- Pytest Tutorial – How to Test Python Code (video, freeCodeCamp.org (YouTube), 2 h)
A two-hour course that walks through first tests, fixtures, marks, parametrize and mocking, so you see the whole workflow once.
- How to write and report assertions in tests - pytest documentation (doc, pytest docs, 15 min)
It covers pytest.raises and pytest.approx, which handle the exception and float checks that plain assert can't.
- About fixtures - pytest documentation (doc, pytest docs, 15 min)
It explains what fixtures are and how they depend on each other, before you use them in practice.
- How to use fixtures - pytest documentation (doc, pytest docs, 30 min)
It covers yield teardown, scopes, autouse and factories, the fixture patterns real test suites rely on.
- Pytest Parametrize: The Complete Guide for 2026 (with Examples) (guide, QASkills, 20 min)
It shows how to replace repeated tests with one parametrized test, including readable ids and indirect parametrization.
- Parametrizing tests - pytest documentation (doc, pytest docs, 20 min)
The official examples extend parametrize to mark individual cases and combine it with fixtures.
- Mocking Vs. Patching (A Quick Guide For Beginners) (article, Pytest with Eric, 20 min)
It makes the difference between a mock and a patch clear before you fake anything in your own tests.
- Using Mocks in Tests - OxRSE Training - University of Oxford (tutorial, University of Oxford RSE, 30 min)
A university exercise that compares unittest.mock, the monkeypatch fixture and pytest-mock, so you can choose between them.
- Coverage - Scientific Python Development Guide (guide, Scientific Python Development Guide, 20 min)
It shows how to measure what your tests cover, using coverage directly or through pytest-cov.
- Configuration - pytest-cov documentation (doc, pytest-cov docs, 15 min)
The last step is learning the options that make coverage useful, such as --cov-branch and --cov-fail-under.