Testing & Modules

Table-driven tests, subtests and benchmarks with the built-in testing package; modules and packages.

Testing is built into the Go toolchain: files ending in _test.go contain functions TestXxx(t *testing.T) and BenchmarkXxx(b *testing.B), and go test finds and runs them. No framework is needed.

A table-driven test

tests is a table of cases; t.Run gives each case its own named subtest.

t.Errorf reports a failure and continues; t.Fatalf would stop the test.

0xF4 is the published check value of CRC-8/SMBUS for "123456789".

BenchmarkCRC8 runs with go test -bench .

Modules and packages

Writing one test function per input Use a table of cases and t.Run

Table-driven tests make adding a case a one-line change and give each case a readable name in the output.

How do you run only tests whose name contains CRC, with the race detector?

go test -race -run CRC ./...