question 248 Testing & Code Quality · medium
Test restock alerts with a Mock
restock_alerts(products, threshold, notify) calls notify(sku, stock) once for every product whose stock is below the threshold, in order, and returns how many alerts it sent. In production notify sends a message to the warehouse team. Write TestRestockAlerts, using Mock as notify, so that broken versions are caught and no real message is ever sent.
Write
class TestRestockAlerts(unittest.TestCase) using unittest.mock.Mock for notifyRulestock below
threshold → notify(sku, stock); stock equal to the threshold → no alert; returns the number of alertsUseful
mock.call_args_list == [call("SPO-11681", 2), …] with from unittest.mock import Mock, call; mock.assert_not_called()