Three tests and a tiny runner (one fails)
The shipping rule is 'free from 500 up' but the code uses >. Which test catches it? Change > to >= and run again.
A test is a small program that runs your code with a chosen input and checks the result against the answer you expect. Tests are a safety net: they let you change code and find out in seconds whether something broke.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write is_adult(age) that returns True from 18 up. Write three tests: below, on and above the boundary, and run them with a loop like the one in this topic.
This function should return the largest number in a list, but the test with [-5, -2, -9] fails. Find the bug: def biggest(nums): top = 0 then a loop that sets top = n when n > top, then return top. Write the failing test first.
Write a test for average([]). Decide what it should do (raise ZeroDivisionError, or return 0?), write the test for your decision, then make the function match.
Explain it without notes
What is a test, and what are arrange, act and assert?
What does the assert statement do, and why should you not use it to validate user input?
What is a regression, and how do tests protect against one?
Explain the test pyramid. Why are there more unit tests than end-to-end tests?
How would you choose inputs for a function that has a rule like "free shipping from 500 up"?
Expected output
PASS test_big_order_ships_free
FAIL test_exactly_500_ships_free - expected 0, got 40
PASS test_small_order_pays_fee
2 of 3 passed