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 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)