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.3 (1 of 2): Test data: normal, edge and wrong

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

Break the form

A college form asks for your age. It says: only 16, 17, 18 or 19.

You are the tester. What do you type to find a bug? Try 17. Try 16. Try 20. Try the word "abc".

A good tester picks data on purpose. This lesson shows how.

Key words

WordWhat it meansExample
Test dataThe values you give a program to test it.17
RangeThe allowed values, from the lowest edge to the highest edge.16 to 19
Valid dataData the program should accept.17
Invalid dataData of the right type but outside the range.5
Valid extreme dataData on the very edge of the range, still allowed.16 and 19
Invalid extreme dataData just outside the edge of the range, not allowed.15 and 20
Erroneous dataData of the wrong type or format, such as text or a decimal where a whole number is needed."abc"
Expected resultWhat the program should do with that data.Accept, or show an error

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

Test data comes in five kinds

KindExample for age 16 to 19Expected result
Valid (normal)17, 18Accepted
Valid extreme16, 19Accepted
Invalid5, 80Refused: outside the range
Invalid extreme15, 20Refused: outside the range
Erroneous"abc", empty, 16.5Refused: not a whole number

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

Match each value to its kind

Have a go

Loading…

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

The age range on a line

515161718192080
InvalidInvalid extremeValid extremeValid (normal)Valid (normal)Valid extremeInvalid extremeInvalid

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

Bugs hide at the edges

Important

Many people think testing 17 or 18 is enough. Programmers write age >= 16 and age <= 19. Here >= means "bigger than or equal to" and <= means "smaller than or equal to". A small mistake, such as age > 16 (only bigger than 16), refuses a 16-year-old. Only the edge value 16 finds this bug.

Try it: run the test data

Code: python
def check_age(text):
    if not text.isdigit():
        return "not a number"
    age = int(text)
    if age < 16 or age > 19:
        return "wrong range"
    return "ok"

for value in ["17", "16", "19", "15", "20", "abc", "16.5"]:
    print(value, "->", check_age(value))

# Change age < 16 to age <= 16, then press Run.
# Find the test data that now gives the wrong answer.
# (<= means "smaller than or equal to")

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

Your turn: write the checker

Write the checker for the college form. check_age gets what the user typed, as text. Give back "ok" for a whole number from 16 to 19. Give back "wrong range" for a whole number outside that. Give back "not a number" if the text is not a whole number. For us, 16.5 is not a whole number. The hidden tests use all five kinds of test data. Press Run Tests.

Code: python
# A college form asks for an age. Only 16, 17, 18 and 19 are allowed.
# The user types text, so the value is always a string, for example "17" or "abc".
#
# Give back one of three answers:
#   "ok"             the text is a whole number from 16 to 19
#   "wrong range"    the text is a whole number, but it is below 16 or above 19
#   "not a number"   the text is not made only of digits: letters, empty, 16.5 or -3
#                    (the dot and the minus sign are not digits)
#
# Hint: "17".isdigit() is True. "abc".isdigit() is False.

def check_age(text):
    return ""

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

Worked example: a test mark

KindA mark from 0 to 100Expected result
Valid55, 80Accepted
Valid extreme0, 100Accepted
Invalid-30, 400Refused
Invalid extreme-1, 101Refused
Erroneous"fifty", emptyRefused: not a number

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

Four steps pick the test data

For any range, follow four steps.

  1. Write the lowest and the highest allowed value.
  2. Valid extreme: those two values.
  3. Invalid extreme: one less than the lowest, one more than the highest.
  4. Add a normal value in the middle, and some wrong types.

Choose the data

Now you are the tester. For any range from low to high, choose the test data. normal_value gives a value inside, not on an edge. valid_extreme gives the two edge values. invalid_extreme gives one value just below and one just above. erroneous_examples gives three or more pieces of text that are not numbers. Press Run Tests.

Code: python
# Choose test data for any range from low to high (both ends are allowed).
# Give back the data. Do not check it: just choose it.

def normal_value(low, high):
    # A value that is clearly inside the range, not on an edge.
    return 0


def valid_extreme(low, high):
    # A list of the two values on the very edges that are still allowed.
    return []


def invalid_extreme(low, high):
    # A list of the two values just outside the edges, one on each side.
    return []


def erroneous_examples():
    # A list of at least three values that are not whole numbers at all,
    # such as text. Do not use numbers.
    return []

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

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.