CheckOps
Go testing package
Go's built-in testing package and go test command provide unit tests, table-driven subtests, benchmarks, fuzzing, examples and coverage with no third-party dependencies.
- Unit Testing
- Golang
- Open source
- Free
- Test Runner
- Command Line
- Code Coverage
- Unit testing
- Integration testing
Latest public signals
Not refreshed yet. Refresh reads the public site and, when the entry has a GitHub repository, the latest release.
Introduction
Go ships testing support in its standard library. The testing package, used with the go test command, runs any function of the form func TestXxx(t *testing.T) in files ending in _test.go. Because it is part of the toolchain, every Go project has the same test conventions, and no framework choice is required to get started.
The package covers far more than basic tests. t.Run creates subtests, which makes table-driven tests the idiomatic Go style; t.Parallel() runs tests concurrently; t.Cleanup, t.TempDir and t.Setenv simplify fixtures; benchmarks (func BenchmarkXxx(b *testing.B)) measure performance; native fuzzing (func FuzzXxx(f *testing.F), added in Go 1.18) generates inputs and stores failing cases in testdata/fuzz; and ExampleXxx functions double as runnable documentation. go test -cover and -coverprofile produce coverage, -race enables the race detector, and -run/-bench select tests by pattern. Go is BSD-3-Clause licensed.
Test helpers call t.Helper() so failures point at the calling line, and testing/fstest, net/http/httptest and testing/iotest in the standard library provide ready-made fakes for file systems, HTTP servers and readers.
Features
- Built into Go: No dependencies;
go testeverywhere. - Subtests and parallelism: Table-driven tests with
t.Runandt.Parallel. - Benchmarks and fuzzing: Performance and native fuzz testing.
- Coverage and race detection:
-cover,-coverprofileand-race.
Pros
- Free: Included with Go.
- Consistent: The same conventions in every Go project.
Cons
- Minimal assertions: Checks are plain
ifstatements; many teams add testify or go-cmp. - No built-in mocking: Use interfaces with hand-written fakes or gomock.
Usage and Links (Example)
// price_test.go
package price
import "testing"
func TestApply(t *testing.T) {
tests := []struct {
name string
price, pct, want int
}{
{"no discount", 100, 0, 100},
{"ten percent", 100, 10, 90},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := Apply(tc.price, tc.pct); got != tc.want {
t.Errorf("Apply(%d, %d) = %d, want %d", tc.price, tc.pct, got, tc.want)
}
})
}
}
func FuzzApply(f *testing.F) {
f.Add(100, 10)
f.Fuzz(func(t *testing.T, price, pct int) { _ = Apply(price, pct) })
}
go test ./... -race -cover
go test -fuzz=FuzzApply -fuzztime=30s
Resources:
Pricing
Free as part of the Go toolchain (BSD-3-Clause licence).
Recommended for?
- Every Go project, as the foundation of its test suite.
- Teams wanting benchmarks and fuzzing without extra tools.
Conclusion
Go’s testing package is a complete, standard foundation for unit tests, benchmarks and fuzzing, often extended with small helper libraries.
Alternatives
Other tools on CheckOps that cover similar ground. Open a page, or compare one with Go testing package side by side.
TTestifyTestify is an MIT-licensed Go toolkit with assert and require assertion packages, mock objects and test suites, the most widely used testing library in the Go ecosystem.Compare with Go testing package
GGinkgoGinkgo is an MIT-licensed BDD-style testing framework for Go with Describe/Context/It specs, parallel execution, focused and labelled specs and rich reporting, usually paired with the Gomega matcher library.Compare with Go testing package
GGoMock (uber-go/mock)GoMock is an Apache-2.0 mocking framework for Go, maintained by Uber after the original golang/mock was archived, with the mockgen code generator and a gomock controller for expectations.Compare with Go testing package
RRapidRapid is an MPL-2.0 property-based testing library for Go, inspired by Hypothesis, with automatic shrinking, state machine testing and integration with go test.Compare with Go testing package
GGomegaGomega is an MIT-licensed matcher and assertion library for Go, designed for Ginkgo but usable with plain go test, with asynchronous Eventually/Consistently assertions.Compare with Go testing package
TTestcontainersTestcontainers starts throwaway Docker containers from tests so integration tests can use real databases, brokers, and browsers.Compare with Go testing package