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).
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
assert not is_valid(-5)Fill in the Blanks
Spot the Error
Student with a class code? Join your class →