Detailed explanation of Go tool chain (2): code coverage analysis artifact go tool cover

go tool cover function

go tool cover is a command in the Go toolchain, which is used to analyze the code coverage of test cases. By analyzing the test coverage files, it helps developers and testers understand which code is executed and how many times it is executed in the test.

Through code coverage analysis, you can know how many code lines, branches and statements are covered after the test case is executed, and whether there is any code that is not covered by the test case, which helps to find potential problems and errors in the code and improve the quality and reliability of the code.

How to use go tool cover and examples

First, you need to provide the code coverage file generated by using the go test command (you can also provide a file generated by other methods in the same format as the file generated by go test). For example, take the github.com/luduoxin/json-validator-go project as an example. After cloning the code, switch to the validator directory of the project, open the terminal, and execute the following command:

 $ go test -coverprofile=c.out

Intercept the first two lines of c.out as follows

mode: set
github.com/luduoxin/json-validator-go/validator/scanner.go:11.30,15.2 3 

The meaning of each part of the second line is as follows:

"File: start line. start column, end line. end column the number of statements in the basic block the number of times the basic block is executed"

Next, let's look at the method of using go tool cover to analyze the file c.out generated above, and use the following command to view the coverage rate of each method and the total coverage rate

$ go tool cover -func=c.out

The default is to output the result to standard output, and you can use -o to output the result to other places, such as outputting to a file:

$ go tool cover -func=c.out -o coverage.out

Use the following method to generate an html page and automatically open the browser to display the details of the coverage:

$ go tool cover -html=c.out

In this way, you can check the code coverage very intuitively. The code that has been covered is marked in green (the darker the green, the more times it has been covered), the code that is not covered is marked in red, and the code that does not need to be detected is marked in gray. Move the mouse over the code block, and the corresponding number of times covered will pop up. It is also possible not to automatically open the browser display, but to output the html content to other places:

$ go tool cover -html=c.out -o coverage.html

To generate a coverage file, you first need to insert the source code. Use go tool cover to generate the inserted code, for example:

go tool cover -mode=set -var=CoverageVariableName scanner.go

After execution, the instrumented source code will be output to standard output.

go tool cover usage scenarios

Can be used in the following scenarios:

  1. Developers can use it to analyze the coverage rate of unit test cases, and optimize the unit test cases by analyzing the coverage rate of unit test cases.
  2. Testers can use it to analyze the coverage rate of test cases. Through certain means, with the continuous execution of test cases, the coverage files are continuously generated, and then use go tool cover to analyze the coverage rate to find possible test blind spots and codes that are not covered by test cases.

Guess you like

Origin blog.csdn.net/luduoyuan/article/details/131797896