Test failures at the scanner boundary
Write tests that catch real scanner regressions without creating network connections.
Test the contract through an adapter
A test double can return a known observation, record its arguments or raise a specific exception. That makes network policy testable without relying on a machine being online or a port remaining open. Import the supplied scanner_core module and call its scan function from a unittest TestCase named ScannerTests.
Exercise more than the successful path. Confirm stable endpoint ordering, a timeout that remains timed_out, scope validation before any probe and cancellation that propagates. These behaviors are independent: a scanner can produce a correct-looking open result while still probing an invalid target first or swallowing an interruption.
Make tests prove that they can fail
The course check first runs your test suite against the correct scanner. Then it substitutes deliberately faulty implementations. Your tests must reject reversed output, timeout misclassification, ignored scope and swallowed cancellation with assertion failures. An import error or a test crash does not demonstrate that a behavioral regression was detected.
Reference scanner_core.scan through the module rather than caching a from-import alias. The exercise swaps that module attribute to supply faults. In a larger project, a similar boundary can use dependency injection or patch where a name is looked up.
The main guard uses an explicit TestLoader and TextTestRunner. That works in this browser's execution namespace as well as a normal local script. All checks use recorded observations; none opens a socket or depends on a real network timeout.
import unittest
class Example(unittest.TestCase):
def test_state(self):
self.assertNotEqual("timed_out", "refused")
suite=unittest.defaultTestLoader.loadTestsFromTestCase(Example)
unittest.TextTestRunner().run(suite)
Put it into practice.
Define ScannerTests(unittest.TestCase), with at least four executed tests and no skips or expected failures. Import scanner_core and call scanner_core.scan dynamically. Verify numeric plan order, timeout classification, full scope validation before probes, and KeyboardInterrupt propagation. Your suite must pass the correct implementation and fail assertions against four supplied behavioral mutants, without errors. Keep the explicit main-guard TestLoader/TextTestRunner so Run executes the suite.
See an explained solution
import unittest
import scanner_core
class ScannerTests(unittest.TestCase):
def test_order(self):
rows=scanner_core.scan(["192.0.2.1"],[443,22],["192.0.2.0/24"],lambda *args:{"state":"open","elapsed_ms":1})
self.assertEqual([row["port"] for row in rows],[22,443])
def test_timeout(self):
def probe(*args):raise TimeoutError()
rows=scanner_core.scan(["192.0.2.1"],[80],["192.0.2.0/24"],probe)
self.assertEqual(rows[0]["state"],"timed_out")
def test_scope_before_probe(self):
calls=[]
with self.assertRaises(ValueError):
scanner_core.scan(["192.0.2.1","198.51.100.1"],[80],["192.0.2.0/24"],lambda *args:calls.append(args))
self.assertEqual(calls,[])
def test_cancellation(self):
def probe(*args):raise KeyboardInterrupt()
with self.assertRaises(KeyboardInterrupt):
scanner_core.scan(["192.0.2.1"],[80],["192.0.2.0/24"],probe)
if __name__ == "__main__":
suite=unittest.defaultTestLoader.loadTestsFromTestCase(ScannerTests)
unittest.TextTestRunner().run(suite)
Behavioral tests remain independent of real networking. Running the same suite against faulty adapters demonstrates that its assertions detect meaningful regressions.