04 / REUSABLE INVESTIGATION TOOLS · LESSON 15

Test the boundary of a detection rule

Write tests that accept a correct detector and expose realistic faulty implementations.

18 min · learn at your pace

A passing test can still be weak

A detector that uses > instead of >= might pass a test with ten failures and a threshold of three. It fails only at the boundary. Choose examples that distinguish the promised behavior from plausible mistakes: below, exactly at, and above the threshold.

The supplied detector.py implements a tiny rule. Your task is to test it, not rewrite it. Import meets_threshold into main.py, define a unittest.TestCase subclass, and put any unittest.main() call behind the main guard. That lets the browser checker discover your tests without starting another test runner unexpectedly.

Test the contract, including invalid configuration

Counts must be nonnegative integers and thresholds positive integers. Booleans and fractional values are invalid. Use assertRaises(ValueError) for those inputs and assertTrue or assertFalse for valid decisions. Exercise more than one threshold so a hard-coded result cannot pass.

Automatic checking runs your suite against the correct function and against deliberately faulty replacements. This technique asks whether your tests can detect meaningful defects. It does not require particular test names or a particular number of your own methods, provided the suite actually runs and rejects the faults.

Do not skip tests or label expected failures. Keep assertions independent of printed terminal text. A useful test explains which contract failed and gives you confidence to change implementation details later.

Try an example.py
import unittest
from detector import meets_threshold

class BoundaryTest(unittest.TestCase):
    def test_equal(self):
        self.assertTrue(meets_threshold(3, 3))

if __name__ == "__main__":
    unittest.main()
YOUR TURN

Put it into practice.

Write unittest.TestCase tests in main.py for the supplied detector.meets_threshold(count, threshold), imported as meets_threshold. The correct rule is count >= threshold; count must be a nonnegative int and threshold a positive int, with bools/floats invalid (ValueError). Cover below, equal, above, different thresholds and invalid values. Your suite must pass the correct detector and reject strict-greater, always-true, fixed-threshold and permissive-validation replacements. Do not change detector.py, skip tests, or use expected failures. Guard unittest.main().

See an explained solution
One way to solve it.py
import unittest
from detector import meets_threshold

class DetectionTests(unittest.TestCase):
    def test_below(self):
        self.assertFalse(meets_threshold(2, 3))

    def test_exact_boundary(self):
        self.assertTrue(meets_threshold(3, 3))

    def test_above_and_different_policy(self):
        self.assertTrue(meets_threshold(8, 3))
        self.assertFalse(meets_threshold(3, 5))

    def test_invalid_settings(self):
        for count, threshold in [(-1, 3), (2, 0), (2, True), (True, 2), (1.5, 3), (2, 1.5)]:
            with self.subTest(count=count, threshold=threshold):
                with self.assertRaises(ValueError):
                    meets_threshold(count, threshold)

if __name__ == "__main__":
    unittest.main()

Boundary and invalid-input tests distinguish the correct rule from realistic faults. The browser checks the usefulness of your tests rather than requiring a specific test layout.