Pytest: Testing Python Code
Welcome, champion! 🔥
You've mastered 0 of 87 lessons. Start your first lesson free — no signup needed.
About this course
Most Python developers learn pytest one Stack Overflow answer at a time, and end up with tests that are slow, flaky or so heavily mocked that they prove little. This online pytest course on Abekus teaches the framework properly, starting from a first passing and failing test and building up to assertions, fixtures, parametrisation, markers, mocking, configuration and plugins, coverage and test design, in short reading-card lessons each followed by an MCQ quiz.
Who this pytest course is for
It is written for Python developers who write few or no automated tests today: students with a project or two behind them, backend engineers and data engineers. You should be comfortable with Python functions, classes, modules and exceptions; the course does not teach Python itself. By the end you can write readable tests with fixtures, parametrisation, markers and mocks, measure coverage, configure a project and run the suite in GitHub Actions.
Examples follow one modern setup: pytest 9 on Python 3.10 or later, test files named test_*.py with plain functions and plain assert, configuration only in pyproject.toml, tmp_path for temporary files, capsys and caplog for output and logs, and mocking with monkeypatch, the standard library's unittest.mock and the mocker fixture from pytest-mock. The plugins taught are pytest-cov, pytest-mock, pytest-xdist, pytest-asyncio and pytest-randomly. The course leaves out unittest.TestCase-style tests, property-based testing with Hypothesis, tox and nox, browser testing, BDD plugins, framework-specific plugins such as pytest-django, and publishing your own plugin.
How the lessons work
Each lesson is a short set of reading cards on one feature or practice, usually with a short code example and the output pytest prints, followed by a quiz of multiple-choice questions that often ask what a test run will report. Lessons go from basics up: first tests and running them, then assertions and failure outcomes, fixtures, parametrisation, markers and selection, mocking and isolation, configuration, hooks and plugins, coverage and reports, test design, and finally comparisons and worked cases. You move at your own pace. Every lesson works from code alone; diagrams such as fixture scopes or a patch-target lookup appear only where they help.
MCQ practice vs video courses for pytest
Video courses on Udemy, Coursera or YouTube are a good choice if you want to watch someone set up a test suite in an editor from scratch. This course is built for a different job: a short explanation followed straight away by questions about real test code, such as which fixture runs first, how many test ids a stacked parametrize produces, whether a patch targets the right name or whether a strict xfail that passes fails the run. pytest's behaviour is precise, and predicting it question by question builds the mental model that saves debugging time later. Many developers use both: a video for a first walkthrough, and questions to make the details stick.
Common pytest mistakes
The course corrects these directly:
- Patching where a function is defined. A mock must patch the name where the code under test looks it up.
- Using a session-scoped fixture for everything. Wider scopes save time but share state between tests, and a wider-scoped fixture cannot request a narrower one.
- Looping over cases inside one test. Parametrize gives each case its own id and its own pass or fail.
- Treating skip and xfail as the same. Skip means the test should not run; xfail records a known failure, and strict xfail fails the run when the test unexpectedly passes.
- Comparing floats with ==. pytest.approx compares with a tolerance.
- Chasing 100% coverage. High coverage with weak assertions still misses bugs, and branch coverage shows paths that line coverage hides.
- Mocking every dependency. Over-mocked tests pass while real behaviour breaks.
Best way to learn pytest
Write tests for code you already have. Start with one behaviour per test in arrange, act, assert order, then pull repeated setup into fixtures, repeated cases into parametrize and slow or external calls behind monkeypatch or a mock. The last part of the course is built around these choices: fixture versus setup method, monkeypatch versus mock.patch, when to use session-scoped fixtures and when to use real objects, applied to testing a Flask API endpoint, a pandas data-cleaning function, code that calls a payment API, and running the suite in GitHub Actions.