Phase 6: Testing & Code Quality · ~35 minutes · Python · uv · pytest
Writing Your First Real Tests
assert 3 == 2.5 — pytest doesn't just tell you a test failed. It tells you exactly what your code actually returned, right in the failure message.
Hiring signal: Writes small, focused tests with self-explanatory names instead of one giant test that only tells you something broke
What you will learn
- Write and run real pytest tests using plain assert statements
- Read a real pytest failure output and identify exactly what was expected vs. what happened
- Explain why many small, focused tests are more useful than one large test checking everything
- Name tests so a failure is self-explanatory without reading the test body
Introduction
Type: Learn Languages: Python Prerequisites: Lesson 01 (Why Untested Code Lies to You) Time: ~35 minutes
Objective
Learning objectives
- Write and run real pytest tests using plain
assert statements - Read a real pytest failure output and identify exactly what was expected vs. what happened
- Explain why many small, focused tests are more useful than one large test checking everything
- Name tests so a failure is self-explanatory without reading the test body
What you're building
A script test_your_function.py for a real function you wrote in an earlier phase (Phase 04 or 05 is a good source — a sorting function, a recursive function, a class method):
- At least 4 separate
test_* functions, each testing exactly one behavior, with names specific enough that a failure alone tells you what broke - At least one test that specifically targets an edge case (empty input, a boundary value, or similar — per Lesson 01's discipline)
- Run with
pytest -v and include, as a comment at the top of the file, the real terminal output showing all tests passing - If you can find or construct a genuine bug in the function under test, include a second comment showing the real
FAILED output before you fixed it
A test file has one single test function, test_everything(), containing 12 different assert statements checking 12 different behaviors of a function. It fails. What's the most accurate description of what you'll learn from the failure report?
A test function is one unit as far as pytest is concerned — it runs until the first assert that fails, then stops and reports that one function as FAILED, exactly as this lesson's test_even_length_list example did. With 12 unrelated checks crammed into one function, a failure tells you 'something in here is wrong' and stops before even attempting the other 11 — you'd have to comment out or reorder assertions by hand to find out about the rest. This is the precise, concrete cost of not splitting behaviors into separate, individually-named test functions.
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers The Problem, Check Yourself, Key Terms & Next — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy