runtime
248 / 500
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.

Writeclass TestRestockAlerts(unittest.TestCase) using unittest.mock.Mock for notify
Rulestock below threshold → notify(sku, stock); stock equal to the threshold → no alert; returns the number of alerts
Usefulmock.call_args_list == [call("SPO-11681", 2), …] with from unittest.mock import Mock, call; mock.assert_not_called()

Loading your progress…

Test restock alerts with a Mock — Python practice · Runtime