liveBook · Manning

Testing package

As explained in Go in Practice, Second Edition

The Go testing package is a standard-library package for defining and running automated tests and reporting test failures.

The Go testing package supports automated tests, failure reporting, command-line execution, code coverage, and race-condition detection. Test files end in _test.go, so Go runs them during testing but excludes them from application builds. section 1.2.4

A practical testing workflow:

  • Define a function whose name begins with Test and accept a *testing.T value for reporting failures. section 1.2.4
  • Run go test for the current package or go test ./... for the package tree. section 1.2.4

Detecting a Unicode reversal bug

A test reverses names and compares actual and expected values with strings.EqualFold. The failure exposes byte-wise reversal of a multibyte character; converting the string to []rune fixes the implementation, after which go test reports PASS.

How Go in Practice, Second Edition covers it

How deep it goes
Demonstrates test-file naming, Test functions, failure output, package commands, and coverage, but not advanced test organization.
Examples
Uses a reverse-name function, TestName, Unicode input, failure diagnostics, rune conversion, and coverage output.
What it assumes
Assumes basic Go functions and strings while demonstrating testing conventions and commands directly.
What you can do after
Diagnose/debug implementation defects with Test functions, go test output, assertions, and coverage reports.
How it connects
Connects testing with go test, code coverage, race-condition detection, Unicode handling, and test-driven development.

Also explained in

Compare how 2 books explain testing package →

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.