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 testing. This lesson shows four kinds of tests that check what a program does.
Key words
| Word | What it means | Example |
|---|---|---|
| Test | A check that 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. | 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. | 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 you do it | Pizza app example |
|---|---|---|---|
| Unit test | One small part on its own | While you 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.
Match each test to its example
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Tests run from small to whole
flowchart LR
unit[Unit tests: each part] --> integration[Integration tests: parts together]
integration --> system[System test: the whole product]
system --> release[Ready for users]
smoke[Smoke test on every new version] -.-> unit
Functional tests check what software does
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? You give it an input. You look at the output. You compare it with what you expect.
A unit test checks one function
A unit test calls one function and checks the answer. In Python you 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 return 1.0 to return 0.5 in the function, then press Run.
# Read the error that appears.
Make a free account to save your work and track your progress.
Test where the rule changes
Tip
Many people test only everyday values, such as 10 and 40. Most bugs hide where a rule changes. The fare changes at 16, so test 15 and 16 as well.
Your turn: catch the bugs
A bus company has a fare function. A child under 16 pays 1.0. Everybody else pays 2.5. You do not write the function. You write the checks. Fill in check_fare with assert lines. The hidden tests run your checks on one good function and three wrong ones. Your checks must pass the good one and catch every wrong one. Press Run Tests.
# fare(age) should give 1.0 for a child (under 16) and 2.5 for everybody else.
# You get a function called fare. Write checks for it inside check_fare.
# Use assert, for example: assert fare(10) == 1.0
# Your checks must pass for a good fare and fail for a wrong one.
def check_fare(fare):
pass
Make a free account to save your work, or press Start lab to work through the whole lesson.
Smoke tests first, then the joins
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.)
Make a free account to save your work and track your progress.
Check: pick the test
Have a go
Check: pick the test
4 questionsKey words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Four tests, four questions
Important
Functional tests ask: does it do its job? Unit is one part. Integration is parts together. System is the whole product. Smoke is a quick first check.
Final check
Have a go