gpt4 book ai didi

go - 无法生成 gin-gonic 服务器应用程序的代码覆盖率报告

转载 作者:数据小太阳 更新时间:2023-10-29 03:09:21 27 4
gpt4 key购买 nike

  • go版本:go版本go1.11.2 linux/amd64
  • gin 版本(或提交引用):提交 #5acf660
  • 操作系统:Ubuntu 16.04LTS

描述

我正在尝试使用示例应用程序为 gin 服务器生成代码覆盖率报告。示例.go

package main

import (
"github.com/gin-gonic/gin"
)

func main() {
r := gin.Default()
r.GET("/ep1", getEp1)
r.GET("/ep2", getEp2)
//r.Run()
}

func getEp1(c *gin.Context) {
}

func getEp2(c *gin.Context) {
}

这是我的测试文件:sample_test.go

package main

import (
"fmt"
"testing"
)

func TestRunMain(t *testing.T) {
fmt.Println("TestRunMain ...")
main()
}

生成代码覆盖率的命令:

$ go test -covermode=count -coverpkg ./... -test.coverprofile cover.cov

TestRunMain ...

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.

  • using env: export GIN_MODE=release

  • using code: gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET /ep1 --> _/home/ubuntu/tmp/sample.getEp1 (3 handlers)

[GIN-debug] GET /ep2 --> _/home/ubuntu/tmp/sample.getEp2 (3 handlers)

PASS

coverage: 100.0% of statements in ./...

ok _/home/ubuntu/tmp/sample 0.013s

这是 cover.cov 文件的内容:

mode: count

/home/ubuntu/tmp/sample/sample.go:7.13,12.2 3 1

/home/ubuntu/tmp/sample/sample.go:14.30,15.2 0 0

/home/ubuntu/tmp/sample/sample.go:17.30,18.2 0 0

到目前为止一切都很好!但如您所见,我还没有运行服务器。在文件:sample.go 中,当我取消注释行 r.Gin() 时,服务器运行。要退出应用程序,我需要执行 Ctrl+C。在这种情况下,不会生成代码覆盖率报告。 我错过了什么?

r.Gin() 在 sample.go 中未注释的命令行输出:

$ go test -covermode=count -coverpkg ./... -test.coverprofile cover.cov

TestRunMain ...

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.

  • using env: export GIN_MODE=release

  • using code: gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET /ep1 --> _/home/ubuntu/tmp/sample.getEp1 (3 handlers)

[GIN-debug] GET /ep2 --> _/home/ubuntu/tmp/sample.getEp2 (3 handlers)

[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default

[GIN-debug] Listening and serving HTTP on :8080

^Csignal: interrupt

FAIL _/home/ubuntu/tmp/sample 0.711s

cover.go 内容:

$ cat cover.cov

mode: count

谁能告诉我这里缺少什么?

最佳答案

我不太清楚 Gin 是什么,但我想我可以理解您的问题。在您的测试中,您正在调用具有 http 监听器的 main,这就是您提示的行。似乎您认为您需要 CTRL + C 才能让您的应用程序作为某种守护进程运行,但这是错误的,您正在做的是提示您的应用程序过早结束,这会中断您的测试并输出错误消息。

要回答您的问题,您需要创建一个测试套件,您可以在其中运行测试并在这些测试停止时选择它来关闭您的 http 服务器。看看这个:https://golang.org/pkg/testing/#hdr-Main

func TestMain(m *testing.M) {
/*set up your router or
database connections or
anything else you'll need
*/
exitCode := m.Run()
os.Exit(exitCode)
}

现在,在为您的端点运行测试时,您将需要发出模拟 http 请求,有点像真实用户。看看这个:

https://golang.org/pkg/net/http/httptest/

我将提供一个通用的小示例。

func ATest(t* testing.T){    
req, _ := http.NewRequest("GET", "/route", nil)
responseRecorder = httptest.NewRecorder()
router.ServeHTTP(responseRecorder, req)
if (http.StatusOk != responseRecorder.Code){
t.Fail()
}
}

如果这对你有帮助,请告诉我。

关于go - 无法生成 gin-gonic 服务器应用程序的代码覆盖率报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54591963/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com