Module 9 · Lesson 1 of 541/50 in the course~12 min
Module lessons (1/5)
The package `testing`
Lesson preview5 hands-on exercises readyCourse Go · Module 9 — Testing, modules and tooling
Files *_test.go, functions TestXxx(t *testing.T), t.Error vs t.Fatal, go test ./....
What you'll learn in this lesson
- Write unit tests according to the official conventions (
*_test.go,func TestXxx(t *testing.T)). - Choose wisely between
t.Error/t.Errorf(continue) andt.Fatal/t.Fatalf(stop immediately). - Log reliable teardown operations via
t.Cleanup. - Improve stack trace errors by marking shared utilities with
t.Helper().
Overview
In Go, tests are part of the standard library: no external frameworks or assertion DSLs are needed. All you need is a *_test.go file next to the source code, the testing package and the go test command.
Go automatically recognizes tests by following three simple rules:
Sections covered in this lesson
- 1File and signature conventions
- 2
t.Errorvst.Fatal - 3Assertion helper and
t.Cleanup - 4Guided practice
- 5Check for understanding
Pro Lesson · Module 9 — Testing, modules and tooling
Unlock "The package `testing`" and the full Course Go curriculum
Files `*_test.go`, functions `TestXxx(t *testing.T)`, `t.Error` vs `t.Fatal`, `go test ./...`.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Go plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.