golang testing的基本使用

韦高阳
2023-12-01

测试功能

如果你实现一个方法 or 函数,就应该使用test程序验证功能是否正确。比如这样:

add.go

func Add(a int, b int) int {
	return a+b
}

func AddThree(a int, b int, c int) int {
	return a+b+c
}

有了简单的函数就可以编写测试用例
测试文件要以add_test.go形式来命名, 测试方法以Test开头

func TestAdd(t *testing.T) {
	got := Add(1+2)
	if got != 3 {
		t.Errorf("Add(1+2) = %d\n", got)
	}
}

func TestAddThree(t *testing.T) {
	got := AddThree(1+2+3)
	if got != 6 {
		t.Errorf("AddThree(1+2+3) = %d\n", got)
	}
}

最后就是执行测试了, test 命令后面要跟所有测试相关的文件,如果仅仅是测试文件会报错,如果仅仅是当前目录下的测试文件也可以用点号代替,比如 go test -v .

go test -v add_test.go add.go 
=== RUN   TestAdd
--- PASS: TestAdd (0.00s)
=== RUN   TestAddThree
--- PASS: TestAddThree (0.00s)
PASS
ok      go.programming/testing/add      (cached)

指定测试用例, -run 后面的参数是正则表达式

go test -v -run TestAddT add_test.go add.go 
=== RUN   TestAddThree
--- PASS: TestAddThree (0.00s)
PASS
ok      go.programming/testing/add      0.250s

基准测试

基准测试需要单独编写函数

func Benchmark_Add(b *testing.B) {
	var n int
	for i:=0; i<b.N; i++ {
		add(n+i)
	}
}

运行基准测试

go test -v -bench=. add_test.go add.go  
=== RUN   TestAdd
--- PASS: TestAdd (0.00s)
=== RUN   TestAddThree
--- PASS: TestAddThree (0.00s)
goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
BenchmarkAdd
BenchmarkAdd-8          1000000000               0.2700 ns/op
BenchmarkLoopAdd
BenchmarkLoopAdd-8      1000000000               0.2356 ns/op
PASS
ok      command-line-arguments  3.612s

内存分配测试

在命令行中添加-benchmem参数以显示内存分配情况, benchmem要和bench一起使用

go test -v -bench=. -benchmem add_test.go add.go
=== RUN   TestAdd
--- PASS: TestAdd (0.00s)
=== RUN   TestAddThree
--- PASS: TestAddThree (0.00s)
goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
BenchmarkAdd
BenchmarkAdd-8          1000000000               0.2698 ns/op          0 B/op          0 allocs/op
BenchmarkLoopAdd
BenchmarkLoopAdd-8      1000000000               0.2321 ns/op          0 B/op          0 allocs/op
PASS
ok      command-line-arguments  1.214s

并发测试


func TestLoopAdd(t *testing.T) {
	testCases := []struct {
		A int
		B int
		C int
	}{
		{
			A: 1,
			B: 2,
			C: 3,
		},
		{
			A: 2,
			B: 3,
			C: 5,
		},
		{
			A: 3,
			B: 4,
			C: 7,
		},
	}
	for _, tc := range testCases {
		tc := tc
		t.Run(fmt.Sprintf("%d-%d", tc.A, tc.B), func(t *testing.T) {
			t.Parallel()
			if Add(tc.A, tc.B) != tc.C {
				t.Errorf("%d-%d != %d", tc.A, tc.B, tc.C)
			}
		})
	}
}

执行并发测试

go test -v -bench .

=== RUN   TestAdd
--- PASS: TestAdd (0.00s)
=== RUN   TestAddThree
--- PASS: TestAddThree (0.00s)
=== RUN   TestLoopAdd
=== RUN   TestLoopAdd/1-2
=== PAUSE TestLoopAdd/1-2
=== RUN   TestLoopAdd/2-3
=== PAUSE TestLoopAdd/2-3
=== RUN   TestLoopAdd/3-4
=== PAUSE TestLoopAdd/3-4
=== CONT  TestLoopAdd/1-2
=== CONT  TestLoopAdd/3-4
=== CONT  TestLoopAdd/2-3
--- PASS: TestLoopAdd (0.00s)
    --- PASS: TestLoopAdd/1-2 (0.00s)
    --- PASS: TestLoopAdd/3-4 (0.00s)
    --- PASS: TestLoopAdd/2-3 (0.00s)
goos: darwin
goarch: amd64
pkg: go.programming/testing/add
cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
BenchmarkAdd
BenchmarkAdd-8          1000000000               0.2794 ns/op
PASS
ok      go.programming/testing/add      0.717s

代码地址:
https://github.com/zyong/go.programming/tree/main/testing/add

参考

 类似资料: