Definition
A regression test is a test that is kept and rerun after every change to a program, so that behavior that once worked, or a defect that was once fixed, cannot go wrong again unnoticed. A regression is a change that makes previously correct behavior incorrect. In the broad sense, regression testing reruns the existing tests after a change. In the narrow sense, a regression test is written when a defect is fixed: it fails on the faulty version and passes on the corrected one.
The idea
Fixed defects come back. Someone undoes the fix by mistake, brings an old copy of the code back in, rewrites a function and drops a check whose purpose was not obvious, or copies the faulty code to a new place. A regression test turns what was learned from the defect into a check that runs automatically on every later version.
A guard posted at a door that a burglar once used is a fair picture. The guard must stop anyone who comes through that door, not only people who look like that burglar. A regression test likewise has to check the rule that the defect broke, such as “a name too long for the buffer must be refused”, not an accidental symptom of one occurrence, such as “the program did not crash”. The picture stops at one point: a guard sees every visitor, while a test checks only the inputs it runs.
How it works
Reproduce. Find an input and a setup that make the failure happen. Shrink the input if it is large. Delta debugging §10.39 is an algorithm §14.1 that does this: it repeatedly removes parts of a failing input and keeps each removal after which the failure still occurs.
Find the cause. Name the rule that the program broke, for example a length check that is off by one. The crash, the corrupted value or the error message is a symptom of that cause.
Write the assertions against the rule. Check the required behavior, such as refusal with nothing stored, at the failing input and at its neighbors. Boundary-value testing §10.31, which picks inputs at and on each side of every limit, supplies the neighbors.
Check fail before, pass after. Run the new test on the faulty version and on the corrected one. It must fail on the first and pass on the second. A test that passes on the faulty version does not test this defect.
Keep the test and run it on every change, usually by continuous integration, a service that builds and tests every change automatically. When the suite grows large, regression test selection runs only the tests that a change can affect, and test prioritization runs first the tests most likely to fail.
When an old test starts to fail and the change that broke it is unknown, the history of changes can be searched by halving, which the command git bisect of the version control system Git automates. The search tests the version in the middle of the suspect range and keeps the half that contains the first failing version. Among 1000 candidate changes, 10 test runs suffice, because each run halves the range and . The search assumes that the failure is reproducible and that every version after the breaking change fails.
Worked example
A 16-byte buffer holds a name followed by a zero byte that marks the end of the name, as strings in C do. A name of characters therefore needs bytes, so the rule is , that is, . The faulty code refused a name only when . A 16-character name was therefore accepted, and it needed bytes: the characters went to positions 0 to 15 and the zero byte to position 16, one past the last valid position. This is a buffer overflow §4.4, a write outside the memory reserved for a buffer. In one build, the byte after the buffer was unused padding, a filler byte the compiler left between variables, and nothing visible happened. In another build, it held part of a neighboring variable, and the device crashed.
A symptom test sends a 16-character name and checks that the program does not crash. In the build with padding, it passes even on the faulty version, so it fails the fail-before check. It would also pass after a wrong repair that silently cuts the name to 15 characters when the requirement is to refuse it.
A cause test checks the rule at the boundary with : names of 14 and 15 characters must be accepted and stored exactly, and names of 16 and 17 characters must be refused with nothing stored. The table shows how five versions of the code behave.
| version | ||||
|---|---|---|---|---|
| required | accept | accept | refuse | refuse |
| faulty: refuse if | accept | accept | accept | refuse |
| overstrict: refuse if | accept | refuse | refuse | refuse |
| truncating: cut to 15 characters | accept | accept | cut | cut |
| correct: refuse if | accept | accept | refuse | refuse |
The cause test fails on the faulty version (at ), on the overstrict repair (at ) and on the truncating repair (at and ). It passes only on the correct version.
A tool can strengthen the test. AddressSanitizer §10.22 is a checker that the compiler builds into the program on request; it checks every memory access and reports any access outside an object. On the faulty version, it reports the 1-byte overflow at whenever the buffer is a variable or an allocation of its own, whatever lies next to it in memory. It does not see an overflow into the next field of the same structure.
Stated precisely
Let be the faulty version, the corrected version, the defect, and a test with oracle . Then is a regression test for when three conditions hold.
fails on .
passes on .
checks the rule that broke, so that condition 1 holds because of and not because of an incidental effect, such as a crash at one address.
For every later version , a failure of means that breaks the rule on the inputs of , or that itself is broken. A pass means only that keeps the rule on those inputs. A practical check of condition 3 is to put the original defect back, or a close variant such as in place of , and confirm that fails. Mutation testing §10.45 does this systematically: it makes many small deliberate changes to the code and counts how many of them the tests detect.
Limits and common mistakes
A regression test guards the inputs it runs. The same kind of mistake elsewhere, such as a second copy routine with the same off-by-one check, is not covered. Property-based testing §10.7, which checks a stated rule on many generated inputs, and fuzzing §10.9, which feeds a program large numbers of generated inputs and watches for failures, search more widely.
Shrinking a reproducer can remove what the failure needs, such as a particular timing, an earlier state or a long sequence of messages. Keep the setup that makes the test meaningful, and record why the test exercises the cause.
An intermittent failure that does not occur in one run has not been shown to be fixed by that run. Repeat the run many times, or remove the variation, for example by deterministic replay §10.37, which reruns a recorded execution with the same inputs and the same order of events.
A test that compares output with output recorded from an earlier version, often called a golden or snapshot test, detects change, not wrongness. If the recorded output was wrong, the test protects the wrong behavior.
Loosening or deleting a failing regression test to let a change through removes the protection it gave. Treat a failing regression test as evidence of a regression until it is shown to be otherwise.
Slow suites get skipped, and tests that fail at random teach people to rerun until they pass, which also hides real regressions. A regression suite protects only while it is fast and deterministic enough to be trusted.
Questions answered
Does the regression check the cause or only one accidental symptom?
A regression test checks the cause when its assertions state the rule that the defect broke and it fails on the faulty version because that rule is broken. It checks only a symptom when it asserts an incidental effect of one occurrence, such as the absence of a crash, a particular error message or a value at a particular address. Three questions decide which:
Does the test fail on the faulty version in every build, not only in the build that crashed?
Would it fail on plausible wrong repairs, such as hiding the symptom by silently truncating the input, or making the check too strict?
Does it check the boundary from both sides, with the last valid input and the first invalid one?
For example, if a 16-byte buffer must hold a name and the zero byte that ends it, the rule is that names of at most 15 characters are accepted and longer ones refused. A test that only checks “no crash on a 16-character name” passes in a build where the overflowing byte lands in unused memory. A test that requires acceptance at 14 and 15 characters, and refusal with nothing stored at 16 and 17, fails on the faulty code, on a truncating repair and on an overstrict repair, so it checks the cause.
Related entries: Boundary-value testing (§10.31), Delta debugging (§10.39), Mutation testing (§10.45), Buffer overflow (§4.4), Test oracle (§10.5), Deterministic replay (§10.37).