Go programming example [test]

read catalog

Unit testing is an important part of writing good Go programs.

The testing package provides the tools needed to write unit tests. After writing the unit tests, we can run the tests through the go test command.

For the convenience of demonstration, the code of the example is located in the main package. In fact, the code of the unit test can be located in any package.

Test code is usually located in the same package as the code to be tested.

// main_test.go
package main

import (
	"fmt"
	"testing"
)

// 我们要测试下面这个简单的函数——返回最小值。
// 一般地,需要被测试的代码应该在类似于 `intutils.go` 的文件下,
// 其对应的测试文件应该被命名为 `intutils_test.go`。
func IntMin(a, b int) int {
    
    
	if a < b {
    
    
		return a
	} else {
    
    
		return b
	}
}

// 通常编写一个名称以 `Test` 开头的函数来创建测试。
func TestIntMinBasic(t *testing.T) {
    
    
	ans := IntMin(2, -2)
	if ans != -2 {
    
    
		// `t.Error*` 会报告测试失败的信息,然后继续运行测试。
		// `t.Fail*` 会报告测试失败的信息,然后立即终止测试。
		t.Errorf("IntMin(2, -2) = %d; want -2", ans)
	}
}

// 单元测试可以重复,所以会经常使用 *表驱动* 风格编写单元测试,
// 表中列出了输入数据,预期输出,使用循环,遍历并执行测试逻辑。
func TestIntMinTableDriven(t *testing.T) {
    
    
	var tests = []struct {
    
    
		a, b int
		want int
	}{
    
    
		{
    
    0, 1, 0},
		{
    
    1, 0, 0},
		{
    
    2, -2, -2},
		{
    
    0, -1, -1},
		{
    
    -1, 0, -1},
	}

	for _, tt := range tests {
    
    
		// t.Run 可以运行一个 "subtests" 子测试,一个子测试对应表中一行数据。
		// 运行 `go test -v` 时,他们会分开显示。
		testname := fmt.Sprintf("%d,%d", tt.a, tt.b)
		t.Run(testname, func(t *testing.T) {
    
    
			ans := IntMin(tt.a, tt.b)
			if ans != tt.want {
    
    
				t.Errorf("got %d, want %d", ans, tt.want)
			}
		})
	}
}

Use the -v parameter to view details:

[root@bogon test]# go test -v
=== RUN   TestIntMinBasic
--- PASS: TestIntMinBasic (0.00s)
=== RUN   TestIntMinTableDriven
=== RUN   TestIntMinTableDriven/0,1
=== RUN   TestIntMinTableDriven/1,0
=== RUN   TestIntMinTableDriven/2,-2
=== RUN   TestIntMinTableDriven/0,-1
=== RUN   TestIntMinTableDriven/-1,0
--- PASS: TestIntMinTableDriven (0.00s)
    --- PASS: TestIntMinTableDriven/0,1 (0.00s)
    --- PASS: TestIntMinTableDriven/1,0 (0.00s)
    --- PASS: TestIntMinTableDriven/2,-2 (0.00s)
    --- PASS: TestIntMinTableDriven/0,-1 (0.00s)
    --- PASS: TestIntMinTableDriven/-1,0 (0.00s)
PASS
ok      test    0.002s
[root@bogon test]# 

Guess you like

Origin blog.csdn.net/weiguang102/article/details/129876164