Testing with go test

Contents
Table-driven tests
go test -v
Coverage
Benchmarks

Table-driven tests

Go tests live in files ending with _test.go next to the code they check. A table test lists inputs and expected outputs in a slice, then loops over the rows, so adding a case means adding one line.

First, the code under test in main.go:

package main import "fmt" func Add(a, b int) int { return a + b } func main() { fmt.Println(Add(2, 3)) }

Next, the table test in main_test.go, in the same package:

package main import "testing" func TestAdd(t *testing.T) { tests := []struct { a int b int want int }{ {1, 2, 3}, {0, 0, 0}, {-1, 1, 0}, } for _, tc := range tests { if got := Add(tc.a, tc.b); got != tc.want { t.Errorf("Add(%d, %d) = %d, want %d", tc.a, tc.b, got, tc.want) } } }

Each row names its inputs and the expected answer, and Errorf reports the exact mismatch. Name every test with the Test prefix so the tool discovers it automatically.

go test -v

Run the suite with go test, and add -v to watch each test by name. A passing run prints PASS lines, while failures show the file and line of every broken expectation.

go test -v

=== RUN TestAdd
--- PASS: TestAdd (0.00s)
PASS
ok hello 0.004s

The tool runs every Test function in the package and summarizes the result on the last line. Keep the suite green at all times: a failing test you ignore teaches the team to ignore all of them.

Coverage

Coverage counts which lines your tests execute. Ask for a percentage while you work, and open the HTML report when you want the exact lines painted as covered or missed.

go test -cover

ok hello 0.004s coverage: 100.0% of statements

Every statement of Add runs under TestAdd, so the report shows full marks. Treat 100 percent as a hint, not a proof: it measures visited lines, not the quality of your expectations.

Benchmarks

Benchmarks measure speed the same way tests check truth: name the function with a Benchmark prefix and loop to b.N. The tool scales the loop and reports nanoseconds per operation, so compare numbers before and after each optimization.

package main import "testing" func BenchmarkAdd(b *testing.B) { for i := 0; i < b.N; i++ { Add(2, 3) } }

go test -bench=. -benchtime=100x

BenchmarkAdd-8 1000000 1050 ns/op

The Add from main.go is reused here without changes. Benchmarks share the package with tests, so fast checks and slow measurements live side by side.

Next: Advanced errors

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: