Introducing the Built-in Go Testing Support
In this lesson, we'll discuss the testing facilities that come with Go.
We'll cover the following...
It turns out that Go, as a pragmatic language, provides rich testing support that includes ways to define and run tests, benchmark the run time and memory usage of your code, provide usage examples, and even test coverage. The topics we will cover include the testing package, the go test command, and writing, and running tests.
The testing package
Go testing starts (and often ends) with the testing package. This package defines types and functions that facilitate writing automated tests for Go packages. The go test
command fully supports the testing package and is designed to run your tests and capture the results. Later, we will cover later some other test frameworks, but they are all based on top of the testing package. Here are some of the built-in capabilities you get right away:
- Self-discovery of tests and benchmarks
- Nested tests and table-driven tests
- Skipping tests
- Verified example code
- Package-level setup and teardown
Alright, let’s jump right in and see how to go about writing tests.
Writing tests
If you’re familiar with xUnit-style tests, then get ready for something fresh. Go ...