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 (2 of 3): More testing methods and automation

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

A quiet Tuesday and Black Friday

A shop website works well on a quiet Tuesday. On Black Friday 20,000 people arrive at once.

The site is slow. Then it stops. The tests on Tuesday never saw this. Different problems need different kinds of testing. In this lesson you meet six more groups of methods.

Key words

WordWhat it means
Performance testChecks how fast the system is and how much memory it uses.
Load testChecks the system with the number of users you expect on a busy day.
Stress testPushes the system past its limit to see how and when it breaks.
Usability testReal people try the product. You watch how easy it is to use.
Regression testRuns the old tests again after a change, to check nothing broke.
Closed box testYou test inputs and outputs. You cannot see the code.
Open box testYou can see the code and choose tests that use every path.
AutomationLetting a computer run the tests for you.
MacroA recorded list of actions that a program can repeat.
ScriptA small program that runs a job, such as running tests.
FunctionA named piece of code that does a job.
Unit testA test of one small part on its own.
PrototypeA simple early model of a product, used to try out an idea.

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

Six more groups of testing methods

MethodPurposeWhen it is used
PerformanceIs it fast enough, and does it use too much memory?When speed matters, and after big changes
UsabilityCan real people use it easily?On a prototype and on the finished product
RegressionDid a change break something that worked?After every change or fix
LoadDoes it cope with the expected number of users?Before a busy time, such as a sale
StressWhat happens when it is pushed past its limit?To find the breaking point and see how it fails
Closed box and open boxTest with no view of the code, or with the code in viewClosed: acceptance and system tests. Open: unit tests

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

Match the situation to the method

Have a go

Loading…

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

Closed box and open box

In closed box testing you treat the program like a sealed box. You put data in and check what comes out. You do not care how it works inside.

In open box testing you can see the code. You choose tests so that every path is used: every if, every elif and every else.

Try it: cover every path

Code: python
def fare(age):
    if age < 5:
        return 0.0
    elif age < 16:
        return 1.0
    else:
        return 2.5

# This function has three paths, so an open box tester needs three tests.
assert fare(3) == 0.0
assert fare(10) == 1.0
assert fare(30) == 2.5
print("All three paths tested")

# Delete the last assert. Which path is now never tested?

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

Benefits and drawbacks

MethodBenefitDrawback
PerformanceFinds slow parts before users doNeeds a realistic machine and data
UsabilityShows real problems that developers missSlow and costly: needs real people
RegressionCatches broken old featuresThe set of tests grows and takes time to run
Load and stressFinds the limit before a busy dayNeeds special tools and a copy of the live system
Closed boxTests like a real user, and the code does not guide the testsSome paths in the code may never be tried
Open boxCan test every path in the codeThe tester needs to understand the code

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

Quick check: which method?

Have a go

Quick check: which method?

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

Let the computer do the testing

Running the same tests by hand is slow and boring. People make mistakes when they are bored.

Automation lets a computer run tests again and again, the same way every time. It is very good for regression tests: run all the tests after each change, even at night.

Three ways to automate

ToolWhat it isExampleDrawback
MacroRecords the actions of a user and plays them backRecord the steps in a spreadsheet and repeat themBreaks when the screen changes
ScriptA small program that runs tests and reportsA Python file that calls functions and checks answersSomebody must write and keep it up to date
Functional testing toolSoftware that clicks through an app and checks the resultsA tool that opens a web page, types and presses buttonsTakes time to learn and set up

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

Try it: a test script

Code: python
def double(n):
    return n * 2

arguments = [2, 5, 3]
expected = [4, 10, 7]

for i in range(len(arguments)):
    answer = double(arguments[i])
    if answer == expected[i]:
        print("PASS", arguments[i])
    else:
        print("FAIL", arguments[i], "expected", expected[i], "got", answer)

# One test is wrong on purpose. Which one?

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

Try it: a performance test

Code: python
from datetime import datetime

start = datetime.now()
total = 0
for number in range(1000000):
    total = total + number
end = datetime.now()

print("Total:", total)
print("Time taken:", end - start)

# Change 1000000 to 10000000. How does the time change?

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

Extra: Try it: find the breaking point

Code: python
capacity = 5
served = 0

for user in range(1, 13):
    if served < capacity:
        served = served + 1
        print("User", user, "served")
    else:
        print("User", user, "refused: the server is full")

print("It broke at user", capacity + 1)

# A stress test keeps adding users until something fails.
# Change capacity to 8. Where is the breaking point now?

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

Quick check: a regression test

Have a go

Quick check: a regression test

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

Your turn: four test scripts

Write four small automation scripts.

  1. count_passes(actual, expected) compares two lists and gives back how many answers match.
  2. regression_check(old_answers, new_answers) gives back a list of the positions where an answer has changed.
  3. fast_enough(seconds_taken, limit_seconds) gives back True if the time was under the limit. Exactly on the limit is not fast enough.
  4. load_report(replies) counts how many users got "ok".

Press Run Tests to check.

Code: python
def count_passes(actual, expected):
    # A test script. actual and expected are two lists of the same length.
    # Compare the items in the same position. Give back how many are equal.
    return None


def regression_check(old_answers, new_answers):
    # A regression test. old_answers were right last week. new_answers are from today.
    # Give back a list of the positions (starting at 0) where the answer is now DIFFERENT.
    return None


def fast_enough(seconds_taken, limit_seconds):
    # A performance test. Give back True if the time taken was less than the limit.
    return None


def load_report(replies):
    # A load test. replies is a list of "ok" and "error", one for each user.
    # Give back how many users got "ok".
    return None

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

Watch: more testing methods and automation

Press Play on the short film below. It tests the fare function on every path and runs a test script. Then it shows a regression test finding what a change broke, and asks what an automated test cannot judge. Watch what each test compares.

Film: more testing methods and automation

More testing methods and automation

Every scene, as text

  1. Open box testing: you can see the code, so you choose tests that use every path. In closed box testing you cannot see it, and only check what comes out. The lesson's fare function has three paths, so it needs three tests: fare(3) takes the if, fare(10) the elif and fare(30) the else. All three asserts pass, so it prints All three paths tested.
  2. Automation: a test script lets the computer run the tests, the same way every time. It calls double on each argument and compares the answer with the expected one. It prints PASS 2 and PASS 5, then FAIL 3 expected 7 got 6: double(3) gives 6, and the expected 7 is the test that is wrong on purpose.
  3. A regression test runs the old tests again after a change, to check nothing broke, and a script is very good at it. The task's regression_check compares the old answers, right last week, with the new answers from today, position by position: regression_check([2, 4, 6, 8], [2, 4, 0, 0]) prints [2, 3], so the change broke the answers at positions 2 and 3, which used to work.
  4. Myth (false): automated tests are good at judging if a page looks pleasant. A test script such as the task's count_passes can only compare each answer with an expected one: count_passes([4, 10, 6], [4, 10, 7]) prints 2, as 4 and 10 match and 6 is not 7. How pleasant a page looks has no expected answer to compare.
  5. The cure: that needs a person. In a usability test real people try the product, and you watch how easy it is to use. A test repeated many times, such as a regression test, is the one to automate, but somebody must write the script and keep it up to date.

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.