Using table-driven tests to improve coverage
This recipe will demonstrate the process to write a table-driven test, collect test coverage, and improve it. It will also make use of the github.com/cweill/gotests
package to generate tests. If you've been downloading the test code for other chapters, these should look very familiar. Using a combination of this recipe and the previous two, you should be able to achieve 100% test coverage in all cases with some work.
Getting ready
Configure your environment according to these steps:
- Refer to the Getting ready section of the Mocking using the standard library recipe of this chapter.
- Run the
go get github.com/cweill/gotests/
command.
How to do it...
These steps cover writing and running your application:
- From your terminal/console application, create the
chapter8/coverage
directory and navigate to it. - Copy tests from https://github.com/agtorre/go-cookbook/tree/master/chapter8/coverage or use this as an exercise to write some of your own code.
- Create a file...