逻辑测试
1.主要测试逻辑是否符合预期行为,使用 *testing.T
2.有错误的话通过t.Error(args …interface{}) 输出,或者通过 t.Errorf(format string, args …interface{}) 输出 format:格式 args:可变参数
3.测试文件以 “_test” 结尾
4.运行测试: go test | go “modName/packageName”
go test 运行项目中所有的测试
go test JSF/test 运行test包中的测试(go.mod:module JSF,使用的go mod modName:JSF)
5.如果要运行指定方法,可以使用-run “FunctionName”
go test JSF/test -run TestAdd
xieruixiangdeMacBook-Pro:one xieruixiang$ go test
PASS
ok JSF/test 0.191s
xieruixiangdeMacBook-Pro:one xieruixiang$ go test JSF/test
ok JSF/test (cached)
xieruixiangdeMacBook-Pro:one xieruixiang$ go test JSF/test -run TestAdd
ok JSF/test 0.103s
// test/add.go
package test
func CompareInt(a, b int) int {
if a > b {
return 1
} else if a == b {
return 0
} else {
return -1
}
}
//test/add_test.go
package test
import (
"testing"
)
func TestAdd(t *testing.T) {
//有错误的话通过t.Error(args ...interface{}) 输出,或者通过 t.Errorf(format string, args ...interface{}) 输出1 format:格式 args:可变参数
if CompareInt(2, 1) != 1 {
t.Error("2应该比1大")
}
if CompareInt(2, 1) != 0 {
t.Error("2应该等于2")
}
}
测试覆盖率
- go test 加上 -coverprofile=“filename”.out 可以将测试覆盖率写入filename.out文件中
xieruixiangdeMacBook-Pro:one xieruixiang$ go test JSF/test -coverprofile=cover.out
ok JSF/test 0.400s coverage: 80.0% of statements
- 使用 go tool cover -html filename.out 以html页面形式查看哪些分支未覆盖到
xieruixiangdeMacBook-Pro:one xieruixiang$ go tool cover -html cover.out
基准测试(性能)
1.测试执行性能,使用 *testing.B
2.文件还是以"_test"结尾,方法以"Benchmark"开头
package test
import (
"testing"
)
func TestAdd(t *testing.T) {
//有错误的话通过t.Error(args ...interface{}) 输出,或者通过 t.Errorf(format string, args ...interface{}) 输出1 format:格式 args:可变参数
if CompareInt(2, 1) != 1 {
t.Error("2应该比1大")
}
if CompareInt(2, 2) != 0 {
t.Error("2应该等于2")
}
}
//文件还是以"_test"结尾,方法以"Benchmark"开头
func BenchmarkAdd1(t *testing.B) {
//有错误的话通过t.Error(args ...interface{}) 输出,或者通过 t.Errorf(format string, args ...interface{}) 输出1 format:格式 args:可变参数
if CompareInt(2, 1) != 1 {
t.Error("2应该比1大")
}
if CompareInt(2, 2) != 0 {
t.Error("2应该等于2")
}
}
func BenchmarkAdd2(t *testing.B) {
//有错误的话通过t.Error(args ...interface{}) 输出,或者通过 t.Errorf(format string, args ...interface{}) 输出1 format:格式 args:可变参数
if CompareInt(2, 1) != 1 {
t.Error("2应该比1大")
}
if CompareInt(2, 2) != 0 {
t.Error("2应该等于2")
}
}
3.运行 go test + “-bench”
# go test JSF/test -bench .
# 运行JSF/test中所有Benchmark开头的函数(基准测试)
xieruixiangdeMacBook-Pro:one xieruixiang$ go test JSF/test -bench .
goos: darwin
goarch: amd64
pkg: JSF/test
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkAdd1-12 1000000000 0.0000002 ns/op
BenchmarkAdd2-12 1000000000 0.0000002 ns/op
PASS
ok JSF/test 0.484s
# go test JSF/test -bench Add2
# 运行JSF/test中BenchmarkAdd2函数
xieruixiangdeMacBook-Pro:one xieruixiang$ go test JSF/test -bench Add2
goos: darwin
goarch: amd64
pkg: JSF/test
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkAdd2-12 1000000000 0.0000002 ns/op
PASS
ok JSF/test 0.223s
# go test -bench .
# 运行项目所有的基准测试
xieruixiangdeMacBook-Pro:one xieruixiang$ go test -bench .
goos: darwin
goarch: amd64
pkg: JSF/test
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkAdd1-12 1000000000 0.0000003 ns/op
BenchmarkAdd2-12 1000000000 0.0000002 ns/op
PASS
ok JSF/test 0.206s