A hands-on introduction to testing Python code, written for people who've coded a bit but have never written a test. Five short lessons that build on each other — by the end you'll know what tests are, why they're worth your time, and how to write them with pytest.
- Clone or download this repo.
- Install
pytest:pip install -r requirements.txt - Work through the folders in order. Each folder has a
README.mdthat explains the concept, plus example code and tests you can run. - From inside any lesson folder, run:
pytest
That's it. Don't skip ahead — each lesson assumes you've done the previous one.
If you'd rather not install anything locally:
docker build -t testing-for-beginners .
docker run --rm testing-for-beginners # run all tests
docker run --rm testing-for-beginners pytest 02-pytest-basics # run one lesson
docker run --rm -it testing-for-beginners bash # poke around
- 01-why-test — What tests are and why they matter, shown by catching a real bug.
- 02-pytest-basics — How
pytestfinds and runs tests, and howassertworks. - 03-fixtures — How to share setup code between tests without repeating yourself.
- 04-mocking — How to test code that talks to APIs or other external things.
- 05-what-to-test — Practical guidance on what's actually worth testing.
- 06-testing-agents — Bonus lesson: unit testing LangGraph agents with a fake LLM.