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.2 (2 of 2): Closed box, open box and AI testing

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

A lift with a button

You press a lift button. The lift comes. You do not know how the lift works inside.

A lift engineer can open the panel and look at the inside. That person tests in a different way.

Software is tested in both ways: from outside, and from inside.

Key words

WordWhat it meansExample
Closed box testingTesting inputs and outputs with no view of the code.Press Buy and see the receipt
Open box testingTesting with the code in view.Test every if and else
Structural testingAnother name for open box testing.Test every path
PathOne route through the code.The code inside one if
Automatic testingA program running the tests for you.Run 500 checks in a minute
Manual testingA person running the tests by hand.Press each button
AI toolA program that can write text or code when you ask it.A chat assistant
RequirementSomething the product must do.Users can log in
FunctionA named piece of code that does one job.grade(score)
Unit testA test of one small part.Test the grade function
Test caseOne input and the answer you expect.Input 3, expect 6

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

Closed box tests outside, open box inside

Closed boxOpen box
What the tester seesOnly inputs and outputsThe code inside
Who often does itTesters, users, the clientThe developer
How tests are chosenFrom the requirementsFrom the paths in the code
ExampleType 45 and check the grade says nearSee the line if score >= 40 and make sure a test reaches it
Weak pointCannot see code that is never reachedCan miss a requirement the code forgot

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

Match each word to its meaning

Have a go

Loading…

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

Try it: find the paths

Code: python
def grade(score):
    if score < 0 or score > 100:
        return "invalid"
    if score >= 50:
        return "pass"
    if score >= 40:
        return "near"
    return "fail"

for score in [75, 45, 10, 150]:
    print(score, grade(score))

# There are 4 paths. Did the four scores above reach all of them?
# (>= means "is bigger than or equal to")

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

Test either side of each if

Tip

In the code, >= means "is bigger than or equal to". So score >= 50 means 50 or more. Test 49, 50 and 51. Mistakes often hide where an if changes the answer.

Reach every path

You can see the grade function in the starter. This is open box testing. Fill in inputs_for_every_path. It gives a list of scores. Together they must reach all four paths: invalid, pass, near and fail. Also include 50 and 40, where the answer changes. Include one score below 0 and one score above 100. Press Run Tests.

Code: python
# This is the function we test. You can see all of it, so this is open box testing.
#
# def grade(score):
#     if score < 0 or score > 100:
#         return "invalid"
#     if score >= 50:
#         return "pass"
#     if score >= 40:
#         return "near"
#     return "fail"
#
# Give back a list of scores. Together they must make the function
# go down EVERY path: invalid, pass, near and fail.
# Also test the limits 50 and 40, where the answer changes.
# Include one score below 0 and one score above 100.

def inputs_for_every_path():
    return []

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

People explore, programs repeat

Manual testing: a person runs the test. Good for new things, how a page feels and odd ideas.

Automatic testing: a program runs the tests. Good for tests you repeat, such as after every change. A computer does not get bored and does not forget a step.

Build a test runner

Write a tiny automatic test runner. A case is a pair: the input and the answer you expect. failed_cases calls the function for each case and gives back the cases that did not match. all_pass says True when nothing failed. Press Run Tests.

Code: python
# An automatic test runner. A case is a pair: (input, expected answer).
# For example: (3, 6) means "when the input is 3 we expect 6".
# For one case: case[0] is the input and case[1] is the expected answer.

def failed_cases(function, cases):
    # Call function with each input. Give back a list of the cases where
    # the answer was not the expected one. Keep the order.
    return []


def all_pass(function, cases):
    # True if no case failed.
    return False

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

An AI tool can suggest tests

An AI tool can help you test. You can ask it for test ideas, for odd inputs, or to write the first version of unit tests.

It is fast, but it can be wrong, and it does not know your requirements. You must read and run what it writes.

Using an AI tool: ask, read, run, add

StepWhat you doExample
AskType a clear prompt (the words you give the AI)Write 5 test cases for grade(score). 50 or more is a pass. Scores go from 0 to 100.
ReadCheck each test against the ruleIs the expected answer right for 50?
RunRun every test. Fix any test that is wrongThe AI wrote 49 as a pass. That is wrong.
AddAdd the tests the AI missedAdd -1 and 101, which are not allowed.

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

You are responsible, not the AI

Warning

Many people think tests from an AI tool are already correct. They can be wrong, so check every test the AI writes. Do not paste passwords or private data into it. You are responsible for the result, not the AI.

Check: AI and testing

Have a go

Check: AI and testing

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

Key words crossword

Have a go

Loading…

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

Final check

Have a go

Final check

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.