runtime
246 / 500
question 246 Testing & Code Quality · medium

A table of tests for durations

Delivery slots are written like 1h30m, 45m or 2h. parse_duration(text) turns one into minutes and is correct. Write TestParseDuration with table-driven tests (subTest) for the texts it must accept and for the texts it must refuse, so that broken versions are caught.

Writeclass TestParseDuration(unittest.TestCase) with a table of valid texts and a table of invalid ones, each checked under subTest
Acceptshours and/or minutes, in that order, with optional spaces around the whole text: "1h30m" → 90, "45m" → 45, "2h" → 120, "90m" → 90, " 1h5m " → 65
Refuses (ValueError)an empty text, a number without a unit ("30"), minutes before hours ("30m1h"), and 60 or more minutes after hours ("1h75m")

Loading your progress…

A table of tests for durations — Python practice · Runtime