Skip to the guide

Writing tests fast: the cases that catch bugs

IntermediateConcept7 pages

In a machine-coding round you have time for few tests, so each one must catch something the demo can't. This page shows which tests those are, and in what order to write them. Interviewers want to see that you test the edges of your rules as well as the normal case.

The height stick at a fairground ride

A new ride at a fairground has one rule on its sign: riders must be at least 120 cm tall. The gates open soon, so the operator wants to check her height stick quickly. A tall man walks through first, and the stick lets him on. That tells her almost nothing, because a badly marked stick would let him on too.

So she stands a child who is exactly 120 cm against the stick, and then a child who is 119 cm. If the mark is one centimetre out, one of those two children shows it, and nobody else in the queue would. Then she checks what happens after the stick says no. The child who was turned away must not have used up a seat.

The operator is you, and the sign's rule is a rule in the problem statement. This page's project is a community shed that lends tools, where a member may hold 2 tools at once and each loan lasts 7 days. The mark on the stick is a number in the code, MAX_TOOLS or LOAN_DAYS. The tall man is the happy-path test, the normal case that works. The two children are tests at the line, on the exact limit and just past it. The seat that stays free is a refusal test, which checks that a refused command changed nothing.