| name | go-test |
| description | Go testing package. Use for Go testing. |
Go Test
Go has a built-in testing framework in the testing package. It follows Go's effective, minimalist philosophy: no magic, just code.
When to Use
- Go Projects: It is the standard. No 3rd party runner needed.
- Benchmarks: Built-in support (
func BenchmarkXxx(b *testing.B)).
Quick Start
package main
import "testing"
func TestAdd(t *testing.T) {
got := Add(1, 2)
want := 3
if got != want {
t.Errorf("Add(1, 2) = %d; want %d", got, want)
}
}
Run with go test ./....
Core Concepts
Table Driven Tests
The idiomatic way to write Go tests. Define a slice of structs with input/output, then loop range over them.
tests := []struct {
input int
want int
}{
{1, 2},
{2, 4},
}
for _, tc := range tests {
t.Run(, { ... })
}