1def add(a, b):2 return a + badd()mock1def test_add():2 assert add(2, 3) == 53def test_edges():4 assert add(-2, 2) == 056$
Back: the unit under test (calc.py and the add() block), a web service and a mockFront: the tests (test_calc.py), their pass/fail lamps and the terminal running pytestTokens: a test's input going in, the function's answer coming out to the lampAt the back, the unit: the function add in calc.py and the add() block that runs it. In front, the tests in test_calc.py, a lamp beside each test and a terminal that runs pytest. Number tokens carry a test's known input into the function and its answer to the lamp: green for pass, red for fail. The code card changes as the code is broken, fixed, and grown test-first: red, green, refactor. Every result in this figure was checked by running these exact files with pytest 9.1.1 on Python 3.14. The terminal is trimmed to fit: real pytest prints the file name before the dots (test_calc.py ..) and how long the run took.

One test

  1. A unit is the smallest piece of code you can check on its own, usually one function. Here it is add, in the file calc.py. Give it two numbers, a and b, and it returns their sum.

Run them often

Test first

More uses

What happens

;; step 1/12 · unit
# calc.py
def add(a, b):
    return a + b

unit      one small piece of code: here, one function
input     two numbers, a and b
output    what it returns: a + b

test_calc.py also starts with the line from calc import add, so the tests can call add. The card leaves it out to save room. Every result in this figure was checked by running these exact files with pytest 9.1.1 on Python 3.14. The terminal is trimmed to fit: real pytest prints the file name before the dots (test_calc.py ..) and how long the run took.