- 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?