Test coverage
When writing tests, it is often important to know how much of the actual code is getting exercised (or covered) by the tests. That number is an indication of the penetration of the test logic against the source code. Whether you agree or not, in many software development practices, test coverage is a critical metric as it is a measure of how well the code is tested.
Fortunately, the Go test tool comes with a built-in coverage tool. Running the Go test command with the -cover
flag instruments the original source code with coverage logic. It then runs the generated test binary, providing a summary of the overall coverage profile of the package, as shown in the following:
$> go test -cover PASS coverage: 87.8% of statements ok github.com/vladimirvivien/learning-go/ch12/vector 0.028s
The result shows a well-tested code with a coverage number of 87.8%
. We can use the test tool to extract more details about the section of the code that is tested. To do this, we use the ...