Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Functional testing: unit to system

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

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

WordWhat it meansExample
TestA check that shows if the software does the right thing.Press Buy and see an order
BugA mistake in the software.The price shows as 0
Functional testingTests that check what the software does.Does Buy make an order?
Unit testA test of one small part, such as one function.Test the fare function
Integration testA test that two or more parts work together.Basket and payment
System testA test of the whole product, from start to end.Choose, pay, get the receipt
Smoke testA quick check that the main things work at all.The app opens
FunctionA named piece of code that does one job.fare(age)
AssertA 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

TestWhat it checksWhen we do itPizza app example
Unit testOne small part on its ownWhile we write the codeThe price function adds up 3 pizzas
Integration testParts that work togetherWhen the parts are joinedThe basket sends the right price to the payment part
System testThe whole productWhen the product is nearly readyChoose, pay and get the receipt
Smoke testOnly the main things, quicklyEvery new version, before other testsThe 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

Code: python
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

Code: python
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

The lesson goes on

3 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.