Build a regression suite around fixed bugs
Build a regression suite around fixed bugs
A regression test captures a previously broken behavior and prevents its accidental reintroduction. First reproduce the bug with a minimal failing test, then fix the implementation, then keep the test. Prefer a named case that records the actual contract over a test of the patch itself. Add neighboring boundary values so the fix does not merely special-case the original input.
Create the files shown below in a fresh directory, then run go test -count=1 -v ./... there. The separator lines are file labels, not source code. Go 1.23 is used for these local examples.
--- go.mod ---module example.test/lesson
go 1.23
--- lesson.go ---package lesson
func CanReserve(stock, requested int) bool { return requested > 0 && requested <= stock }
--- lesson_test.go ---package lesson
import "testing"
func TestLesson(t *testing.T) { for _, c := range []struct { name string stock, n int want bool }{{"zero_request", 5, 0, false}, {"exact_stock", 5, 5, true}, {"over_stock", 5, 6, false}, {"negative_request", 5, -1, false}} { t.Run(c.name, func(t *testing.T) { if CanReserve(c.stock, c.n) != c.want { t.Fatal(c) } }) }}An earlier predicate requested <= stock would incorrectly accept zero and negative quantities. The tests protect both the fixed case and adjacent valid behavior. In the evidence suite, the old predicate is deliberately restored and the negative test must fail. A passing test that never fails on the original bug is weak evidence.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonKeep an observed input as a named regression
--- go.mod ---module example.test/lesson
go 1.23
--- lesson.go ---package lesson
func SplitLabel(s string) (string, string, bool) { for i, c := range s { if c == ':' { if i == 0 || i == len(s)-1 { return "", "", false } return s[:i], s[i+1:], true } } return "", "", false}
--- lesson_test.go ---package lesson
import "testing"
func TestLesson(t *testing.T) { for _, c := range []struct { input, key, value string ok bool }{{"key:a:b", "key", "a:b", true}, {"key:", "", "", false}, {":value", "", "", false}, {"plain", "", "", false}} { k, v, ok := SplitLabel(c.input) if k != c.key || v != c.value || ok != c.ok { t.Fatal(c, k, v, ok) } }}This example defines the first colon as the separator and rejects empty sides. A previously observed input key:a:b exposes implementations that split at every colon and discard later data. Keep inputs synthetic, small and independent; run the fast regression suite on every change and run heavier integration cases separately. A regression suite grows with real failures, not redundant snapshots.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: TestLessonTry it locally
Write a regression suite for an inclusive valid page size of 1 through 100. Assert 0, 1, 100 and 101. Check that the old strict upper bound would fail. Use a fresh module with the same go.mod shown above; run go test -count=1 -v ./...
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Advanced testing 06: Build a regression suite around fixed bugs
Check the lesson objective before moving on.
What should happen when a regression test runs against the original defect?
Why test the adjacent valid value?