go test function
go test is a command in the Go toolchain that compiles and runs Golang test code written as required and generates test reports.
The file where the test code is located is required to be named *_test.go. The file named in this way will not be compiled by the go build command, but will be compiled and run by go test. There are several types of functions in *_test.go:
- Unit test function: Prefixed with Test as the function name, used to test the logical behavior of the program. The parameter must be t * and there is no return value.
- Benchmark function: Prefixed with Benchmark as the function name, used to test the performance of the program. The parameter must be b * and no return value.
- Fuzz testing function: Prefix with Fuzz as the function name, used to test the robustness of the program. The parameter must be f * and no return value
- Example function: Prefixed with Example as the function name, used to provide sample documentation, without parameters and return values.
go test usage method and usage example
The use of go test is relatively simple. Run the following command directly in the root directory of the project or in a certain package:
go test
go test will compile and run all test codes in the current project or current package and output test results.
A specific test can be run by specifying the path to the package or test file, multiple can be specified, or wildcards can be used to match. For example:
go test [packages] go test
Here are some commonly used parameter descriptions:
- -v: Show detailed test output, including the name and results of each test case.
- -run: Specifies the regular expression of the test function to run.
- -cover: Conduct code coverage analysis at the same time to display the execution of the code.
- -coverprofile: outputs the results of code coverage analysis to the specified file.
- -count: Specifies the number of runs of the test, default is 1.
- -timeout: Sets the run timeout time of the test.
- -bench: Run benchmarks related to performance testing.
- -benchmem: Displays statistics on memory allocation when running benchmarks.
Here we will focus on explaining the -coverprofile parameter. This parameter can output the code coverage results of the test case to the specified file, and then use go tool cover for specific analysis. Let’s take a look at the effect of bringing the -coverprofile parameter. Taking the /luduoxin/json-validator-go project as an example, after clone the code, switch to the validator directory of the project, open the terminal, and execute the following command:
$ go test -coverprofile=
The first two lines of intercepted are as follows:
mode: set /luduoxin/json-validator-go/validator/:11.30,15.2 3
The meanings of each part in the second line are as follows:
"File: Start line. Start column, End row. End column Number of statements in this basic block Number of times the basic block is executed"
You can do many things by figuring out the format of this output content. For example, you can create a file in this format during the process of executing test cases, and then use the go tool cover tool to analyze it.
This is the article about the detailed explanation of Go toolchain code testing tool go test. For more related Go toolchain go test content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!