golang testing 单元测试后为什么无法显示覆盖率?

我在执行单元测试后,所有方法都被测试,但唯独显示不出覆盖率,一下是信息:

➜  tests git:(develop) ✗ go test ./ -v -cover
=== RUN   Test_GetUsers
--- PASS: Test_GetUsers (0.08s)
=== RUN   Test_GetCurrentLocation
--- PASS: Test_GetCurrentLocation (0.01s)
=== RUN   Test_CreateOrUpdateLocationArgs
--- PASS: Test_CreateOrUpdateLocationArgs (0.00s)
=== RUN   Test_LocationCreateAndExists
--- PASS: Test_LocationCreateAndExists (0.07s)
=== RUN   Test_Delete
--- PASS: Test_Delete (0.02s)
=== RUN   Test_FindDashboard
--- PASS: Test_FindDashboard (0.01s)
=== RUN   Test_FindServiceType
--- PASS: Test_FindServiceType (0.00s)
=== RUN   Test_EditArgs
--- PASS: Test_EditArgs (0.00s)
=== RUN   Test_SortArgs
--- PASS: Test_SortArgs (0.00s)
PASS
coverage: [no statements]
ok      nimbus_backend/apisrv/tests    (cached)    coverage: [no statements]

大家看 coverage: no statements 但我查不到相关 no statements 的原因,所以在此求教, 我 go 的版本是 1.13 ubuntu 16.04,谢谢各位

阅读 7.8k
2 个回答

You can also generate coverage profile using Cover tool

$ go test -coverprofile=coverage.out

To analyze coverage via text prompt use

$ go tool cover -func=coverage.out

To analyze coverage via a browser, you can also use

$ go tool cover -html=coverage.out

Here are various options supported by Cover command

Usage of 'go tool cover':

Given a coverage profile produced by ‘go test’:

go test -coverprofile=c.out

Open a web browser displaying annotated source code:

go tool cover -html=c.out

Write out an HTML file instead of launching a web browser:

go tool cover -html=c.out -o coverage.html

Display coverage percentages to stdout for each function:

go tool cover -func=c.out

这个问题看起来挺久的,我也遇到了,同时回答顺便记录下避免后来者遇到无处可寻,其实是因为测试的被测试的文件与单元测试的文件需要在同一个目录,例如,你的测试文件是aaa.go 单元测试文件aaa_test.go,则应当需要在同一个包中,如果不是则会显示not statements,也算是一种规范问题。如果有看过开源项目代码就是这种规范

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏