QA GLOSSARY · TERM 3 OF 6

Off-by-one error

An off-by-one error is a mistake of exactly one step at an edge: a greater-than sign where a greater-than-or-equal was meant, or a loop that runs one time too many or too few. The free-shipping bug in this site's QA challenge is one: the shop says free at $50 or more, and the code says more than $50.

Updated 4 December 2026

A real example

In the QA challenge shipping is meant to be free on orders of $50 or more. The code says "more than $50" instead. For every order except one exact amount the shop behaves correctly, so the bug survives casual testing. At exactly $50.00 a $5 fee is still added.

The shape of the bug (not the challenge's actual code)
// the rule on the page: free shipping at $50 or more
if (subtotal > 50)  shipping = 0;   // wrong: an order of exactly $50.00 still pays $5
if (subtotal >= 50) shipping = 0;   // right

One character is the whole difference. That is typical: an off-by-one error is small in the code and large in effect, because it hits the customers who happen to be on the line.

Where else they hide

  • Loops and lists: counting from 0 or from 1, stopping before the last item or one after it.
  • Limits: a text field that allows 100 characters and refuses the 100th, a password rule of "at least 8" coded as more than 8.
  • Pages and slices: the first and last item of each page of results, or an "items 1 to 10" list that shows nine.
  • Date ranges: a report "from the 1st to the 31st" that stops on the 30th.

How a test finds one

Test at the edge. That is the whole method, and it has a name: boundary value analysis. Try the value just below the limit, the value on it, and the value just above it. An off-by-one error usually fails on the value that sits on the line, and passes on the other two. This is the check from the challenge:

The check for the $50 bug, from the QA challenge
await setCart(page, { notebook: 2, pens: 0 }); // $50.00
await expect(page.getByTestId('shipping')).toHaveText('Free');

Two orders either side ($49.99 and $50.01) would pass with the broken code, and so would any number of typical orders. Only the value on the line fails, which is why it is worth a test of its own.

Edges in a real tool

Every threshold in the free website health check is a place where an off-by-one error could live, and is worth testing on both sides of the line. For example, an HSTS header with a lifetime of 15,552,000 seconds (180 days) passes, and one with 15,551,999 gets a warning. A page of 1,399 bytes sent without compression passes, and one of 1,400 bytes fails. The guide to slow server response, compression and redirects lists them.

Questions people ask

Why is it called off-by-one?

Because the result is wrong by one: one too many, one too few, or one step on the wrong side of a limit.

Is it only a programming mistake?

It is usually coded that way, but it often starts in a requirement written two ways: "over 50" and "50 or more" mean different things. Asking exactly which one is meant is a test in itself.

Will a code review catch it?

Sometimes. It is easy to read past a sign that looks right. A test at the exact limit catches it every time it runs, which is why the check is worth keeping as a regression test.

Where the facts come from

See also on this site