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

Free lesson

OS 7.1 (1 of 4): Functional testing: unit to system

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
TestA check that 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.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.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 you do itPizza app example
Unit testOne small part on its ownWhile you 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.

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

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 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.

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

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.)

Make a free account to save your work and track your progress.

Check: pick the test

Have a go

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

Key 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

Final check

6 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

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.