douzhajie7168 2015-02-05 16:27
浏览 131
已采纳

如何衡量Golang集成测试覆盖率?

I am trying to use go test -cover to measure the test coverage of a service I am building. It is a REST API and I am testing it by spinning it up, making test HTTP requests and reviewing the HTTP responses. These tests are not part of the packages of the services and go tool cover returns 0% test coverage. Is there a way to get the actual test coverage? I would expect a best-case scenario test on a given endpoint to cover at least 30-50% of the code for specific endpoint handler, and by adding more tests for common error to improve this further.

  • 写回答

3条回答 默认 最新

  • doukougua7873 2015-02-06 10:52
    关注

    I was pointed at the -coverpkg directive, which does what I need - measures the test coverage in a particular package, even if tests that use this package and not part of it. For example:

    $ go test -cover -coverpkg mypackage ./src/api/...
    ok      /api    0.190s  coverage: 50.8% of statements in mypackage
    ok      /api/mypackage   0.022s  coverage: 0.7% of statements in mypackage
    

    compared to

    $ go test -cover ./src/api/...
    ok      /api    0.191s  coverage: 71.0% of statements
    ok      /api/mypackage   0.023s  coverage: 0.7% of statements
    

    In the example above, I have tests in main_test.go which is in package main that is using package mypackage. I am mostly interested in the coverage of package mypackage since it contains 99% of the business logic in the project.

    I am quite new to Go, so it is quite possible that this is not the best way to measure test coverage via integration tests.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。