Brief
Write tests for ordinary results, boundaries and impossible inputs, and keep a check so a fixed bug cannot quietly return.
In the evidence room, Mara has ten minutes in the story before the meeting. Voss has prepared an accusation against your tool. Three batches arrive: A has 6 rows and 5 IDs; B has 0 and 0; C has 2 rows and 3 IDs. An old bug returned -1 for C. Prepare checks Mara can show Imani.
Your mission
- Calculate A’s expected result and write an
assert. - Give B’s expected reaction and explain why it is valid.
- Give C’s expected reaction and show the matching
unittestcheck. - Name the test to keep after the fix and say what it cannot prove.
How to check your work
- A gives 1 and
assert repeats(6, 5) == 1passes. - B gives 0: no receipt in an empty batch can repeat.
- C raises
ValueError, never -1. - The rejection test for C stays in the suite; receipt provenance needs a separate inquiry.
Course example
Workspace version: the three tests are loaded explicitly from their class, then run in the browser.
import unittest
def repeats(rows, distinct):
if rows < 0 or distinct < 0 or distinct > rows:
raise ValueError('impossible count')
return rows - distinct
assert repeats(5, 4) == 1
class TestRepeats(unittest.TestCase):
def test_normal(self):
self.assertEqual(repeats(5, 4), 1)
def test_boundary(self):
self.assertEqual(repeats(0, 0), 0)
def test_invalid(self):
with self.assertRaises(ValueError):
repeats(3, 4)
if __name__ == '__main__':
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestRepeats)
result = unittest.TextTestRunner().run(suite)
assert result.wasSuccessful()