dsa456369 2019-02-08 11:58
浏览 90
已采纳

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

  • go version: go version go1.11.2 linux/amd64
  • gin version (or commit ref): Commit #5acf660
  • operating system: Ubuntu 16.04LTS

Description

I am trying to generate code coverage reports for a gin server using sample application. sample.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) {
}

This is my test file: sample_test.go

package main

import (
        "fmt"
        "testing"
)

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

Command to generate code coverage:

$ 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

Here is the content of cover.cov file:

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

Everything good so far! But as you can see I am not running the server yet. In file: sample.go, when I uncomment the line r.Gin(), server runs. To exit the application I need to perform Ctrl+C. In this case, there are no code coverage reports generated. What am I missing?

Command line output with r.Gin() uncommented in 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

Content of cover.go:

$ cat cover.cov

mode: count

Can anyone please tell me what I am missing here?

  • 写回答

1条回答 默认 最新

  • duanlang0025 2019-02-08 20:48
    关注

    I don't quite know what Gin is but I think I can see your problem. In your test you are calling main which has an http listener, that's the line that you're complaining about. Seems like you think you need CTRL + C to leave your application running as some sort of daemon but that's false, what you're doing is that you're prompting your application to end prematurely, which interrupts your tests and outputs an error message.

    To answer your question you need to create a test suite where you can run your tests and choose that when those tests are down to put down your http server as well. Have a look at this: 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)
    }
    

    Now when running tests for your endpoints you're gonna need to make mock http requests, kind of like a real user. Have a look at this:

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

    I'll provide a generic small example.

    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()
        }
    }
    

    Let me know if this helps ya.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型