当前位置:网站首页>Go 单元测试
Go 单元测试
2022-08-01 15:55:00 【zakariyaa33】
单元测试就是在终端使用go test进行我们自定义的测试,代码编写要求如下:
Go 语言推荐测试文件和源代码文件放在一块,测试文件以 _test.go 结尾,例如我们需要测试写在mytry.go里的Add函数,则测试文件名为mytry_test.go
测试用的函数名为需要测试的函数前加Test,如要测试Add,则测试用函数名为TestAdd
有多个待测试函数时,可以用-run来选择运行其中的一个用例
go test -run TestAdd
-v可以显示每个用例的测试结果
多个子测试
//mytry.go
package main
func Add(a int, b int) int {
return a + b
}
func main(){
ret:=Add(1,2)
}
//mytry_test.go
package main
import "testing"
func TestAdd(t *testing.T){
cases:=[]struct{
Name string
A,B,Want int
}{
{
"pos",2,3,5},
{
"neg",2,-4,-2},
{
"zero",2,-2,0},
}
for _,c:=range cases{
t.Run(c.Name,func(t *testing.T){
if Get:=Add(c.A,c.B);Get!=c.Want{
t.Errorf("Name:%s,Want:%d,Get:%d",c.Name,c.Want,Get)
}
})
}
}
go test -run TestAdd -v
=== RUN TestAdd
=== RUN TestAdd/pos
=== RUN TestAdd/neg
=== RUN TestAdd/zero
--- PASS: TestAdd (0.00s)
--- PASS: TestAdd/pos (0.00s)
--- PASS: TestAdd/neg (0.00s)
--- PASS: TestAdd/zero (0.00s)
PASS
ok goProject 0.002s
go test -run TestAdd/zero -v
=== RUN TestAdd
=== RUN TestAdd/zero
--- PASS: TestAdd (0.00s)
--- PASS: TestAdd/zero (0.00s)
PASS
ok goProject 0.002s
帮助函数
t.Helper(),用于标注该函数是帮助函数,报错时将输出帮助函数调用者的信息,而不是帮助函数的内部信息。
// calc_test.go
package main
import "testing"
type calcCase struct{
A, B, Expected int }
func createMulTestCase(t *testing.T, c *calcCase) {
// t.Helper()
if ans := Mul(c.A, c.B); ans != c.Expected {
t.Fatalf("%d * %d expected %d, but %d got",
c.A, c.B, c.Expected, ans)
}
}
func TestMul(t *testing.T) {
createMulTestCase(t, &calcCase{
2, 3, 6})
createMulTestCase(t, &calcCase{
2, -3, -6})
createMulTestCase(t, &calcCase{
2, 0, 1}) // wrong case
}
在这里,我们故意创建了一个错误的测试用例,运行 go test,用例失败,会报告错误发生的文件和行号信息:
$ go test
--- FAIL: TestMul (0.00s)
calc_test.go:11: 2 * 0 expected 1, but 0 got
FAIL
exit status 1
FAIL example 0.007s
可以看到,错误发生在第11行,也就是帮助函数 createMulTestCase 内部。18, 19, 20行都调用了该方法,我们第一时间并不能够确定是哪一行发生了错误。有些帮助函数还可能在不同的函数中被调用,报错信息都在同一处,不方便问题定位。
如果我们标注帮助函数
func createMulTestCase(c *calcCase, t *testing.T) {
t.Helper()
t.Run(c.Name, func(t *testing.T) {
if ans := Mul(c.A, c.B); ans != c.Expected {
t.Fatalf("%d * %d expected %d, but %d got",
c.A, c.B, c.Expected, ans)
}
})
}
运行 go test,报错信息如下,可以非常清晰地知道,错误发生在第 20 行。
$ go test
--- FAIL: TestMul (0.00s)
calc_test.go:20: 2 * 0 expected 1, but 0 got
FAIL
exit status 1
FAIL example 0.006s
网络测试
假设需要测试某个 API 接口的 Handler 能够正常工作,例如 HelloHandler
func HelloHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
}
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestHelloHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil) //这里的url好像可以随便写
if err != nil {
fmt.Println(err)
}
ret := httptest.NewRecorder()
HelloHandler(ret, req)
if ret.Body.String() != "hello world" {
fmt.Println("handler erro")
}
}
$ go test -run TestHelloHandler -v
=== RUN TestHelloHandler
--- PASS: TestHelloHandler (0.00s)
PASS
ok goProject 0.002s
测试覆盖率
go test -v -coverprofile=c.out
go tool cover -html=c.out
会在浏览器生成一个包含测试函数的页面,绿色的就是覆盖到了的,红色是没覆盖到的
边栏推荐
- 2.8K 120Hz触控双屏加持 灵耀X 双屏Pro 2022让办公无惧想象
- VIM使用指南(7)单词移动/删除技巧
- Inflation continues, Kenya's food security a concern
- IronOS, an open source system for portable soldering irons, supports a variety of portable DC, QC, PD powered soldering irons, and supports all standard functions of smart soldering irons
- MySQL中的存储过程(详细篇)
- LeetCode50天刷题计划(Day 9—— 整数转罗马数字(20.40-22.10)
- May 20, 2022 The most complete fish game navigation
- js邯郸市地图网页源码下载
- what is tail tooth feast
- hzero-resource秒退
猜你喜欢
随机推荐
JSON数据转换总结(VIP典藏版)
实习日报-2022-7-30
8年软件测试工程师感悟 —— 写给还在迷茫中的朋友
Arduino无线下载 Arduino USB接口无线自动下载程序
指针进阶(三)之指针与数组笔试题
通胀持续 肯尼亚粮食安全引关注
LeetCode50天刷题计划(Day 8—— 盛最多水的容器(23.00-1.20)
gconf/dconf实战编程(3)利用dconf库读写配置实战以及诸多配套工具演示
WPF如何自定义隐藏下拉框选项
珠海市生物安全P3实验室主体结构封顶
【LeetCode】37、解数独
Pytorch - Distributed Model Training
Slider/Carousel图片切换支持触摸屏
eslint语法报错解决
MySQL查询上的问题
接口测试框架开发实践5:配置文件读取
what is tail tooth feast
js判断是pc端还是移动端(包括ipad)
2022年7月最热的10篇AI论文
反序列化漏洞详解









