Working with the testing package
The standard library provides the testing
package, which contains the essentials we need for writing and running tests. In this section, we will explore how to use it and begin to apply it so that we can write tests for our simple terminal calculator example.
The testing package
The testing
package provides support for testing Go code. It must be imported by all test code as this is the way to interact with the test runner. At a glance, the testing
package seems very simplistic, but it fits with Go’s language design. Packages should be small, focused, and have a limited number of dependencies. This should make them easy to test with a relatively simple testing library.
Here are some of the important types from the testing
library that we will be using:
testing.T
: All tests must use this type to interact with the test runner. It contains a method for declaring failing tests, skipping tests, and running tests in parallel. We will...