登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-03-16 15:54:28 455浏览 收藏

**无法生成 Gin 服务器应用程序的代码覆盖率报告** 在使用 Gin 服务器框架时,无法生成代码覆盖率报告。测试中调用具有 HTTP 侦听器的 `main` 函数,导致应用程序在测试关闭之前结束。需要创建测试套件,在关闭 HTTP 服务器时选择该套件。另外,需要模拟 HTTP 请求以运行端点测试。

问题内容

  • 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 测试运行主... [gin-debug] [警告] 创建一个已附加记录器和恢复中间件的引擎实例。 [gin-debug] [警告] 在“调试”模式下运行。在生产中切换到“发布”模式。 使用环境:export gin_mode=release 使用代码:gin.setmode(gin.releasemode) [gin-debug] get /ep1 --> _/home/ubuntu/tmp/sample.getep1 (3 个处理程序) [gin-debug] get /ep2 --> _/home/ubuntu/tmp/sample.getep2 (3 个处理程序) 经过 覆盖率:./... 中 100.0% 的语句 好的 _/home/ubuntu/tmp/sample 0.013s

这是 cover.cov 文件的内容:

模式:计数 /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。在这种情况下,不会生成代码覆盖率报告。 我错过了什么?

在sample.go中取消注释r.gin()的命令行输出:

$ go test -covermode=count -coverpkg ./... -test.coverprofile cover.cov 测试运行主... [gin-debug] [警告] 创建一个已附加记录器和恢复中间件的引擎实例。 [gin-debug] [警告] 在“调试”模式下运行。在生产中切换到“发布”模式。 使用环境:export gin_mode=release 使用代码:gin.setmode(gin.releasemode) [gin-debug] get /ep1 --> _/home/ubuntu/tmp/sample.getep1 (3 个处理程序) [gin-debug] get /ep2 --> _/home/ubuntu/tmp/sample.getep2 (3 个处理程序) [gin-debug] 环境变量 port 未定义。默认使用端口:8080 [gin-debug] 在 :8080 上监听和服务 http ^c信号:中断 失败_/home/ubuntu/tmp/sample 0.711s

cover.go 的内容:

$ 猫盖.cov 模式:计数

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


解决方案


我不太知道杜松子酒是什么,但我想我可以看到你的问题。在您的测试中,您正在调用具有 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()
    }
}

请告诉我这是否对您有帮助。

本篇关于《无法生成 gin-gonic 服务器应用程序的代码覆盖率报告》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>