首页 > 其他 > 详细

go_test

时间:2021-02-05 23:26:32      阅读:33      评论:0      收藏:0      [点我收藏+]
package test

import (
	"os"
	"strconv"
	"testing"
)

func TestMain(m *testing.M) {
	// before test
	retCode := m.Run()
	// after test
	os.Exit(retCode)
}

// 单元测试
func TestAdd(t *testing.T) {
	data := [...][3]int{
		{1, 2, 3},
		{2, 2, 3},
	}

	// 测试组
	// for _, a := range data {
	// 	if a[2] != Add(a[0], a[1]) {
	// 		t.Error("test failed")
	// 	}
	// }

	// 子测试
	for i, a := range data {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			res := Add(a[0], a[1])
			if res != a[2] {
				t.Error("test failed")
			}
		})
	}
}

// go test -v
// go test -cover
// 	go test -cover -coverprofile=c.out # 将覆盖率相关的记录信息输出到一个文件
// 	go tool cover -html=c.out

// 性能测试
func BenchmarkAdd(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = Add(1, 2)
	}
}

// go test -bench=Add
// go test -bench=Split -benchmem 获取内存参数

go_test

原文:https://www.cnblogs.com/zhh567/p/14379720.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!