The buy button that did nothing
You open a pizza app. You choose a pizza. You press Buy. Nothing happens.
You delete the app. The company lost a customer. It also lost the money for your pizza.
Somebody could have found this problem before you did. That job is called testing. Today you learn four kinds of tests that check what a program does.
Key words
| Word | What it means | Example |
|---|---|---|
| Test | A check that shows if the software does the right thing. | Press Buy and see an order |
| Bug | A mistake in the software. | The price shows as 0 |
| Functional testing | Tests that check what the software does. | Does Buy make an order? |
| Unit test | A test of one small part, such as one function. | Test the fare function |
| Integration test | A test that two or more parts work together. | Basket and payment |
| System test | A test of the whole product, from start to end. | Choose, pay, get the receipt |
| Smoke test | A quick check that the main things work at all. | The app opens |
| Function | A named piece of code that does one job. | fare(age) |
| Assert | A Python line that stops with an error if something is false. | assert 2 + 2 == 4 |
The cells students fill in are left blank in this preview.
Four kinds of functional test
| Test | What it checks | When we do it | Pizza app example |
|---|---|---|---|
| Unit test | One small part on its own | While we write the code | The price function adds up 3 pizzas |
| Integration test | Parts that work together | When the parts are joined | The basket sends the right price to the payment part |
| System test | The whole product | When the product is nearly ready | Choose, pay and get the receipt |
| Smoke test | Only the main things, quickly | Every new version, before other tests | The app opens and the menu shows |
The cells students fill in are left blank in this preview.
Functional testing
Every program has jobs it must do. A pizza app must take an order.
Functional testing asks one question: does the software do its job? We give it an input. We look at the output. We compare it with what we expect.
Unit tests in Python
A unit test calls one function and checks the answer. In Python we use assert.
If the check is true, nothing happens. If it is false, Python stops with an error. Run the code below. Then put a mistake in the function and run it again.
Try it: a unit test
def fare(age):
if age < 16:
return 1.0
return 2.5
assert fare(10) == 1.0
assert fare(40) == 2.5
print("All checks passed")
# Change 1.0 to 0.5 in the function and run again.
# What error do you see?
Test the limit
data-callouts.tip.label
Most bugs hide where a rule changes. The fare changes at 16. So test 15 and 16, not only 10 and 40.
Smoke and integration tests
A smoke test is quick. It checks that the main parts work. If the app does not open, there is no point in running 500 other tests.
An integration test checks the join between parts. Each part can pass its own unit tests and the parts can still fail together. For example, one part sends the price in pounds and the next part reads pence. (There are 100 pence in 1 pound.) Run the code below to see it.
Try it: two good parts, one bad join
def basket_total():
return 2.50 # the price in pounds
def take_payment(pence):
return "Charged " + str(pence) + " pence"
# Each part is right on its own. Now join them:
print(take_payment(basket_total()))
# It says 2.5 pence. That is wrong. The first part gives pounds, the second wants pence.
# Fix it: change the last line to print(take_payment(round(basket_total() * 100)))
# (The * sign means times. 2.50 times 100 is 250 pence.)
Quick check: pick the test
Have a go
Quick check: pick the test
4 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.