Editor

Predict the Test Results

Intermediate · Predict the output · from Python Testing with pytest: Write Tests That Actually Help

From the lesson: Python Testing with pytest: Write Tests That Actually Help

Imagine you wrote a spell-checker. It works great on the word "hello." But what about "HeLLo"? Or an empty string? Or a number? You won't know until something breaks - unless you write tests.

Read the lesson

Read the code below carefully. The test runner catches assertion errors and reports results. Predict exactly what gets printed. Think about which tests will pass and which will fail.

def clamp(value, low, high):
if value < low:
return low
if value > high:
return high
return value
tests = [
('in_range', lambda: clamp(5, 1, 10) == 5),
('below_min', lambda: clamp(-3, 0, 100) == 0),
('above_max', lambda: clamp(50, 1, 10) == 10),
('at_boundary', lambda: clamp(10, 1, 10) == 10),
]
for name, check in tests:
if check():
print(f'PASS: {name}')
else:
print(f'FAIL: {name}')

More pytest exercises

All Python exercises · 50 practice problems · Testing quiz