Testing, debugging and code quality in Go
You test your code with table-driven tests, benchmarks and fuzzing, debug with delve and catch mistakes before they run with static analysis.
Level 201 · 18 lessons · 15 practice labs
Lessons
Writing tests
- Test files and the difference between t.Error and t.Fatal
- Table-driven tests
- Subtests and t.Parallel
- Helpers, the testdata folder and golden files
- Fake dependencies through interfaces
- Coverage: go test -cover and the HTML report
- Short task: surround an existing parser with tests
Measuring and fuzzing
- Benchmarks: testing.B and benchstat
- Example functions: documentation that runs
- Fuzzing: unexpected input with go test -fuzz
- An introduction to pprof: CPU and memory profiles
- Short task: find and fix a hidden crash with fuzzing
Debugging and static analysis
- Step-by-step debugging with delve
- go vet: mistakes the compiler does not see
- Consistent code with staticcheck and gofmt
- Build tags and a one-command test pipeline
- Short task: bring the analysis findings down to zero
Capstone
- Capstone: a log parser with tests and over eighty per cent coverage
Practice labs
- Using the difference between t.Error and t.Fatal: Fix a test that panics on a nil result so it uses t.Fatal for the precondition and t.Error for independent checks.
- A table test for a duration parser: Write a table with named subtests for a duration text parser and expose the hidden bug with bad inputs and boundary values.
- Fixing shared state in parallel subtests: Find the shared package variable and temp directory in tests that fail now and then once t.Parallel is added and give each subtest its own state.
- Showing the right line from a test helper: Add t.Helper to a comparison function that reports the failure line inside the helper and do clean-up with t.Cleanup.
- Testing report output with a golden file: Compare a report generator's output with a golden file under testdata and make it refreshable with an -update flag.
- Replacing clock and mail dependencies with fakes: Separate a reminder service tied to time.Now and real mail sending through interfaces and test boundary times with a fake clock and a recording fake sender.
- Finding an uncovered branch and adding a test: Find the error branches left red in the coverage report and bring the package above ninety per cent coverage.
- Comparing two implementations with benchstat: Write benchmarks reporting allocations too for two string-joining implementations and show with benchstat that the difference is significant.
- Finding and speeding up the hot path from a profile: Take CPU and memory profiles from a benchmark, find and remove the line that allocates most and show at least a twofold improvement in the benchmark.
- Writing a runnable example function: Write example functions with Output comments for two exported functions of a package and show go test verifies them.
- Fuzzing a codec with a round-trip property: Write a fuzz test checking the round-trip property of an encode and decode pair, add the input it finds as a seed and fix the bug.
- Finding and closing a hidden crash with fuzzing: Find with fuzzing the index panic that appears only on special input in a settings line parser, turn the minimised input into a regression test and close it.
- Resolving go vet findings: Fix go vet warnings such as a wrong format verb and a copied lock and bring the gofmt diff to zero.
- Tracing a wrong total with delve: Set a breakpoint with dlv in a program that computes an invoice total wrongly, watch the variables and find and fix the shadowed variable.
- A one-command test pipeline with build tags: Separate integration tests with a build tag, write one script running format, vet, staticcheck, race and a coverage threshold in order and bring findings to zero.
About this course
You test your code with table-driven tests, benchmarks and fuzzing, debug with delve and catch mistakes before they run with static analysis.
What you will learn
- Writing tests
- Measuring and fuzzing
- Debugging and static analysis
- Capstone
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.