Lesson 11 of 12
Structured learning draftTest a rule at its boundary
A stock rule can look correct until an item sits exactly at the reorder threshold. Write automated tests for values below, at and above the threshold, so a later edit that changes equality is detected immediately.
Learning objectives
- Encode an expected result in a test.
- Cover an equality boundary.
- Recognise a regression after changing a rule.
Test a rule at its boundary: the working sequence
Trace the three stages of test a rule at its boundary and compare them with the example.
The requirement says reorder at or below the threshold.
Three assertions test below, equal and above the threshold.
Changing <= to < makes the equality assertion fail.
A test compares an observed result with a stated expectation. Here the expected boundary behaviour is part of the requirement. Test discovery and test execution need a documented command; an unexecuted test file proves nothing.
import unittest
def needs_reorder(quantity, threshold):
return quantity <= threshold
class ReorderTests(unittest.TestCase):
def test_boundary(self):
self.assertTrue(needs_reorder(4, 5))
self.assertTrue(needs_reorder(5, 5))
self.assertFalse(needs_reorder(6, 5))
if __name__ == '__main__':
unittest.main()One test runs and passes: the quantities 4, 5 and 6 match the required rule. The runner's timing and formatting vary by environment.Try it, then change it
- Save the file as test_reorder.py and run python test_reorder.py.
- Change <= to <, run again and read the failing assertion.
- Restore the correct rule and verify the test passes again.
Common mistakes
- Testing only the obvious successful case.
- Changing a failed expectation to match a bug without reconsidering the requirement.
- Using assert statements as a substitute for validating untrusted production input.
Practice activity
Apply the lesson
Test remaining(opening, received, sold) for a normal result and zero balance. Decide whether negative inputs are permitted and write a test for your policy. Keep the changed example, its output and a short explanation of whether the result matched your prediction.
How to check your work
- Write down the expected result before running or submitting your work.
- Try a normal example, an empty or missing input, and a boundary case where the rule changes.
- Compare the actual result with your prediction. Record the smallest example that explains a difference.
- Explain one change you made and why it improved the result. Save your evidence in the lesson notes.
When you get stuck · Python
The script stops with a traceback or gives an unexpected value.
- Find the cause
- Read the exception type and message on the last line, then find the referenced line in your own file. Compare the value and type of each input with what that operation expects.
- Try this
- Reduce the failing case to a few lines. Check variable spelling, numeric conversion and file paths. Handle only the exception you expect; do not hide an unknown error with a broad except block.
- Check the fix
- Run the corrected case and a deliberately invalid input. The valid input produces the expected result; the invalid input gives a clear message or raises the documented exception.
Use this course-wide example when it fits your task. For a different failure, record the input, expected result, actual result and smallest steps that reproduce it.
Watch, pause, practise
Free video lessons
Choose the chapter that matches your task. Videos open on the original provider’s site; playback uses additional data.
CS50: programming with Python
Choose the lecture on functions, loops, exceptions, tests or files that matches your current task.
Try it yourself: Before running an example, predict its output. Try an empty input and record how the program behaves.
Source checked 2026-10-07 · Free learning material; optional certificates may cost extra.Check your understanding
Which input exposes changing <= to < in the reorder rule?
Lesson summary
- Tests express a requirement with a known expectation.
- Equality cases catch common boundary errors.
- A regression test must run after the relevant change.
Sources and further reading
- unittest: unit testing frameworkPython Software Foundation - accessed 2026-10-07
- Python language referencePython Software Foundation - accessed 2026-10-07
Personal study note
Your notes
Keep the input, expected result, actual result and what you changed. Save before leaving this lesson. Download a copy to keep it outside this browser.