test function
A Go function whose name starts with Test and which takes a *testing.T, so go test finds and runs it automatically.
In more detail
Name it after the behaviour it checks, such as TestHealthy or TestParsePortError, so a failure tells you what broke. Inside, compare what you got with what you wanted and report a mismatch through t.
Example
func TestHealthy(t *testing.T) {
if !healthy(204) {
t.Error("success code should be healthy")
}
}Related terms
- Go testing package — Go's standard library package for writing tests, run by the go test command.
- t.Error and t.Fatal — The *testing.T methods that mark a test as failed: the Error forms continue running the test, the Fatal forms stop it immediately.
- go test — The go subcommand that builds a test binary from the _test.go files in a package, runs every test function and reports what failed.
Taught in
- The Standard testing Package — Concurrent Systems Programming with Go
- Writing Table Driven Tests — Concurrent Systems Programming with Go