Testing that invalid input is rejected

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Testing that invalid input is rejected

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line tests that a validator rejects the bad age -5?
A assert is_valid(-5)
B assert not is_valid(-5)
2. True or false: a test for invalid input passes when the validator accepts the bad value.
A False
B True
3. A function should raise a ValueError for a negative age. Which structure checks this?
A if/else only
B try/except ValueError
4. Test: try: check_age(-5) then except ValueError: print('PASS'). What should happen if check_age(-5) raises NO error?
A The test fails, because the expected error never happened
B The test passes, because nothing went wrong
5. Which test data is best for testing that a validator for ages 0 to 120 rejects bad input?
A -5 and 121
B 18 and 65

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1assert not is_valid(-5) Atry: check_age(-5) print("FAIL") except ValueError: print("PASS")
2except ValueError: print("PASS") Bassert not is_valid(-5)
3To test that a validator rejects a bad value, call it with that value and assert it returns False CPasses only if is_valid(-5) returns False, so the bad value -5 is correctly rejected
4If bad input should raise an error, put the call in a try block and print PASS in the except block DThere is no FAIL message after the call in try. If no error occurs, the test never reports a failure, so a broken validator would go unnoticed.
5assert is_valid(-5) EThis asserts that the bad value is accepted. It should be assert not is_valid(-5), so the test passes only when -5 is rejected.
6try: check_age(-5) except ValueError: print("PASS") FRuns when the bad input raises a ValueError, which is the expected error, so the test passes

Definitions

assert not is_valid(-5)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
assert ___ is_valid(-5)
2.
______ not is_valid(-5)
3.
try: register(-5) except __________:
4.
assert validate(-5) == _____
5.
try: ________(-5) except ValueError:

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def is_valid(age):
B return 0 >= age <= 120
C assert not is_valid(-5)
Question 2
A def is_valid(age):
B return 0 <= age <= 120
C assert is_valid(25)
D assert is_valid(-5)
Question 3
A try:
B check_age(-5)
C print("FAIL")
D except TypeError:
E print("PASS")
Question 4
A def test_negative():
B assert not is_valid(5)
C test_negative()
Question 5
A def is_valid(pw)
B return len(pw) >= 8
C assert not is_valid("abc")

Answer Key

Testing that invalid input is rejected
Section A: Quick Questions
Q1
B — assert not is_valid(-5)
Q2
A — False
Q3
B — try/except ValueError
Q4
A — The test fails, because the expected error never happened
Q5
A — -5 and 121
Section B: Match the Pairs
Q1
assert not is_valid(-5) → Passes only if is_valid(-5) returns False, so the bad value -5 is correctly rejected
Q2
except ValueError: print("PASS") → Runs when the bad input raises a ValueError, which is the expected error, so the test passes
Q3
To test that a validator rejects a bad value, call it with that value and assert it returns False → assert not is_valid(-5)
Q4
If bad input should raise an error, put the call in a try block and print PASS in the except block → try: check_age(-5) print("FAIL") except ValueError: print("PASS")
Q5
assert is_valid(-5) → This asserts that the bad value is accepted. It should be assert not is_valid(-5), so the test passes only when -5 is rejected.
Q6
try: check_age(-5) except ValueError: print("PASS") → There is no FAIL message after the call in try. If no error occurs, the test never reports a failure, so a broken validator would go unnoticed.
Section C: Definitions
Q1
assert not is_valid(-5) — Tests that bad input is rejected: call the validator with an invalid value and assert it returns False. If an error is expected, use try/except; no error means the test fails.
Section D: Fill in the Blanks
Q1
assert not is_valid(-5)
Q2
assert not is_valid(-5)
Q3
try: register(-5) except ValueError:
Q4
assert validate(-5) == False
Q5
try: register(-5) except ValueError:
Section E: Spot the Error
Q1
B — The first comparison is the wrong way round. 0 >= age accepts negative numbers, so is_valid(-5) returns True and the assert fails. It should be 0 <= age <= 120.
Q2
D — The test expects an invalid value to be accepted. To check that bad input is rejected, it must be assert not is_valid(-5).
Q3
D — The test is looking for the wrong error type. The validation code should raise a ValueError, so the line must be except ValueError:.
Q4
B — 5 is a valid value, so this does not test invalid input. The test needs a bad value such as -5.
Q5
A — The def line is missing its colon at the end. It should be def is_valid(pw): or Python raises a SyntaxError.