Testing Python with pytest
Write tests that catch bugs before your users do: assert, pytest, fixtures, parametrize, mocking, coverage and TDD, on a mobile money wallet.
- 0 students
- 90 lessons
- 6 h 25 min
Created by Ayim Nelson
What you will learn
- Decide what to test: normal cases, edges and errors
- Install pytest, run it and choose tests with -v, -k and -x
- Read pytest's failure reports and fix the real cause
- Organise tests in files, classes and folders that pytest discovers
- Test exceptions and decimal results with pytest.raises and approx
- Remove repetition with parametrize and fixtures, including conftest.py
- Use tmp_path, capsys, monkeypatch, marks and mocks to test real-world code
- Measure coverage, work test-first and run tests in CI
About this course
Tests are small programs that check your code does what you meant, every time you change it. This course teaches testing with pytest, the most popular Python test tool, through one running example: Ama's mobile money wallet, with its transfer fees, balances, SMS alerts and statements. You'll learn what to test, how to run and read pytest's output, and the tools that keep tests short: fixtures, parametrize, marks, mocking and coverage.
For: anyone who has finished the Python Crash Course (or knows functions, classes, exceptions and modules) and wants to stop testing by hand. Every pytest run in the lessons is real output; the exercises run in your browser.
By the end: you'll have a tested wallet module and statement app, you'll know how to read any pytest failure, and you'll be able to work test-first and run your tests automatically on every push.
Course content
11 sections · 90 lessons
Why test?8 lessons
- Why test your codePreviewReading · 3 min
- What a test is: assertPreviewReading · 3 min
- Writing test functionsPreviewReading · 3 min
- What to test: normal casesReading · 3 min
- What to test: edges and boundariesReading · 3 min
- What to test: errors and bad inputReading · 3 min
- What makes a good testReading · 3 min
- Catch the buggy fee functionsExercise · 10 min
Running pytest9 lessons
- Installing pytestReading · 3 min
- Your first pytest runReading · 4 min
- Reading the result lineReading · 3 min
- Verbose output with -vReading · 3 min
- Running one file or one testReading · 3 min
- Choosing tests with -kReading · 3 min
- Stopping at the first failure with -xReading · 3 min
- Quiet mode and exit codesReading · 3 min
- Build a tiny pytest with -k and -xExercise · 12 min
Reading failures8 lessons
- When a test failsReading · 4 min
- Assertion rewritingReading · 3 min
- Comparing lists and dictsReading · 3 min
- Comparing stringsReading · 3 min
- Adding your own messageReading · 3 min
- Shorter tracebacks with --tbReading · 3 min
- Errors inside your codeReading · 3 min
- Fix the bugs the tests foundExercise · 12 min
Test layout and discovery6 lessons
- How pytest finds testsReading · 3 min
- Grouping tests in a classReading · 3 min
- A tests folderReading · 3 min
- Telling pytest where your code isReading · 3 min
- More settings: addopts and older filesReading · 3 min
- Which tests will pytest collect?Exercise · 10 min
Exceptions and decimals8 lessons
- Testing that code raises: pytest.raisesReading · 3 min
- When the error doesn't comeReading · 3 min
- Checking the message with matchReading · 3 min
- Looking at the exceptionReading · 3 min
- Why 0.1 + 0.2 isn't 0.3Reading · 3 min
- pytest.approxReading · 3 min
- approx with tolerances and listsReading · 3 min
- Test errors and decimalsExercise · 12 min
Parametrize: many cases, one test7 lessons
- Repeating yourselfReading · 3 min
- @pytest.mark.parametrizeReading · 4 min
- When one case failsReading · 3 min
- Naming cases with pytest.paramReading · 3 min
- Parametrizing error casesReading · 3 min
- Stacking parametrizeReading · 3 min
- A parametrize engineExercise · 12 min
Fixtures9 lessons
- Setup that repeatsReading · 3 min
- Your first fixtureReading · 4 min
- A fresh fixture for every testReading · 3 min
- Fixtures that use fixturesReading · 3 min
- Cleaning up with yieldReading · 4 min
- Fixture scopeReading · 3 min
- Sharing fixtures with conftest.pyReading · 3 min
- When a fixture breaks: errorsReading · 3 min
- A tiny fixture systemExercise · 12 min
Built-in fixtures and marks8 lessons
- tmp_path: a fresh folder for each testReading · 3 min
- capsys: checking printed outputReading · 3 min
- monkeypatch: environment variablesReading · 3 min
- Skipping tests: skip and skipifReading · 3 min
- Expected failures: xfailReading · 3 min
- Your own marks and -mReading · 3 min
- Catching mistyped marksReading · 3 min
- Testing printed output and filesExercise · 12 min
Mocking9 lessons
- Why replace real things in testsReading · 3 min
- Swapping a function with monkeypatchReading · 4 min
- Mock objectsReading · 3 min
- Checking how a mock was calledReading · 3 min
- Making a mock raise: side_effectReading · 3 min
- patch() from unittest.mockReading · 3 min
- Patch where it's usedReading · 3 min
- Don't mock too muchReading · 3 min
- Mock the SMS gatewayExercise · 12 min
Coverage and test-driven development9 lessons
- Organising a test suiteReading · 3 min
- Measuring coverage with pytest-covReading · 4 min
- Finding untested linesReading · 3 min
- Branch coverageReading · 3 min
- What coverage can't tell youReading · 3 min
- Test-driven developmentReading · 3 min
- TDD in action: red, then greenReading · 4 min
- TDD in action: the next testReading · 3 min
- Build trotro_fare() test-firstExercise · 12 min
Testing a small app end to end9 lessons
- The statement appReading · 3 min
- Unit tests for the partsReading · 4 min
- An end-to-end testReading · 3 min
- Testing the error pathReading · 3 min
- Running the real programReading · 3 min
- The whole suite with coverageReading · 3 min
- Running tests in CIReading · 4 min
- Test the statement app end to endExercise · 15 min
- Final quizQuiz · 10 min
Reviews
No reviews yet.
Comments
Log in to join the conversation.
Loading comments...