Testing a lift
A lift (an elevator in a building) has a sign: maximum 8 people. How would you test it?
Try 3 people. Try exactly 8. Try 9. Try 0. What about a chair, or a dog? Each of these is a different kind of data for a test. The values you use to test are called test data (the data you test with). Good testers choose it carefully.
Key words
| Word | What it means | Example |
|---|---|---|
| Test data | The values you give to a program to test it. | |
| Valid data | Data the program should accept: the right kind and inside the limits. | 55 for a mark of 0 to 100 |
| Invalid data | The right kind of data, but outside the limits. The program should reject it. | 150 |
| Boundary data | Values on the edges of the limits, and the first values just outside them. | 0, 100, -1, 101 |
| Erroneous data | The wrong kind of data. The program should reject it. | "abc" |
| Test plan | A table that lists each test, its data and its expected result. | |
| Expected result | What should happen, decided before the test. | |
| Actual result | What really happened when you ran the test. |
The cells students fill in are left blank in this preview.
Four kinds of test data
| Kind | Example for a mark from 0 to 100 | Expected result |
|---|---|---|
| Valid | 55 | Accepted |
| Invalid | 150 | Rejected: it is outside the limits |
| Boundary | 0, 100, and the values just outside: -1 and 101 | Accepted on the edge. Rejected just outside |
| Erroneous | "abc" or nothing at all | Rejected: it is not a whole number |
The cells students fill in are left blank in this preview.
Which kind of data is it?
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Why edges matter
Many bugs are at the edges of the limits. A programmer writes age > 13 when they should write age >= 13. Test data of 5 or 15 never finds it. Only 13 does. (With age > 13, the value 13 is wrongly rejected.)
So always test the lowest and highest valid values, and the first value outside each one.
Try it: run the test data
def mark_ok(text):
return text.isdigit() and int(text) >= 0 and int(text) < 100
for data in ["55", "0", "100", "101", "abc"]:
print(data, mark_ok(data))
# One answer is wrong. Which data found the bug? What is the bug?
Make a free account to save your work and track your progress.
A test plan comes before testing
A test plan is written before the testing starts. It says what you will test, why, with what data, and what should happen.
You fill in the last column during the test. If the actual result is not the expected result, the test fails and you have found a bug.
A test plan for a mark
| Test | Purpose | Test data | Expected result | Actual result |
|---|---|---|---|---|
| 1 | Valid data in the middle is accepted | 55 | Accepted | Fill in when you run it |
| 2 | The lowest valid mark is accepted | 0 | Accepted | Fill in when you run it |
| 3 | The highest valid mark is accepted | 100 | Accepted | Fill in when you run it |
| 4 | A mark just above the top is rejected | 101 | Rejected | Fill in when you run it |
| 5 | A mark just below the bottom is rejected | -1 | Rejected | Fill in when you run it |
| 6 | Text instead of a number is rejected | abc | Rejected | Fill in when you run it |
The cells students fill in are left blank in this preview.
Quick check: the plan
Have a go
Quick check: the plan
4 questionsTry it: record the actual results
def mark_ok(text):
return text.isdigit() and int(text) >= 0 and int(text) <= 100
data = ["55", "0", "100", "101", "-1", "abc"]
expected = ["Accepted", "Accepted", "Accepted", "Rejected", "Rejected", "Rejected"]
for i in range(len(data)):
if mark_ok(data[i]):
actual = "Accepted"
else:
actual = "Rejected"
if actual == expected[i]:
print(data[i], "PASS")
else:
print(data[i], "FAIL")
# Change <= 100 to < 100. Which test fails?
Make a free account to save your work and track your progress.
Extra: Try it: a test that crashes
def mark_ok(text):
return int(text) >= 0 and int(text) <= 100
data = ["55", "101", "abc"]
expected = ["Accepted", "Rejected", "Rejected"]
for i in range(len(data)):
try:
if mark_ok(data[i]):
actual = "Accepted"
else:
actual = "Rejected"
except ValueError:
actual = "Crashed"
if actual == expected[i]:
print(data[i], actual, "PASS")
else:
print(data[i], actual, "FAIL")
# This mark_ok has no isdigit() check, so "abc" crashes it.
# A crash is always a FAIL. Write Crashed in the actual result column.
Make a free account to save your work and track your progress.
Your turn: build a test plan
Build the parts of a test plan for a field that takes a whole number from low to high, typed as text.
kind_of_data(text, low, high)gives back "valid", "invalid" or "erroneous". Text that is not a whole number is erroneous. Usetext.isdigit(). This simple check also calls a negative number such as "-1" erroneous.expected_result(text, low, high)gives back "Accepted" for valid data and "Rejected" for the rest.data_for_test(low, high)gives back six pieces of test data as text. They are: below the low edge, the low edge, the middle, the high edge, above the high edge, and "abc". For the middle use(low + high) // 2. This adds them, divides by 2 and drops any decimal part.count_failed(expected, actual)counts the places where two lists of results are different.
Press Run Tests to check.
def kind_of_data(text, low, high):
# A field takes a whole number from low to high, typed as text.
# "valid" a whole number from low to high (both included)
# "invalid" a whole number outside that range
# "erroneous" text that is not a whole number, such as "abc" or "5.5" or ""
# Use text.isdigit() to tell if the text is a whole number.
return None
def expected_result(text, low, high):
# "Accepted" for valid data. "Rejected" for everything else.
return None
def data_for_test(low, high):
# Give back a list of 6 pieces of test data, all as text, in this order:
# low - 1, low, the middle (low + high) // 2, high, high + 1, "abc"
# For 13 and 19 that is ["12", "13", "16", "19", "20", "abc"].
return None
def count_failed(expected, actual):
# expected and actual are two lists of "Accepted" and "Rejected", the same length.
# Give back how many places are different. These are the tests that failed.
return None
Make a free account to save your work, or press Start lab to work through the whole lesson.
Watch: test data and test plans
Press Play on the short film below. It sorts test data for a mark into its kinds. Then it writes a test plan, runs it and finds a bug. Last, it shows how erroneous data is different from invalid data. Watch which test finds the bug.
Film: test data and test plans
Test data and test plans
Every scene, as text
- Four kinds of test data for a mark that must be a whole number from 0 to 100. 55 is valid data, the right kind inside the limits, so it is accepted. 150 is invalid data, the right kind outside the limits, so it is rejected. Boundary data is the lowest and highest valid marks, 0 and 100, accepted, and the first marks outside them, -1 and 101, rejected: so 101 is boundary data and invalid too. Last, abc is erroneous data, the wrong kind, rejected.
- A test plan is written before any test runs: each test, its purpose, its test data and its expected result, decided before the test. The lesson's plan for the mark: test 1 is 55, in the middle, accepted; tests 2 and 3 are the edges, 0 and 100, accepted; tests 4 and 5 are just outside, 101 and -1, rejected; test 6 is abc, text instead of a number, rejected.
- Run the tests and fill in the actual results. This is the lesson's "Try it: record the actual results" with <= 100 changed to < 100, as it asks. Tests 1 and 2 pass. Test 3, 100, is rejected where it should be accepted: the actual result is not the expected one, so the test fails and a bug is found, by the top edge alone. Tests 4, 5 and 6 pass.
- Myth (false): erroneous data is the right kind of data but outside the limits. That is invalid data; erroneous is the wrong kind. The task's kind_of_data asks about the kind first: text that is not a whole number gives "erroneous". Only a whole number is checked against the limits, low to high. So kind_of_data("150", 0, 100) prints invalid and kind_of_data("abc", 0, 100) prints erroneous.
- The cure: ask about the kind first, then the limits. Not a whole number, like abc: erroneous. A whole number outside 0 to 100, like 150: invalid. A whole number from 0 to 100, like 55: valid. Boundary data is also one of these three: 101, just outside, is invalid, and 100, on the edge, is valid.
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