──────────────────────────────────────

62. Maintain a Python Tool · Make the Bug Fail a Test

Write a test that fails for the reported result before repairing the tool.

Lesson files · lessons/maintain-a-tool/write-a-regression-test

Before you start
- Reduce a reported bug to a small input.
- Use unittest.TestCase and assertEqual to check a function’s return value.

By the end
- Add a regression test that expects the two Hall rows to produce one total.
- Use the test name and failure to show that the suite reproduced the ticket.

Where you can use it
Record the correct result as a test before a repair so the team can catch the same problem after later changes.

A regression test records a behaviour the tool needs to keep. Give it the smallest input that reproduces the ticket. Write the expected result from the ticket. Run it on the original tool and inspect the failure to see whether it reports the same problem.

The starter has one passing test for an ordinary room. Studio prints how many tests ran and names any failing assertion. In a terminal, unittest can run that same test class and show its full failure report.

Predict
What should happen when you test the combined total on the original tool?
- Both tests should pass immediately.
- The new test fails and the ordinary-room test still passes.

Worked example (example.py)

from io import StringIO
import unittest


def room_totals(rows):
    totals = {}
    for name, minutes in rows:
        totals[name] = totals.get(name, 0) + minutes
    return totals


class RoomTests(unittest.TestCase):
    def test_ordinary_room(self):
        self.assertEqual(room_totals([("Hall", 6)]), {"Hall": 6})


if __name__ == "__main__":
    suite = unittest.defaultTestLoader.loadTestsFromTestCase(RoomTests)
    result = unittest.TextTestRunner(stream=StringIO()).run(suite)
    print(f"Tests run: {result.testsRun}")
    print(f"Failures: {len(result.failures)}")
    for case, _ in result.failures:
        print(f"Failing test: {case.id().split('.')[-1]}")


Example result
Tests run: 1
Failures: 0

Keep the passing test and the original room_totals function. Add test_trailing_space to RoomTests. Call room_totals with Hall for six minutes and Hall with a trailing space for four minutes. Assert that the result is one Hall entry with ten minutes. Check that the runner names this failing test.

Starter program (starter.py)

from io import StringIO
import unittest


def room_totals(rows):
    totals = {}
    for name, minutes in rows:
        totals[name] = totals.get(name, 0) + minutes
    return totals


class RoomTests(unittest.TestCase):
    def test_ordinary_room(self):
        self.assertEqual(room_totals([("Hall", 6)]), {"Hall": 6})


if __name__ == "__main__":
    suite = unittest.defaultTestLoader.loadTestsFromTestCase(RoomTests)
    result = unittest.TextTestRunner(stream=StringIO()).run(suite)
    print(f"Tests run: {result.testsRun}")
    print(f"Failures: {len(result.failures)}")
    for case, _ in result.failures:
        print(f"Failing test: {case.id().split('.')[-1]}")


Hint
Add a new method beginning with test_ and use self.assertEqual with the result and the expected room dictionary.

Case 1. Try the sample
Input
(No input.)
Expected output
Tests run: 2
Failures: 1
Failing test: test_trailing_space

Stretch
Save your script as test_room_report.py and open a terminal in that folder. Run python3 -B -m unittest -v test_room_report, or use py on Windows. Read the failing assertion. Then repair room_totals and rerun the same command to see both cases pass.

Reflect
How does the assertion help you tell a failure about the room total from a mistake in the test itself?

Check your prediction
The new test fails and the ordinary-room test still passes. The ordinary-room test uses one input row. The new case exposes two keys where the user expects one.
