By the end of this lesson
Write a small test suite that reports a failing assertion.
Before you start
- Python 3.12 or a compatible newer Python 3 interpreter; no external packages.
- Complete lesson 19 in this route, or be comfortable with its examples.
Three words to know
- Test case
- A repeatable check of a behaviour.
- Assertion
- A comparison that fails the test if the expectation is not met.
- Regression
- A previously working behaviour broken by a later change.
1. Test a rule, not a print statement
Save this complete file as lesson.py and run it. Test methods start with test_. Each method explains one expected behaviour. The runner discovers methods on the TestCase subclass; its result is usually written to stderr, so it may look different from the stdout examples in earlier lessons.
import unittest
def count_open(statuses):
return sum(1 for status in statuses if status == "open")
class CountTests(unittest.TestCase):
def test_mixed(self):
self.assertEqual(count_open(["open", "closed", "open"]), 2)
def test_empty(self):
self.assertEqual(count_open([]), 0)
def test_case_is_exact(self):
self.assertEqual(count_open(["Open"]), 0)
if __name__ == "__main__":
unittest.main()
2. A failure is useful evidence
Temporarily change the mixed-case expectation from 2 to 3 and rerun. You should see a failed assertion showing the expected and actual values. Restore 2 after observing the failure. Do not change an assertion just to make a broken implementation pass; first decide which behaviour is correct.
3. Expand coverage deliberately
A passing test suite confirms the cases you wrote, not all possible inputs. Useful boundaries include empty data, first and last allowed values, missing fields and malformed input. The final project tests real process output and exit status as well as the counting function. Network calls are unnecessary for those local behaviours.
YOUR TURN
A small check before you continue
Add a test for ["closed", "closed"]. What should it assert?
Show a suggested answer
It should assert that count_open returns 0. The function counts only exact open values; it does not count the number of records overall.
Sources and scope
Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.
