Utilizing Go's testing framework
Testing is one of the most important and least loved parts of any language. Testing provides a developer with the knowledge that something works as expected. I cannot count the times that writing unit tests has proven that a function or method did not work the way I expected. This saved countless hours of debugging.
To this end, tests need to have the following attributes:
- Easy to write
- Fast to execute
- Simple to refactor
- Effortless to understand
To satisfy these needs, Go tackles tests by doing the following:
- Breaking tests into their own files
- Providing a simple
testing
package - Using a testing methodology called table-driven tests (TDTs)
In this section, we will cover how to write basic tests, Go's standard TDT methodology, creating fakes with interfaces, and—finally—some third-party packages that I used and others that are popular, but I don't necessarily recommend.
...