0
votes
  • 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

1 Answers

1
votes

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.