golang 单元测试
目录
GoTests工具自动化test使用
1.插件包地址:
https://github.com/cweill/gotests
安装
go get -u github.com/cweill/gotests/...
会在go.mod里引入插件包
2.为选择的方法创建单元测试方法
3.测试单个函数方法
3.1.1在测试的文件里会生成如下代码
part_one/hello_world_test.go
func TestHelloGo(t *testing.T) {
tests := []struct {
name string
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := HelloGo(); got != tt.want {
t.Errorf("HelloGo() = %v, want %v", got, tt.want)
}
})
}
}
3.1.2.1在TODO写入通过的测试数据
func TestHelloGo(t *testing.T) {
tests := []struct {
name string
want string
}{
// TODO: Add test cases.
{
"test TestHelloGo",
"hello go",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := HelloGo(); got != tt.want {
t.Errorf("HelloGo() = %v, want %v", got, tt.want)
}
})
}
}
3.1.2.2.运行测试
$ cd /media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/go-how-to-write-test/part_one
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/go-how-to-write-test/part_one$ go test -v -cover -run TestHelloGo
Before ====================
=== RUN TestHelloGo
=== RUN TestHelloGo/test_TestHelloGo
--- PASS: TestHelloGo (0.00s)
--- PASS: TestHelloGo/test_TestHelloGo (0.00s)
PASS
coverage: 33.3% of statements
End ====================
ok github.com/wuxiaoxiaoshen/go-how-to-wirte-test/part_one 0.025s
3.1.3.1在TODO写入不通过的测试数据
func TestHelloGo(t *testing.T) {
tests := []struct {
name string
want string
}{
// TODO: Add test cases.
{
"test TestHelloGo",
"hello go111",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := HelloGo(); got != tt.want {
t.Errorf("HelloGo() = %v, want %v", got, tt.want)
}
})
}
}
3.1.3.2运行测试
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/go-how-to-write-test/part_one$ go test -v -cover -run TestHelloGo
Before ====================
=== RUN TestHelloGo
=== RUN TestHelloGo/test_TestHelloGo
hello_world_test.go:126: HelloGo() = hello go, want hello go1
--- FAIL: TestHelloGo (0.00s)
--- FAIL: TestHelloGo/test_TestHelloGo (0.00s)
FAIL
coverage: 33.3% of statements
End ====================
exit status 1
FAIL github.com/wuxiaoxiaoshen/go-how-to-wirte-test/part_one 0.042s
4.测试单个文件,一定要带上被测试的原文件
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/go-how-to-write-test/part_one$ go test -v -cover hello_world_test.go hello_world.go
Before ====================
=== RUN TestHello
hello_world_test.go:13: Hello() = Hello World, want Hello World
hello_world_test.go:20: Hello() = Hello World, want Hello World
--- PASS: TestHello (0.00s)
=== RUN TestHello2
=== RUN TestHello2/test_for_hello_with_run
hello_world_test.go:31: Hello() = Hello World, want = Hello World
--- PASS: TestHello2 (0.00s)
--- PASS: TestHello2/test_for_hello_with_run (0.00s)
=== RUN TestHelloWithTable
=== RUN TestHelloWithTable/test_for_hello
--- PASS: TestHelloWithTable (0.00s)
--- PASS: TestHelloWithTable/test_for_hello (0.00s)
=== RUN TestWorld
hello_world_test.go:86: ok
--- PASS: TestWorld (0.00s)
=== RUN Test_world
=== RUN Test_world/#00
got: world
hello_world_test.go:106: world() != world, want
--- FAIL: Test_world (0.00s)
--- FAIL: Test_world/#00 (0.00s)
=== RUN TestHelloGo
=== RUN TestHelloGo/test_TestHelloGo
--- PASS: TestHelloGo (0.00s)
--- PASS: TestHelloGo/test_TestHelloGo (0.00s)
=== RUN ExampleHello
--- PASS: ExampleHello (0.00s)
FAIL
coverage: 100.0% of statements
End ====================
FAIL command-line-arguments 0.023s
FAIL
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/go-how-to-write-test/part_one$
5.测试指定文件夹下整个包所有文件的单元测试
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/go-how-to-write-test/part_one$ ll
总用量 9
drwxrwxrwx 1 haima haima 0 10月 6 20:01 .
drwxrwxrwx 1 haima haima 4096 10月 6 17:55 ..
-rwxrwxrwx 1 haima haima 220 10月 6 17:38 hello_world.go
-rwxrwxrwx 1 haima haima 2576 10月 6 20:01 hello_world_test.go
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/go-how-to-write-test/part_one$ go test -v -cover ./
Before ====================
=== RUN TestHello
hello_world_test.go:13: Hello() = Hello World, want Hello World
hello_world_test.go:20: Hello() = Hello World, want Hello World
--- PASS: TestHello (0.00s)
=== RUN TestHello2
=== RUN TestHello2/test_for_hello_with_run
hello_world_test.go:31: Hello() = Hello World, want = Hello World
--- PASS: TestHello2 (0.00s)
--- PASS: TestHello2/test_for_hello_with_run (0.00s)
=== RUN TestHelloWithTable
=== RUN TestHelloWithTable/test_for_hello
--- PASS: TestHelloWithTable (0.00s)
--- PASS: TestHelloWithTable/test_for_hello (0.00s)
=== RUN TestWorld
hello_world_test.go:86: ok
--- PASS: TestWorld (0.00s)
=== RUN Test_world
=== RUN Test_world/#00
got: world
hello_world_test.go:106: world() != world, want
--- FAIL: Test_world (0.00s)
--- FAIL: Test_world/#00 (0.00s)
=== RUN TestHelloGo
=== RUN TestHelloGo/test_TestHelloGo
--- PASS: TestHelloGo (0.00s)
--- PASS: TestHelloGo/test_TestHelloGo (0.00s)
=== RUN ExampleHello
--- PASS: ExampleHello (0.00s)
FAIL
coverage: 100.0% of statements
End ====================
FAIL github.com/wuxiaoxiaoshen/go-how-to-wirte-test/part_one 0.013s
FAIL
测试整个项目里所有*_test.go结尾文件的单面测试
$ cd /media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/go-how-to-write-test
haima@haima-PC:/media/haima/34E401CC64DD0E28/site/go/src/haimait/learn/go-how-to-write-test$ go test -coverprofile=couver.out -v ./...
第三方测试包
[Haima的博客]
http://www.cnblogs.com/haima/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构