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

Free lesson

2.12 (1 of 3): Testing parts, then the whole

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.

50 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

Check the brakes first

A bike builder checks the brakes, the gears and the chain one by one. Only then does she put the bike together.

Imagine she tested only the finished bike. A bad brake would be hard to find. Software is built the same way. You test the small parts first, then bigger and bigger parts.

Key words

WordWhat it meansExample
ComponentOne part of a solution.a function, a device or a screen
InterfaceThe place where two parts meet and pass data.a screen and a database
Unit testA test of one small part on its own.
Integration testA test that checks joined parts work together.
System testA test of the whole system against its requirements.
Acceptance testThe customer tests the product and decides to accept it.
Boundary testA test with values on the edges of what is allowed.
Concept testA test of an idea or a design before it is built.
RequirementSomething the solution must do.
FunctionA named piece of code that does a job.
PrototypeA simple early model of a product, used to try out an idea.
DatabaseAn organised store of data that programs can read and change.

The cells students fill in are left blank in this preview.

Five things you test

PartWhat it isAn example test
SoftwareThe code, such as one functionDoes the fare function give 2.50 for an adult?
HardwareThe devices and partsDoes the card reader read a card?
DataFiles and databasesIs every price in the file a number?
InterfacesWhere two parts meetDoes the screen send the right data to the database?
Resulting serviceThe finished productCan a customer buy a ticket from start to end?

The cells students fill in are left blank in this preview.

Quick check: which part?

Have a go

Quick check: which part?

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

Why test the parts first?

A bug in a small part is easy to find. You know which part to look at.

A bug in a finished product could be anywhere. Fixing a bug early is cheaper than fixing it late. And when every part works, you can trust them when they are joined.

Testing grows from small to big

A flowchart of six tests in a column, each with an arrow to the next: Concept test: the idea, Unit tests: each part, Boundary tests: the edges, Integration tests: parts together, System test: the whole thing, and Acceptance test: the customer.A flowchart of six tests in a column, each with an arrow to the next: Concept test: the idea, Unit tests: each part, Boundary tests: the edges, Integration tests: parts together, System test: the whole thing, and Acceptance test: the customer.

Six testing methods

MethodPurposeWhen it is used
Concept testingFind out if an idea or design is worth buildingAt the start, with a sketch or a prototype
Unit testingCheck one small part on its ownEvery time a part is written or changed
Boundary testingCheck the edges of the allowed valuesWhen a part has a limit, such as an age range
Integration testingCheck that joined parts work togetherWhen parts are put together
System testingCheck the whole system against the requirementsWhen all parts are joined
Acceptance testingThe customer checks it does what they needAt the end, before they accept the product

The cells students fill in are left blank in this preview.

Try it: a unit test

Code: python
def fare(age):
    if age < 16:
        return 1.0
    return 2.5

# A unit test checks one small part on its own.
assert fare(10) == 1.0
assert fare(40) == 2.5
print("Both unit tests passed")

# Add a third test: assert fare(16) == 2.5
# Then break the function and see the test fail.

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

Try it: boundary tests

Code: python
def is_teenager(age):
    return age >= 13 and age <= 19

# Test the edges: one below, on the edge, on the edge, one above.
for age in [12, 13, 19, 20]:
    print(age, is_teenager(age))

# Change >= 13 to > 13. Which age now gives the wrong answer?

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

Extra: Try it: let the code find the edge

Code: python
def is_teenager(age):
    return age > 13 and age <= 19

low = 13
high = 19

for age in [low - 1, low, high, high + 1]:
    should_pass = age >= low and age <= high
    if is_teenager(age) == should_pass:
        print(age, "PASS")
    else:
        print(age, "FAIL: wrong answer at this edge")

# One edge is wrong on purpose. Fix is_teenager so all four pass.

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

Match the example to the method

Have a go

Loading…

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

Put the methods in order

Have a go

Put the methods in order

3 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

Benefits (good points) and drawbacks (bad points)

MethodBenefitDrawback
ConceptCheap: no code is neededUsers judge a sketch, not the real thing
UnitFinds bugs early and shows exactly whereDoes not show if parts work together
BoundaryFinds common edge mistakes, such as > (more than) where >= (more than or equal to) is neededTests only the edges, not everything
IntegrationFinds problems where parts meetHarder to find which part is at fault
SystemTests the real whole productLate, so fixes cost more
AcceptanceThe customer is happy it does what they needVery late. A big change is expensive

The cells students fill in are left blank in this preview.

Your turn: build test tools

Build three small testing tools.

  1. boundary_values(low, high) gives back a list of the four boundary values: one below the lowest, the lowest, the highest and one above the highest.
  2. is_boundary(value, low, high) gives back True if a value is one of those four, and False if not.
  3. unit_result(actual, expected) gives back "PASS" if the two are equal and "FAIL" if they are different.

Press Run Tests to check.

Code: python
def boundary_values(low, high):
    # Give back a list of the four boundary values for a range:
    # one below the lowest, the lowest, the highest, one above the highest.
    # For 13 to 19 that is [12, 13, 19, 20].
    return None


def is_boundary(value, low, high):
    # Give back True if the value is one of the four boundary values for low to high.
    # For 13 to 19, the values 12, 13, 19 and 20 give True. The value 16 gives False.
    return None


def unit_result(actual, expected):
    # A unit test compares what a part gave with what was expected.
    # Give back "PASS" if they are equal, and "FAIL" if they are different.
    return None

Make a free account to save your work, or press Start lab to work through the whole lesson.

Watch: testing parts, then the whole

Press Play on the short film below. It tests the fare function from this lesson, then the two functions from your task, first joined and then one part at a time. Watch which test shows where the bug is.

Film: testing parts, then the whole

Testing parts, then the whole

Every scene, as text

  1. A unit test checks one small part on its own: here the lesson's fare function. Each assert compares what fare gives with what was expected. The call fare(10) gives 1.0 and fare(40) gives 2.5, both as expected, so the program goes on and prints Both unit tests passed.
  2. Myth (false): you should test only the finished product. Here the task's boundary_values and is_boundary are joined, with one slip planted in the first, before either is tested, and only the whole is tried. The value 13 is the lowest of 13 to 19, a boundary value, so print(is_boundary(13, 13, 19)) should print True. It prints False. Something is wrong, but the result cannot say which part.
  3. The cure: test each part on its own. For 13 to 19, boundary_values should give [12, 13, 19, 20]. The line print(boundary_values(13, 19)) prints [12, 14, 19, 20], 14 where 13 should be, so the bug is in this part: low + 1, where the second value should be low.
  4. One change, low in place of low + 1, and the same unit test again. It prints [12, 13, 19, 20], the four boundary values: one below the lowest, the lowest, the highest and one above the highest. What it gave matches what was expected, so the unit test passes.
  5. Integration test: only when each part works are the parts joined. The function is_boundary calls boundary_values, where the two parts meet and pass data. The values 12, 13, 19 and 20 print True and 16 prints False, as the task's tests expect: the joined parts work together.
  6. Testing grows from small to big: a concept test of the idea, unit tests of each part, boundary tests of the edges, integration tests of the parts together, a system test of the whole thing, and last the acceptance test, by the customer.

Key words crossword

Have a go

Loading…

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

Exit quiz

Have a go

Exit quiz

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