liveBook · Manning

Chapter 8: Testing and tooling

What you will learn in chapter 8 of Go in Action, Second Edition

This chapter introduction presents Go testing as more than a way to check finished code: tests can guide development, reveal performance problems, and find bugs in unexpected inputs.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduction presents Go testing as more than a way to check finished code: tests can guide development, reveal performance problems, and find bugs in unexpected inputs.

Key terms: testing, test-driven development, contracts, benchmarking, profiling, CPU usage, memory allocation, fuzzer, invariants, randomly generated corpus of data

8.1 Test files and functions

This section explains how Go recognizes test files and functions, and what to check when tests do not run.

Key terms: *_test.go, normal build process, go test, func TestXxx(t *testing.T), testing.T, no tests to run, go help testfunc

8.2 Running tests with go test

This section shows how go test runs a package’s tests, how to interpret its default results, and how verbose mode reveals individual test activity.

Key terms: go test, _test.go, test executable, test flags, PASS, ok, panic, timeout, -v, verbose mode

8.3 Test functions

This section explains what Go test functions look like, how to structure and report a test, and how to choose between testing package internals and testing as a consumer.

Key terms: Test, *testing.T, t parameter, arrange, act, assert, expectation, result, unit test, isolated, external dependencies, _test

8.4 Test life cycle

This section explains how Go tests report failures through testing.T, how fatal and non-fatal failure methods behave, and when to use each.

Key terms: testing.T, t, go test, Fail, Error, Errorf, FailNow, Fatal, Fatalf, fatal methods

8.5 Test-driven design: A calculator with a twist

This section introduces test-driven development through a calculator example, defining its behavior and setting up a compilable skeleton before writing tests.

Key terms: test-driven development, TDD, API, Calculator, Calculate, Store, Recall, add, sub, basicmath

8.5.1 Black-box testing

This section sets up a Calculator test file and explains why tests written outside the target package are usually preferable to tests with access to package internals.

Key terms: *_test.go, calculator_test.go, basicmath, black box testing, open box testing, white box testing, clear box testing, package <package name>_test, unexported code, TDD

8.5.2 Writing tests for error conditions

This section shows how to test a calculator’s named error conditions before its implementation is complete.

Key terms: exported error types, sentinel errors, ErrDivByZero, ErrUnknownOperation, errors.Is, t.Log, t.Logf, go test, test-driven workflow

8.5.3 Writing good test logs

This section shows how to write brief, diagnostic Go test failure messages and avoid failures that provide no useful log output.

Key terms: go test, want, got, t.Log, t.Logf, t.Fail, t.FailNow

8.5.4 Choosing testing targets

This section shows how to choose a feature-level test target for calculator memory and run that test by itself.

Key terms: Store, Recall, TestMemory, calculator memory feature, initial value, 0, Calculate, t.Logf, fatal, TDD workflow

8.5.5 Table-driven testing

The section shows how to test many Calculate inputs without repeating the same validation code by using table-driven tests.

Key terms: table-driven testing, testCase struct, slice, Calculate method, test cases, testCase

8.5.6 Implementation and initial testing

The section implements a calculator from its tests, then runs go test to check the initial implementation.

Key terms: Calculator, memory value, last calculation, Store, Calculate, Recall, type switch, go test, test-driven development

8.5.7 Code coverage

This section shows how to measure and inspect Go test coverage, then use uncovered code to find missing tests and a calculator bug.

Key terms: code coverage, statements, go test -cover, go test -coverprofile, coverage profile, go tool cover, Store, Calculator, TestMemory, Calculate

8.6 Simulating dependencies with an HTTP server

This section shows how to test HTTP handlers directly with httptest recorders and through a temporary server that routes requests.

Key terms: URL, route, GET, POST, body, header, httptest, recorder, GET handler, hello

8.7 Beyond unit tests

The section introduces benchmark and fuzz testing as ways to check performance and uncover missed cases beyond ordinary unit tests.

Key terms: unit tests, benchmark testing, fuzz testing, Go’s built-in testing library

8.7.1 Benchmark testing

This section shows how to use Go benchmarks to compare two string-generation methods, read the results, and recognize what benchmarking cannot reveal.

Key terms: benchmark testing, average processing time, parameters, testing.B, testing.T, go test -bench=., iterations, nanoseconds per operation, baseline, strings.Builder

8.7.2 Fuzz testing

This section introduces Go fuzz testing by using generated cases to find and fix a string-handling bug that ordinary tests might miss.

Key terms: fuzz testing, corpus, FuzzContains, testing.F, Contains, rune, byte array, -fuzztime

8.8 Summary

The chapter summary recaps Go’s test files and runner, test-driven development, ways to improve test suites, and benchmark and fuzz testing.

Where this is used later

Concepts explained in this chapter

← Chapter 7: Errors in Go Chapter 9: Concurrency →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.