Learn pytest by catching real bugs: a hands-on course
Learn pytest from your first assert to fixtures, mocking and a full shop test suite. Real pytest runs in your browser, and your tests must catch planted bugs.
Most pytest tutorials stop at assert add(1, 2) == 3. Then you open a real codebase: a payment
provider to mock, files to read, dates that change every day, a fixture you can't find. And you
realise you know the syntax, not the craft.
Mastering pytest is built around one idea: a test is only as good as the bugs it catches.
How the course works
You join Brightcart, an online shop in Amsterdam, as a test automation engineer. Every exercise gives you a piece of Brightcart's real code (a cart, a discount engine, a payment processor) and asks you to test it.
Then your tests are graded the way a senior engineer would:
- They run against the real code, and must all pass.
- They run against copies of the code with planted bugs: an off-by-one boundary, a discount applied twice, a refund that forgets the shipping. Your tests must fail on each one.
If a planted bug slips through, you're told which kind of bug you missed, not where it is. Find the test that's missing, and run it again. This is mutation testing, the honest way to measure a test suite, and it teaches you to think like a tester: where would this code break?
Everything runs in your browser on real pytest 8: press Run to see pytest's own output, with its assertion details and tracebacks, and Check to be graded. Nothing to install.
What you'll learn
1. Your first tests. What a test is, how pytest finds and runs them, reading a failure, and the outcomes: passed, failed, error, skipped.
2. Effective unit tests. Testing functions and classes, edge cases, exceptions with
pytest.raises, Arrange-Act-Assert, and a test suite for a bank account.
3. Fixtures. @pytest.fixture, scopes, yield teardown, fixtures that use fixtures,
conftest.py, parametrized and autouse fixtures, and a staff library project.
4. Parametrization and pytest's toolkit. parametrize, pytest.param and ids, custom
markers, skip, skipif and strict xfail, tmp_path, the command line, and a CSV importer.
5. Mocking. Test doubles, Mock and MagicMock, side_effect for retries, patch (and
where to patch), monkeypatch, time and randomness, and a payment processor.
6. Files, data and business logic. JSON configuration, CSV reports, dates and leap years, discount rules, state machines, and inventory with reservations.
7. Professional practice. Fixture factories, coverage and its limits, flaky tests, when not to mock, debugging fixture errors, hooks and plugins, and rescuing a messy test suite.
8. Capstone. Ten milestones build a complete test suite for Brightcart's shop: users, catalogue, cart, discounts, orders and payments, ending in one final suite that has to catch bugs planted across the whole shop.
Each module ends with a knowledge check, and the course with a final quiz and a certificate.
Who it's for
Python developers who can write functions and classes and want to write tests that hold up in a real job: juniors preparing for their first role, developers whose team "should really have more tests", and anyone who's been burned by a bug a five-line test would have caught.
Start
Mastering pytest: Beginner to Advanced is part of Ascent. If you're new to pytest, read Testing with pytest first for a quick tour, then dive into fixtures and mocking.
Questions and answers
- Do I need to install pytest for the course?
- No. Every exercise runs real pytest 8 on our servers: write your tests in the browser, press Run to see pytest's output, and Check to have them graded.
- How are the tests I write graded?
- Your tests run against the real code and must all pass. Then they run against copies of the code with planted bugs, and must fail on each one. A test suite that can't tell working code from broken code doesn't pass.
- What do I need to know before starting?
- Python basics: functions, classes, lists and dictionaries, and exceptions. Python Intermediate on Devventura covers everything you need.
- Is there a certificate?
- Yes. Finish the lessons and the capstone, then pass the 15-question final quiz to get a certificate with a link anyone can verify.
Practise it
Read next
- pytest fixtures explained: scope, yield, conftest.py and factories
How pytest fixtures work: requesting them, scopes, yield teardown, conftest.py, parametrized and autouse fixtures, and factory fixtures, with real examples.
- Mocking in pytest: Mock, patch and monkeypatch explained
Test code that calls payment providers, email, the clock or randomness: Mock, MagicMock, side_effect, where to patch, monkeypatch, and when not to mock.
- Testing with pytest: run, read and write Python tests
How to run pytest, read a failing test, select tests with markers and -k, share setup with fixtures, cover many cases with parametrize, and test exceptions.