all*_*mon 4 code-coverage functional-testing go
我有一个 go webservices(REST Api),我们对其进行了单元测试,并且 go cover 工作得很好。
现在我们有一个用 python 编写的测试套件,它启动服务器实例,运行测试,停止服务器。
我想知道是否有一些工具允许我使用特定标志运行我的服务器二进制文件,以便最后它打印我的“黑盒”测试执行的测试的覆盖范围?
谢谢。
基于这篇文章, 我做了以下事情:
创建了一个main_test.go包含以下内容的:
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()
}
Run Code Online (Sandbox Code Playgroud)因为它是一个 Web 服务(因此处于无限循环中),我需要一种在 SIGTERM 上优雅退出的方法(而不被认为是失败),所以我使用了该包go get gopkg.in/tylerb/graceful.v1并替换了(我使用go-restful) main.go 行
- log.Fatal(http.ListenAndServe(":"+port, nil))
+ graceful.Run(":"+port, 10*time.Second, nil)
Run Code Online (Sandbox Code Playgroud)然后我会像这样运行测试
go test -c -covermode=count -coverpkg ./... -o foo.test./foo.test -test.coverprofile coverage.cov & echo $! > /tmp/test.pidkill "$(cat /tmp/test.pid)"