Test route boundaries with unittest
Write assertions that catch endpoint exclusion, string-prefix matching and incorrect route scope.
Route membership is not a host-allocation question
A route prefix covers its entire address range, including the numeric endpoints. That is different from choosing usable host addresses within a subnet. A detector that subtracts network and broadcast addresses can incorrectly exclude destinations from a route assessment.
Test ordinary prefixes, /31 and /32 cases, default routes and IPv6. Include a textual lookalike such as 192.0.20.1 when checking 192.0.2.0/24. A string-prefix implementation might mistake those for the same range.
A passing test suite needs useful assertions
The supplied subnet_rule.route_contains parses strict networks, returns a Boolean for valid input and raises ValueError for invalid text or host bits in a prefix. Different address families return False. Write tests for positive, negative and invalid-input cases.
The course checker runs your suite against the correct helper and then against three realistic mutations: excluding endpoints, matching address text and accepting every address in the same family. Each mutant must produce an assertion failure in your tests. Do not patch the helper yourself.
In the main guard, explicitly load your RouteTests class with unittest.defaultTestLoader.loadTestsFromTestCase and run that suite with TextTestRunner. Explicit loading works in this browser execution namespace and in a standalone script. It also makes the Run output show exactly which tests were executed.
from subnet_rule import route_contains
print(route_contains("192.0.2.0/24", "192.0.2.255"))
print(route_contains("192.0.2.0/24", "192.0.20.1"))
Put it into practice.
Write unittest.TestCase tests for subnet_rule.route_contains(prefix, address). It uses strict network parsing; invalid values/host bits raise ValueError, different families return False, and valid membership includes both endpoints. Cover IPv4/IPv6, outside addresses, /31, /32 and default routes. Your suite must pass the supplied helper and fail assertions for endpoint exclusion, textual-prefix matching and same-family-always-True mutations. Do not replace or patch the helper. Use a main guard that explicitly loads your TestCase and runs TextTestRunner.
See an explained solution
import unittest
from subnet_rule import route_contains
class RouteTests(unittest.TestCase):
def test_ipv4_endpoints_and_outside(self):
self.assertTrue(route_contains("192.0.2.0/24","192.0.2.0"))
self.assertTrue(route_contains("192.0.2.0/24","192.0.2.255"))
self.assertFalse(route_contains("192.0.2.0/24","192.0.20.1"))
def test_small_and_default_prefixes(self):
self.assertTrue(route_contains("192.0.2.10/31","192.0.2.11"))
self.assertTrue(route_contains("192.0.2.9/32","192.0.2.9"))
self.assertFalse(route_contains("192.0.2.9/32","192.0.2.10"))
self.assertTrue(route_contains("0.0.0.0/0","198.51.100.3"))
def test_ipv6_and_different_families(self):
self.assertTrue(route_contains("2001:db8::/32","2001:db8::1"))
self.assertFalse(route_contains("2001:db8::/32","2001:db9::1"))
self.assertFalse(route_contains("0.0.0.0/0","2001:db8::1"))
def test_invalid_inputs(self):
for prefix, address in [("192.0.2.1/24","192.0.2.1"),("bad","192.0.2.1"),("192.0.2.0/24","bad")]:
with self.subTest(prefix=prefix,address=address):
with self.assertRaises(ValueError):
route_contains(prefix,address)
if __name__ == "__main__":
suite = unittest.defaultTestLoader.loadTestsFromTestCase(RouteTests)
unittest.TextTestRunner(verbosity=2).run(suite)
The suite tests the helper’s public behavior rather than its implementation. Mutations demonstrate that its assertions detect realistic mistakes at boundaries and across address families.