Stage 6 · Lesson 6 of 8
Tests with pytest
is a Python testing tool. It finds files named test_*.py and runs every whose name starts with test_. Tests use plain assert statements, such as assert monthly_total(entries) == 42.5.
Run pytest from the project root, and it reports each failure with the values involved. Test the core logic: functions that take values and return values. Cover such as an empty list, one entry and invalid input.
Use pytest.raises(ValueError) to check that bad input is rejected. @pytest.mark.parametrize runs one test over many inputs, and the tmp_path gives you a temporary folder for testing file code. Compare floats with pytest.approx, since 0.1 + 0.2 is not exactly 0.3.
A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it
Resources for this stage
Start with· follow one of these
- GitHub repoFreeProject Based Learning
Community maintained · Beginner
Follow one guided build in your language, then build your own without a guide
- BookFreeAutomate the Boring Stuff with Python
Al Sweigart · Beginner
Free book of practical scripts: files, spreadsheets, web scraping and email
Keep handy
Also worth your time
How to present each project: the problem, stack, challenges and results
- Build Your Own XGo deeper
When you want a harder build: a shell, a key-value store or a tiny Git, from scratch