← ACARA Digital Technologies Years 9–10

Testing that invalid input is rejected

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.

Understanding Testing that invalid input is rejected in ACARA Digital Technologies Years 9–10

What it does: Calling is_valid with -5 and using assert not confirms that the invalid value is rejected. The call that should fail goes in try, and the expected error is caught by except, which prints PASS. If the validator accepts a bad value, it is not doing its job. The test should fail so you know to fix the code. The test expects an error. If the bad input is accepted without an error, the validation is broken, so the test must fail. An empty name is invalid, so the validator should return False. 'assert not' checks for this. Leaving out not reverses the test, so it fails when the validator works correctly.

Common mistakes: 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. The test expects an invalid value to be accepted. To check that bad input is rejected, it must be assert not is_valid(-5).

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 591–622) and word search (Modules 536–622).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line tests that a validator rejects the bad age -5?
assert is_valid(-5)assert not is_valid(-5)
Matches
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.
Applying
Meanings
Term
assert not is_valid(-5)
Meaning
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.
Practice
def is_valid(age):
Testing
Gaps
assert isvalid(-5)
Check Answer
Spots
1def is_valid(age):
2 return 0 >= age <= 120

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Camel' badge — Endurance Champion, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line tests that a validator rejects the bad age -5?
A. assert is_valid(-5)
B. assert not is_valid(-5) ✓
A bad value should be rejected, so is_valid(-5) should return False. 'assert not' passes only when the result is False.
2. True or false: a test for invalid input passes when the validator accepts the bad value.
A. False ✓
B. True
If the validator accepts a bad value, it is not doing its job. The test should fail so you know to fix the code.
3. A function should raise a ValueError for a negative age. Which structure checks this?
A. if/else only
B. try/except ValueError ✓
Put the call inside try and catch ValueError in except. That shows the expected error really happened.
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
The test expects an error. If the bad input is accepted without an error, the validation is broken, so the test must fail.
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
Invalid tests need values outside the allowed range. -5 and 121 should be rejected, but 18 and 65 are valid.

Match the Pairs

assert not is_valid(-5) → Passes only if is_valid(-5) returns False, so the bad value -5 is correctly rejected
except ValueError: print("PASS") → Runs when the bad input raises a ValueError, which is the expected error, so the test passes
To test that a validator rejects a bad value, call it with that value and assert it returns False → assert not is_valid(-5)
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")
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.
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.

Key Term

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.

Fill in the Blanks

1. assert not is_valid(-5)
Hint: Type the keyword that reverses True/False so a rejected bad value makes the test pass.
2. assert not is_valid(-5)
Hint: Type the keyword that stops the program with an error if the condition after it is not true.
3. try: register(-5) except ValueError:
Hint: Type the error type the function should raise when given a bad value.
4. assert validate(-5) == False
Hint: Type the Boolean value a validator should return for invalid input.
5. try: register(-5) except ValueError:
Hint: Type the name of the function being tested, which should raise an error for the bad value.

Spot the Error

Question 1
A def is_valid(age):
B return 0 >= age <= 120
C assert not is_valid(-5)
Line 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.
Question 2
A def is_valid(age):
B return 0 <= age <= 120
C assert is_valid(25)
D assert is_valid(-5)
Line 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).
Question 3
A try:
B check_age(-5)
C print("FAIL")
D except TypeError:
E print("PASS")
Line D — The test is looking for the wrong error type. The validation code should raise a ValueError, so the line must be except ValueError:.
Question 4
A def test_negative():
B assert not is_valid(5)
C test_negative()
Line B — 5 is a valid value, so this does not test invalid input. The test needs a bad value such as -5.
Question 5
A def is_valid(pw)
B return len(pw) >= 8
C assert not is_valid("abc")
Line A — The def line is missing its colon at the end. It should be def is_valid(pw): or Python raises a SyntaxError.

Student with a class code? Join your class →