Go's standard testing package provides the foundation for all testing in Go. It's part of the standard library, requires no external dependencies, and is the basis for all other testing frameworks.
No installation needed - it's part of Go's standard library!
- Unit Testing: Write test functions with
func TestXxx(t *testing.T) - Benchmarking: Measure performance with
func BenchmarkXxx(b *testing.B) - Parallel Execution: Run tests concurrently with
t.Parallel() - Subtests: Organize tests hierarchically with
t.Run() - Table-Driven Tests: Test multiple scenarios efficiently
- Test Coverage: Built-in coverage analysis with
-coverflag
func TestSum(t *testing.T) {
result := Sum(2, 3)
if result != 5 {
t.Errorf("Expected 5, got %d", result)
}
}func TestSum(t *testing.T) {
tests := []struct {
name string
a, b int
expected int
}{
{"positive numbers", 2, 3, 5},
{"negative numbers", -1, -2, -3},
{"zero", 0, 0, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := Sum(tt.a, tt.b)
if result != tt.expected {
t.Errorf("Sum(%d, %d) = %d; want %d",
tt.a, tt.b, result, tt.expected)
}
})
}
}# Run all tests
go test
# Run with verbose output
go test -v
# Run with coverage
go test -cover
# Run specific test
go test -run TestSum
# Run benchmarks
go test -bench=.=== RUN TestSum
=== RUN TestSum/positive_numbers
=== RUN TestSum/negative_numbers
=== RUN TestSum/with_zero
--- PASS: TestSum (0.00s)
--- PASS: TestSum/positive_numbers (0.00s)
--- PASS: TestSum/negative_numbers (0.00s)
--- PASS: TestSum/with_zero (0.00s)
PASS
ok github.com/lirany1/go-testing-framework-examples/01_builtin_testing 0.002s
- ✅ Part of Go standard library (no dependencies)
- ✅ Fast and lightweight
- ✅ Simple and straightforward
- ✅ Excellent IDE support
- ✅ Built-in benchmarking and profiling
- ✅ Native parallel test execution
- ❌ No built-in assertion library (manual
ifchecks) - ❌ Not BDD-style
- ❌ Verbose for complex assertions
- ❌ No built-in mocking (need external tools)
- Use Table-Driven Tests: Test multiple scenarios efficiently
- Name Tests Descriptively: Use
TestFunctionName_Scenariopattern - Test One Thing: Each test should verify a single behavior
- Use Subtests: Organize related tests with
t.Run() - Check Coverage: Aim for meaningful coverage, not 100%