目录
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 ./...