donglingsai2880 2016-01-17 10:58
浏览 458
已采纳

golang生成功能测试的代码覆盖率

I have a go webservices (a REST Api) for which we have unit test, and for which go cover works fine.

Now we have a test suite written in python that launch a instance of the server, run the test , stop the server.

I would like to know if there's some tools that would permit me to run my server binary with a specific flag , so that at the end it prints coverage of the tests executed by my "blackbox" testing ?

Thanks.

  • 写回答

2条回答 默认 最新

  • doudui9516 2016-01-17 15:48
    关注

    Based on this post here's what I did:

    1. created a main_test.go with this content:

      package main
      
      // code based on technique explained here:
      // https://www.elastic.co/blog/code-coverage-for-your-golang-system-tests
      // you can look there if you want to see how not to execute this test
      // when running unit test etc.
      
      // This file is mandatory as otherwise the packetbeat.test binary is not generated correctly.
      
      import (
          "testing"
      )
      
      // Test started when the test binary is started. Only calls main.
      func TestSystem(t *testing.T) {
          main()
      }
      
    2. as it was a web services (and hence in an infinite loop), I needed a way to gracefully exit on SIGTERM (without it being considered a failure), so I used the package go get gopkg.in/tylerb/graceful.v1 and replaced (I use go-restful) in main.go the line

          -       log.Fatal(http.ListenAndServe(":"+port, nil))
          +       graceful.Run(":"+port, 10*time.Second, nil)
      
    3. then I would run the test like this

      • go test -c -covermode=count -coverpkg ./... -o foo.test
      • ./foo.test -test.coverprofile coverage.cov & echo $! > /tmp/test.pid
      • run my test suite
      • kill "$(cat /tmp/test.pid)"
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?