dourong4031 2019-09-18 10:22
浏览 84

如何为REST服务生成代码覆盖率

I want to get test coverage of a REST service written in Go. I am spawning REST service through a goroutine, then making HTTP requests using rest client, and reviewing the HTTP responses. Tests are passing successfully but go test -cover returns 0% test coverage. Is there a way to get the actual test coverage of all the packages used inside the go lang REST service.

my test file: main_test.go

import (
    "testing"
)

// Test started when the test binary is started. Only calls main.
func TestSystem(t *testing.T) {
    go main()    // Spinning up the go lang REST server in a separate go routine.
    http.Post("https://localhost/do_something")
}

my output:

go test -cover  main_test.go
ok      command-line-arguments  0.668s  coverage: 0.0% of statements 
  • 写回答

3条回答 默认 最新

  • duanguzhong5776 2019-09-18 11:09
    关注

    Regardless of the root cause, I think there’s an issue with your general approach here.

    I would suggest that some of what you’re testing here isn’t your code and you shouldn’t bother. You don’t need to actually test the http protocol and http package, right?

    So the way I’ve handled this in the past is to break apart my the meaningful code that I want to test out of the http handler functions. The http handlers should only be responsible for validating input, calling the actual business logic, and then formatting output.

    Once you’ve broken apart your code this way, you can directly call your meaningful functions from your tests. Your http handler functions should be so simple that there is no place for bugs to lurk.

    Don’t seek 100% test coverage. Test what matters.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题