前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go测试

Go测试

原创
作者头像
用户9022575
修改2021-10-03 13:09:15
2720
修改2021-10-03 13:09:15
举报

go test工具

go test用于编译单元测试,检测方法函数是否有问题,熟悉下相关参数,可以让测试过程更新快捷

  • 直接运行编译整个项目的测试文件
代码语言:txt
复制

go test

代码语言:txt
复制
  • 测试单个测试文件,被测文件和对应单元测试成对出现,顺序可调换
代码语言:txt
复制

go test math.go math_test.go

代码语言:txt
复制
  • 查看详细结果
代码语言:txt
复制

go test -v

代码语言:txt
复制
  • 只测试某个函数,或者某几个函数,-run支持正则,如只测试 TestAddMore,TestAddMoreAndMore
代码语言:txt
复制

go test -v -run="TestAddMore"

代码语言:txt
复制
  • 生成test的二进制文件,加 -c 参数
代码语言:txt
复制

go test -c

代码语言:txt
复制
  • 执行这个text测试文件,加 -o 参数
代码语言:txt
复制

go test -v -o math.test.exe

代码语言:txt
复制
  • 查看覆盖率,加 -cover 参数
代码语言:txt
复制

go test -v -cover math.go math_test.go

代码语言:txt
复制

go单元测试

创建一个math.go,写一个add的Func函数,如下

代码语言:txt
复制
package main

func Add(x, y int) int {
	return x + y
}

同级建立一个math_test.go的测试文件

代码语言:txt
复制
package Test

import "testing"

type TestTable struct {
	xArg int
	yArg int
}

// 简单测试,单元测试
func TestAdd(t *testing.T) {
	t.Log(Add(1, 2))
}

// 少量样例测试,表组测试
func TestAddMore(t *testing.T) {
	sum := Add(1, 2)
	if sum == 3 {
		t.Log("the result is ok")
	} else {
		t.Fatal("the result is wrong")
	}

	sum = Add(2, 4)
	if sum == 6 {
		t.Log("the result is ok")
	} else {
		t.Fatal("the result is wrong")
	}
}

// 大量测试样例,表格测试法
func TestAddMoreAndMore(t *testing.T) {
	tables := []TestTable{
		{1, 2},
		{2, 4},
		{4, 8},
		{5, 10},
		{6, 12},
	}

	for _, table := range tables {
		result := Add(table.xArg, table.yArg)
		if result == (table.xArg + table.yArg) {
			t.Log("the result is ok")
		} else {
			t.Fatal("the result is wrong")
		}
	}
}
// === RUN   TestAdd
// math_test.go:12: 3
// --- PASS: TestAdd (0.00s)
// === RUN   TestAddMore
// math_test.go:19: the result is ok
// math_test.go:26: the result is ok
// --- PASS: TestAddMore (0.00s)
// === RUN   TestAddMoreAndMore
// math_test.go:45: the result is ok
// math_test.go:45: the result is ok
// math_test.go:45: the result is ok
// math_test.go:45: the result is ok
// math_test.go:45: the result is ok
// --- PASS: TestAddMoreAndMore (0.00s)
// PASS

go单元测试编写原则

  • 单元测试以_test.go结尾,最好就是 被测试文件_test.go
  • 单元测试函数名必须以 Test开口,如TestAdd
  • 单元测试函数接口一个指向 testing.T 类型的指针,而且不返回任何值

go mock工具

参考,未尝试

练习源码

Test

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • go test工具
  • go单元测试
    • go单元测试编写原则
    • go mock工具
    • 练习源码
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档