Skip to main content

Test Cases Before Code

Why This Matters

If you ask an AI to “write tests”, it decides what is worth testing. It will test what the code does, including its bugs. If you first write down what the code should do, you have something to judge the AI’s tests against. Ten minutes of thinking before prompting is the cheapest quality step there is.

A test case is a sentence

A test case does not need code. It needs three things:

PartQuestionExample for formatDuration
GivenWhat is the starting situation or input?A duration of 63 000 ms
WhenWhat happens?It is formatted for the table
ThenWhat exactly should the result be?1m 03s

If you cannot write the Then precisely, you do not yet know what correct means, and neither does the AI.

Three techniques that find most bugs

  1. Equivalence classes. Group inputs that should behave the same, and pick one from each group. For formatDuration: under 10 s, 10 s to a minute, a minute or more, invalid.
  2. Boundary values. Bugs live at the edges. Test just below, on, and just above each boundary: 9 999 ms, 10 000 ms, 59 499 ms, 59 500 ms, 60 000 ms.
  3. Error cases. What should happen with wrong input? A negative duration, NaN, Infinity.

Your test list for formatDuration

#Given (ms)ThenWhy
14000.4sBelow 10 s: tenths
29 99910sBoundary: rounds up across the limit. Correct? Decide!
310 00010sBoundary
442 00042sSeconds
563 0001m 03sMinutes, zero-padded seconds
6600 00010m 00sZero seconds still padded
7-1errorInvalid
8NaNerrorInvalid

Row 2 is interesting: the code returns 10s, which looks right but comes from the “under 10 seconds” branch. Is that the intended behaviour? A question like this is a finding you take to the developer or product owner. That is testing, and no AI would have asked it for you.

Use AI to challenge your list, not to write it

After you have your own list:

Here are my test cases for formatDuration (code below). Which important cases am I missing?
Do not write code. Answer as a table: input, expected output, why it matters.

Merge the good suggestions and reject the rest. You stay the owner of the list.

Exercise

  1. Write your own test list for formatDuration before looking at the table above. Compare.
  2. Ask your AI assistant to challenge your list with the prompt above. Which suggestion was useful, and which was not?
  3. Write five test cases in Given/When/Then form for the status filter on the TestRun Board page (“show only failed runs”).

Key Takeaways

  • Write test cases before any code exists, in plain Given/When/Then.
  • Equivalence classes, boundary values and error cases find most bugs.
  • Questions about intended behaviour are findings, not failures.
  • Let AI challenge your list; you remain the owner.