9.2 代码覆盖率和性能测试
一、代码覆盖率
IDEA中运行,选择
...with coverage,可以看到代码覆盖率,测试文件中,绿色的是测试过的,红色的是未覆盖到的命令行进行覆盖率测试
// 覆盖测试并生成覆盖测试文件 go test -coverprofile=c.out // 查看输出结果文件 go tool cover -html=c.out备注:c.out是自定义的输出文件名,打开的html中,绿色的是覆盖到的,红色的是未覆盖的
二、性能测试
性能测试代码
func BenchmarkSubstr(b *testing.B) { a, d, c := 3, 4, 5 for i := 0; i < b.N; i++ { if actual := calcTriangle(a, d); actual != c { b.Errorf("测试结果异常,参数:%d, %d, %d", a, d, c) } } }备注:性能测试循环次数由系统决定,因此使用
b.N即可 性能测试使用
testing.B,与之前测试不同,代码覆盖率的测试与普通测试相同运行方式:
IDEA:正常运行即可
命令行测试:
go test -bench .
Last updated
Was this helpful?