liveBook · Manning

Go test

As explained in Go in Practice, Second Edition

go test is a Go command that compiles and runs tests for Go packages.

Use `go test` to compile and run package tests, then inspect which tests passed or failed. Put tests in files ending `_test.go`; Go excludes those files from normal application builds. Test functions begin with `Test` and receive a `*testing.T`, which can report failures with `t.Errorf`. Run `go test` for the current package or `go test ./...` to include packages in subdirectories. section 1.2.4

Catch a string-reversal bug

A byte-by-byte `reverseName` can appear correct with simple input but fail on a multibyte character. A test compares its result with an expected string and reports a mismatch through `t.Errorf`. Reverse a `[]rune` instead, then rerun `go test` to check the fix. This turns an easy-to-miss manual-testing mistake into a regression test. section 1.2.4

func reverseNameFixed(name string) string {
    reversed := make([]rune, 0)
    runes := []rune(name)
    for i := len(runes) - 1; i >= 0; i-- {
        reversed = append(reversed, runes[i])
    }
    return string(reversed)
}

The corrected function converts the input to runes before reversing it, rather than reversing its bytes. section 1.2.4

To find gaps in testing, run `go test -v -cover`. The reported statement-coverage percentage gives a quick overview, not a requirement to reach 100%. For a browser view, run `go test -v -cover -coverprofile mycover.out`, followed by `go tool cover -html mycover.out`. The saved profile helps locate code that the tests did not exercise. section 6.3.4

The same command runs benchmarks stored in `_test.go` files: use `go test -bench=.` for all matching benchmarks or, for example, `go test -bench=BenchmarkBubbleSort10$` for one. Benchmark functions receive a `*testing.B` and use `b.N` to control repeated work. Use `-benchtime` to change the minimum run time and `-benchmem` to include allocation data. section 6.4

Compare sorting costs

A Bubble Sort benchmark suite tests integer slices of several lengths and reports runs and time per operation. As written, its helper generates a slice once and sorts that same slice repeatedly; keep that setup in mind when interpreting the measurements. section 6.4

How Go in Practice, Second Edition covers it

How deep it goes
Shows test naming and failure reporting, then gives commands for coverage profiles and benchmark selection without detailing runner internals.
Examples
Uses a failing string-reversal test, a coverage run over simple functions, and Bubble Sort benchmarks at several input sizes.
What it assumes
Assumes basic Go functions and packages while introducing `_test.go`, `*testing.T`, and the commands needed to run tests.
What you can do after
Practice/perform package testing, generate a coverage view, and run selected Go benchmarks.
How it connects
Connects unit tests with Unicode strings, statement coverage, subtests, and benchmarking.

Also explained in

Compare how 4 books explain go test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.