existGo languageThe testing library and the go test command for implementing unit tests are provided in the
The table of contents for the test explanations in this article is as follows:
This article uses a summation function for unit testing, the class code containing the test function is as follows:
-
package main
-
-
func sumOfTwo(a int,b int) int {
-
return a+b
-
}
Because the simple go test command does not guarantee that the test functions are executed in a certain order, the following lists the execution of a single test function, the sequential execution of multiple test functions, the execution of test functions after initializing the test environment, and performance testing methods
1. Definition and execution of a single test function
The test class code for this function is as follows (note: the test class needs to be imported into the same package):
-
package main
-
-
import (
-
"fmt"
-
"testing"
-
)
-
//The test function name must be TestXxx and the test function function must be passed into the*
-
func TestPrint(t *) {
-
res := sumOfTwo(1,2)
-
("hey")
-
if res != 3 {
-
("wrong result of sumOfTwo") //Print the error message and terminate the case
-
}
-
}
Open the command line in the current directory and use the input command :go test, the test results are as follows:
2. Sequential execution of multiple test functions
-
package main
-
-
import (
-
"fmt"
-
"testing"
-
)
-
-
func testPrint(t *) {// Note that the name should not be named to define the name of the test class, i.e. TestXxx, the first letter of this function name is changed to lower case
-
res := sumOfTwo(1, 2)
-
("hey")
-
if res != 3 {
-
("wrong result of sumOfTwo") //print the error message and terminate the case
-
}
-
}
-
-
func testPrint1(t *) {// Note that the name should not be named to define the name of the test class, i.e. TestXxx, the first letter of this function name is changed to lower case
-
// () skips the current test and follows the pass
-
res := sumOfTwo(2,3)
-
("hey")
-
if res != 5 {
-
("wrong result of sumOfTwo") //print the error message and terminate the case
-
}
-
}
-
func TestPrintAll(t *) {The test classes that the //go test command executes
-
("TestPrint", testPrint)// Define test names, pass in test classes
-
("TestPrint", testPrint1)
-
}
Execute the go test command with the following result:
3. Initialize the test environment
Define TestMain function, in the implementation of the test class if the existence of the function will give priority to the implementation of this function can be completed in this function initialization, such as database links and other functions. Write a test class as follows (Note: TestMain if not executed (), other test functions will not be carried out):
-
package main
-
-
import (
-
"fmt"
-
"testing"
-
)
-
-
func testPrint(t *) {// Note that the name should not be named to define the name of the test class, i.e. TestXxx, the first letter of this function name is changed to lower case
-
// () skips the current test and follows the pass
-
res := sumOfTwo(1, 2)
-
("hey")
-
if res != 3 {
-
("wrong result of Print1to20") //print the error message and terminate the case
-
}
-
}
-
-
func testPrint1(t *) {// Note that the name should not be named to define the name of the test class, i.e. TestXxx, the first letter of this function name is changed to lower case
-
res := sumOfTwo(2,3)
-
res++
-
("hey")
-
if res != 5 {
-
("wrong result of Print1to20") //print the error message and terminate the case
-
}
-
}
-
-
func TestPrintAll(t *) {
-
("TestPrint", testPrint)
-
("TestPrint", testPrint1)
-
}
-
func TestMain(m *) {//TestMain passes in M
-
("test run...")
-
() //Guarantee that the other testcase is executed, before () to do some initialization stuff, such as database connection. If you don't execute this function, the other testcase won't be executed.
-
}
The test function runs with the following results:
4.benchmarkConducting performance tests
Benchmar functions generally begin with Benchmark
Benchmark's cases are generally run multiple times, and this is true for each execution, with the number of times adjusted during execution to reach steady state based on whether the average execution time of the actual case is stable or not, and because of this, the test will always be run if the execution time of the test function doesn't end up converging to steady state, as will be demonstrated below.
Test Functions:
-
package main
-
-
import (
-
"fmt"
-
"testing"
-
)
-
func BenchmarkAll(b *) {
-
for i := 0; i < ; i++ {
-
sumOfTwo(1, 2)
-
}
-
}
Execute tests (with -bench=. is to execute only benchmark tests, note that if you write the Testmain() function, it will execute TestMain() first):
The function was executed 200,000,000 times, with an average execution time of 0.25ns per execution
*************************************************************************************************************************************
The execution time of the test function is ultimately not a steady-state use case:
-
package main
-
-
import (
-
"fmt"
-
"testing"
-
)
-
-
func a(n int) {
-
for n > 0 {
-
n--
-
}
-
}
-
func BenchmarkAll(b *) {
-
for i := 0; i < ; i++ {
-
a(i)
-
}
-
}
Implementation results:
It keeps getting stuck running here.