web123456

How to do unit testing in Go (testing functional tests and benchmark performance tests)

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:

  1. package main
  2. func sumOfTwo(a int,b int) int {
  3. return a+b
  4. }

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):

  1. package main
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. //The test function name must be TestXxx and the test function function must be passed into the*
  7. func TestPrint(t *) {
  8. res := sumOfTwo(1,2)
  9. ("hey")
  10. if res != 3 {
  11. ("wrong result of sumOfTwo") //Print the error message and terminate the case
  12. }
  13. }

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

  1. package main
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. 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
  7. res := sumOfTwo(1, 2)
  8. ("hey")
  9. if res != 3 {
  10. ("wrong result of sumOfTwo") //print the error message and terminate the case
  11. }
  12. }
  13. 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
  14. // () skips the current test and follows the pass
  15. res := sumOfTwo(2,3)
  16. ("hey")
  17. if res != 5 {
  18. ("wrong result of sumOfTwo") //print the error message and terminate the case
  19. }
  20. }
  21. func TestPrintAll(t *) {The test classes that the //go test command executes
  22. ("TestPrint", testPrint)// Define test names, pass in test classes
  23. ("TestPrint", testPrint1)
  24. }

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):

  1. package main
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. 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
  7. // () skips the current test and follows the pass
  8. res := sumOfTwo(1, 2)
  9. ("hey")
  10. if res != 3 {
  11. ("wrong result of Print1to20") //print the error message and terminate the case
  12. }
  13. }
  14. 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
  15. res := sumOfTwo(2,3)
  16. res++
  17. ("hey")
  18. if res != 5 {
  19. ("wrong result of Print1to20") //print the error message and terminate the case
  20. }
  21. }
  22. func TestPrintAll(t *) {
  23. ("TestPrint", testPrint)
  24. ("TestPrint", testPrint1)
  25. }
  26. func TestMain(m *) {//TestMain passes in M
  27. ("test run...")
  28. () //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.
  29. }

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:

  1. package main
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func BenchmarkAll(b *) {
  7. for i := 0; i < ; i++ {
  8. sumOfTwo(1, 2)
  9. }
  10. }

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:

  1. package main
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func a(n int) {
  7. for n > 0 {
  8. n--
  9. }
  10. }
  11. func BenchmarkAll(b *) {
  12. for i := 0; i < ; i++ {
  13. a(i)
  14. }
  15. }

Implementation results:

It keeps getting stuck running here.