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

编程入门 行业动态 更新时间:2024-10-28 07:20:32
本文介绍了如何衡量Golang集成测试覆盖率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用go test -cover来衡量我正在构建的服务的测试覆盖率.这是一个REST API,我通过旋转,测试HTTP请求并查看HTTP响应来对其进行测试.这些测试不是服务包的一部分,并且go tool cover返回0%的测试覆盖率.有没有办法获得实际的测试覆盖率?我希望在给定的端点上进行最佳方案测试,至少可以覆盖特定端点处理程序的30-50%的代码,并希望通过添加更多针对常见错误的测试来进一步改善这一情况.

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.

推荐答案

我指向了-coverpkg指令,该指令可以执行我需要的操作-测量特定程序包中的测试覆盖率,即使使用该程序包的测试和不是它的一部分.例如:

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

相比

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

在上面的示例中,我在使用package mypackage的package main中的main_test.go中进行了测试.我对package mypackage的覆盖范围最感兴趣,因为它包含了项目中99%的业务逻辑.

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.

我对Go语言很陌生,因此这可能不是通过集成测试衡量测试覆盖率的最佳方法.

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.

更多推荐

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

本文发布于:2023-11-14 10:43:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:覆盖率   测试   Golang

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!