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
| Word | What it means | Example |
|---|---|---|
| Test data | The values you give a program to test it. | 17 |
| Range | The allowed values, from the lowest edge to the highest edge. | 16 to 19 |
| Valid data | Data the program should accept. | 17 |
| Invalid data | Data of the right type but outside the range. | 5 |
| Valid extreme data | Data on the very edge of the range, still allowed. | 16 and 19 |
| Invalid extreme data | Data just outside the edge of the range, not allowed. | 15 and 20 |
| Erroneous data | Data of the wrong type or format, such as text or a decimal where a whole number is needed. | "abc" |
| Expected result | What 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
| Kind | Example for age 16 to 19 | Expected result |
|---|---|---|
| Valid (normal) | 17, 18 | Accepted |
| Valid extreme | 16, 19 | Accepted |
| Invalid | 5, 80 | Refused: outside the range |
| Invalid extreme | 15, 20 | Refused: outside the range |
| Erroneous | "abc", empty, 16.5 | Refused: 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
| 5 | 15 | 16 | 17 | 18 | 19 | 20 | 80 |
|---|---|---|---|---|---|---|---|
| Invalid | Invalid extreme | Valid extreme | Valid (normal) | Valid (normal) | Valid extreme | Invalid extreme | Invalid |
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
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.
# 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
| Kind | A mark from 0 to 100 | Expected result |
|---|---|---|
| Valid | 55, 80 | Accepted |
| Valid extreme | 0, 100 | Accepted |
| Invalid | -30, 400 | Refused |
| Invalid extreme | -1, 101 | Refused |
| Erroneous | "fifty", empty | Refused: 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.
- Write the lowest and the highest allowed value.
- Valid extreme: those two values.
- Invalid extreme: one less than the lowest, one more than the highest.
- 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.
# 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