How it works...

The go test -cover command comes with a basic Go installation. It can be used to collect a coverage report of your Go application. In addition, it has the ability to output coverage metrics and an HTML coverage report. This tool is often wrapped by other tools, which will be covered in the next recipe. These table-driven test styles are covered at https://github.com/golang/go/wiki/TableDrivenTests and are an excellent way to make clean tests that can handle many cases without writing a bunch of extra code.

This recipe starts by automatically generating test code, then filling in test cases as needed to help create more coverage. The only time this is especially tricky is when you have non-variable functions or methods being invoked. For example, it can be tricky to make gob.Encode() return an error to increase test coverage. It can also seem quirky to use the method described in the Mocking using the standard library recipe of this chapter and use var gobEncode = gob.Encode to allow patching. For this reason, it can be difficult to advocate for 100% test coverage and instead argue for focusing on testing the external interface extensively—that is, testing many variations of input and output—and in some cases, as we'll see in the Behavior testing using Go recipe of this chapter, fuzzing can become useful.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.36.249