Property-Based Testing Implementation in Go
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Property-Based Testing runs in O(n) time and O(n) space, in Go as in every other language.
- ✓Go is the one language here that ships both. `testing/quick` (`quick.Check`, `quick.CheckEqual`, `quick.Config`, the `quick.Generator` interface) is reflection-driven, frozen, and does NOT shrink.
- ✓Table-driven tests stay; the fuzz target sits beside them in the same `_test.go` file and seeds itself with `f.Add` for the cases you already know, so `go test ./...` still exercises them without `-fuzz`.
- ✓The Go trap: `f.Fuzz` accepts only a fixed set of argument types -- `[]byte`, `string`, `bool`, `rune`, `byte`, `int`/`uint` and every sized int/uint, `float32`, `float64`.
- ✓`go test -fuzz` rebuilds the package with coverage instrumentation and runs `$GOMAXPROCS` worker processes, so the target itself is measurably slower than the same code under `go test` and the wall-clock budget is `-fuzztime`, not an example count.
What Property-Based Testing actually is
Property-Based Testing in Go: what you actually reach for
Go implementation
package codec
import (
"bytes"
"testing"
"testing/quick"
)
// Native fuzzing: f.Fuzz only accepts []byte, string, bool, rune, byte,
// int/uint and every sized int/uint, float32 and float64. A struct must
// be decoded out of one of those.
func FuzzRoundTrip(f *testing.F) {
f.Add([]byte("seed"))
f.Add([]byte{})
f.Fuzz(func(t *testing.T, in []byte) {
got, err := Decode(Encode(in))
if err != nil {
t.Fatalf("decode(%q): %v", in, err)
}
if !bytes.Equal(got, in) {
t.Fatalf("round-trip: got %q want %q", got, in)
}
})
}
// testing/quick: reflection-driven, no shrinking, keep the property tiny.
func TestSortedPreservesLength(t *testing.T) {
prop := func(xs []int) bool { return len(Sorted(xs)) == len(xs) }
if err := quick.Check(prop, &quick.Config{MaxCount: 1000}); err != nil {
t.Error(err)
}
}
Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Go-specific trap
Performance in Go, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for Property-Based Testing?
Go is the one language here that ships both. `testing/quick` (`quick.Check`, `quick.CheckEqual`, `quick.Config`, the `quick.Generator` interface) is reflection-driven, frozen, and does NOT shrink. Native coverage-guided fuzzing is the modern path: `func FuzzX(f *testing.F)` with `f.Add(seed)` and `f.Fuzz(func(t *testing.T, in []byte))`, run via `go test -fuzz=FuzzX`, with the generated interesting corpus cached outside the repo under `$GOCACHE/fuzz`, and only a failing input -- minimised -- written into `testdata/fuzz/FuzzX/`, where it becomes a seed that plain `go test` replays forever after. For classic properties with real shrinking and stateful machines, `pgregory.net/rapid` (`rapid.Check`, `rapid.SliceOf`, `rapid.String`) is the standard third-party choice.
What is the most common Go mistake when implementing Property-Based Testing?
`f.Fuzz` accepts only a fixed set of argument types -- `[]byte`, `string`, `bool`, `rune`, `byte`, `int`/`uint` and every sized int/uint, `float32`, `float64`. Pass a slice of strings or a struct and it fails at run time, not compile time, so your property must decode its own input from `[]byte`. Worse and quieter: the fuzz target is re-entered thousands of times in the SAME process, so a package-level map, a `sync.Pool`, or a lazily initialised global carries state between executions. The corpus file that reproduces such a failure will pass when replayed alone, and the bug looks like a flaky fuzzer instead of shared state.
How does Property-Based Testing actually perform in Go?
`go test -fuzz` rebuilds the package with coverage instrumentation and runs `$GOMAXPROCS` worker processes, so the target itself is measurably slower than the same code under `go test` and the wall-clock budget is `-fuzztime`, not an example count. The growing corpus is written to the build cache and only minimised entries land in `testdata`. `testing/quick` is the opposite profile: single process, but `quick.Value` walks the type with reflection and `reflect.New`s each field per case, so generating a struct of slices allocates far more than the property body does. Time complexity is O(n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for GitHub Copilot, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.