[GO language foundation] GO unit test, command line parameters (13)

unit test

traditional method

  • Analysis of the shortcomings of traditional methods
  1. Inconvenient, we need to call it in the main function, so we need to modify the main function. If the project is currently running, it is possible to stop the project.
  2. It is not conducive to management, because when we test multiple functions or multiple modules, they all need to be written in the main function, which is not conducive to our management and clear our thinking
  3. Lead to unit testing. -> testing Testing framework can solve the problem very well.

basic introduction

Go language comes with a lightweight testing framework testing and comes with the go test command to implement unit testing and performance testing. The testing framework is similar to testing frameworks in other languages, and tests for corresponding functions can be written based on this framework. Use cases, you can also write corresponding stress test cases based on this framework.

add.go:
    func addUpper(n int)  int {
        res := 0
        for i := 1; i <= n - 1; i++ {
            res += i
        }
        return res
    }

add_test.go
    func TestAddUpper(t *testing.T) {

        //调用
        res := addUpper(10)
        if res != 55 {
            //fmt.Printf("AddUpper(10) 执行错误,期望值=%v 实际值=%v\n", 55, res)
            t.Fatalf("AddUpper(10) 执行错误,期望值=%v 实际值=%v\n", 55, res)
        }
        //如果正确,输出日志
        t.Logf("AddUpper(10) 执行正确...")
    }
  • Features
  1. Ensure that each function is runnable and the result of the operation is correct
  2. Ensure that the performance of the code written is good,
  3. Unit testing can find logic errors in program design or implementation in time, exposing the problem early, and facilitating problem location and solution. The focus of performance testing is to find some problems in program design, so that the program can still work under high concurrency. keep it steady
  • to sum up
  1. The test case file name must end with _test.go. For example cal_test.go.
  2. The test case function must start with Test. Generally speaking, it is Test+the name of the function being tested, such as TestAddUpper
  3. The parameter type of TestAddUpper(t *tesing.T) must be *testing.T
  4. In a test case file, there can be multiple test case functions, such as TestAddUpper, TestSub
  5. Run test case instructions
    (1) cmd>go test      [如果运行正确,无日志,错误时,会输出日志]
    (2) cmd>go test -v   [运行正确或是错误,都输出日志]
  1. When an error occurs, you can use t.Fatalf to format and output the error message and exit the program
  2. The t.Logf method can output the corresponding log
  3. The test case function is not placed in the main function, but also executed. This is the convenience of the test case
  4. PASS means that the test case runs successfully, FAIL means that the test case failed to run
  5. To test a single file, be sure to bring the original file to be tested
go test -v cal_test.go cal.go
  1. Test a single method
go test -v -test.run TestAddUpper

Parse record command line

  • os.Args
    os.Args is a slice of string, used to store all command line parameters
    func main() {

        fmt.Println("命令行的参数有", len(os.Args))
        //遍历os.Args切片,就可以得到所有的命令行输入参数值
        for i, v := range os.Args {
            fmt.Printf("args[%v]=%v\n", i, v)
        }
    }
  • Flag package The
    flag package can easily parse the command line parameters, and the order of the parameters can be arbitrary
    package main
    import (
        "fmt"
        "flag"
    )

    func main() {
        //定义几个变量,用于接收命令行的参数值
        var user string
        var pwd string
        var host string
        var port int

        //&user 就是接收用户命令行中输入的 -u 后面的参数值
        //"u" ,就是 -u 指定参数
        //"" , 默认值
        //"用户名,默认为空" 说明
        flag.StringVar(&user, "u", "", "用户名,默认为空")
        flag.StringVar(&pwd, "pwd", "", "密码,默认为空")
        flag.StringVar(&host, "h", "localhost", "主机名,默认为localhost")
        flag.IntVar(&port, "port", 3306, "端口号,默认为3306")
        //这里有一个非常重要的操作,转换, 必须调用该方法
        flag.Parse()

        //输出结果
        fmt.Printf("user=%v pwd=%v host=%v port=%v", 
            user, pwd, host, port)

    }

Guess you like

Origin blog.csdn.net/weixin_54707168/article/details/114006066