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.

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。