Home >Backend Development >Golang >What is the function of go test?
"go test" is a test command, which is used to test programs written in Go language; this test is based on code packages. The "go test" command will automatically read the source code file named "*_test.go" under the source code directory that contains several test functions. The test function generally has a name prefix of "Test" and a type of "testing.T" " function declared with parameters.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Go language has a set of unit testing and performance testing systems, which can quickly test a piece of required code with only a small amount of code added.
The go test command will automatically read the file named *_test.go under the source code directory, generate and run the executable file for testing.
go test command (test command)
go test
command is used to test programs written in Go language . This kind of testing is based on code packages. Of course, this also requires the help of test source code files. We will elaborate on how to write and write Go program test code in the second section of this chapter. Here, we only discuss how to use commands to start tests.
go test
The command will automatically test each specified code package. Of course, the premise is that the test source code file exists in the specified code package. The test source code file is a source code file whose name is suffixed with "_test.go" and contains several test functions. The test function is generally a function whose name is prefixed with "Test" and has a parameter declaration of type "testing.T".
Now, let's test several code packages in the goc2p project. The way to specify a code package when using the go test
command is the same as with other commands - use the code package import path. If you need to test multiple code packages, you need to add spaces between their import paths to separate them. An example is as follows:
hc@ubt:~$ go test basic cnet/ctcp pkgtool ok basic 0.012s ok cnet/ctcp 2.014s ok pkgtool 0.014s
go test
The command will print out the test summary information in units of code packages after executing all the test files in the code package. In the above example, the first column of the three lines of information corresponding to the three code packages is "ok". This means they all passed the test. The third column of each row shows the time, in seconds, it took to run the corresponding test. We can also run the go test
command without any parameters in the code package directory. Its function and results are the same as the above example.
In addition, we can also specify the test source code file for testing. In this case, the go test
command will only execute the tests in the specified file, like this:
hc@ubt:~/golang/goc2p/src/pkgtool$ go test envir_test.go # command-line-arguments ./envir_test.go:25: undefined: GetGoroot ./envir_test.go:40: undefined: GetAllGopath ./envir_test.go:81: undefined: GetSrcDirs ./envir_test.go:83: undefined: GetAllGopath ./envir_test.go:90: undefined: GetGoroot FAIL command-line-arguments [build failed]
We see that, just like specifying the source code file to compile or run, the command program will The source code file generates a virtual code package - "command-line-arguments". However, the test did not pass. But the reason is not that the test failed, but that the compilation failed. For the command program that runs this test, the test source code file envir_test.go belongs to the code package "command-line-arguments". Moreover, this test source code file uses the functions in the library source code file envir.go. However, it does not display the code package to which the source code file of this library is imported. This will obviously cause compilation errors. If we want to solve this problem, we also need to add the source code file tested by this test source code file when executing the command. An example is as follows:
hc@ubt:~/golang/goc2p/src/pkgtool$ go test envir_test.go envir.go ok command-line-arguments 0.010s
Now, we intentionally fail a certain test in the code package pkgtool
. Now let's run the test again:
hc@ubt:~$ go test basic cnet/ctcp pkgtool ok basic 0.010s ok cnet/ctcp 2.015s --- FAIL: TestGetSrcDirs (0.00 seconds) envir_test.go:85: Error: The src dir '/usr/local/go/src/pkg' is incorrect. FAIL FAIL pkgtool 0.009s
We know from the summary information in the above example that the test in the test function TestGetSrcDirs
of envir_test.go in the test source code file failed. In the line of information containing the name of the test source file that failed the test, the colon-separated number immediately following the test source file name is the line number where the error message is located, and the colon-separated number after the line number is the error message. The content of this error message is user-written. Additionally, the last line of the summary information is prefixed with "FAIL". This indicates that the tests for the code package pkgtool are not passing. The reasons for failure are described in the previous message.
Under normal circumstances, we will put the test source code file and the source code file being tested in the same code package. Moreover, the package names declared in these source code files are also the same. In addition, we have another option, that is, the package name declared in the test source code file can be the name of the corresponding package plus the "_test" suffix. We call this kind of test source code file an out-of-package test source code file. However, there is a drawback to testing source code files outside the package, that is, the package-level private program entities in the source code file under test cannot be tested in their test functions, such as package-level private variables, functions, and structure types. This is because the code packages that the two belong to are different. Therefore, we generally rarely write out-of-package test source code files.
About tags
The tag processing part of thego test
command is so large and complicated that Go language developers have to separate this part of the logic from the main body of the go test
command program Separate and create separate source code files. Because the go test
command includes a compile action, it can accept all tags available for the go build
command. In addition, it has many unique markings. These tags are used to control the actions of the command itself, some are used to control and set up the test process and environment, and some are used to generate more detailed test results and statistical information.
A few of the more common tags that can be used with the go test
command are -c
, -i
, and -o
. These two are markers used to control the actions of the go test
command itself. See table below for details.
Table 0-6 go test
Mark description of the command
Mark name | Mark description |
---|---|
-c | Generates an executable file for running the test, but does not execute it. This executable file will be named "pkg.test", where "pkg" is the name of the last element of the import path of the code package under test. |
-i | Install/reinstall the dependency packages required to run the tests, but do not compile and run the test code. |
-o | Specifies the name of the executable file used to run the test. Appending this tag will not affect the running of the test code unless the tag -c or -i is also appended. |
The above tags can be used together. The purpose of using them together is to have the go test
command install dependency packages and compile test code, but not run tests. In other words, let the command program run through all the processes before running the test. This can be used to test the testing process. Note that after adding the -c
tag, the command program will store the executable file used to run the test in the current directory.
【Related recommendations: Go video tutorial, Programming teaching】
The above is the detailed content of What is the function of go test?. For more information, please follow other related articles on the PHP Chinese website!