Go Gin Gonic Unit Testing Deployment Issue

倖福魔咒の 提交于 2020-12-15 05:29:45

问题


I have a Go API built using the Gin framework.

Reading the docs in the testing section here, i tried to implement something similar:

main.go

package main

import (
    "mes/routes"

    "github.com/gin-gonic/gin"
)

func setupMainRoutes(engine *gin.Engine) {
    engine.GET("/mesg/:language/services", routes.AllServices)
    engine.GET("/mesg/:language/service/:id", routes.OneService)
    engine.GET("/mesg/:language/services/search", routes.SearchService)
}

func setupErrorRoutes(engine *gin.Engine) {
    engine.NoRoute(routes.Error404Handler)
}

func setupServer() *gin.Engine {
    // Gin Mode
    gin.SetMode(gin.ReleaseMode)

    // Creates the Gin Engine
    engine := gin.New()

    // Setup the API Routes
    setupMainRoutes(engine)

    // Setup Error Routes
    setupErrorRoutes(engine)

    // Return engine
    return engine
}

func main() {
    // Run the engine
    setupServer().Run()
}

main_test.go

package main

import (
    "net/http"
    "net/http/httptest"
    "testing"

    "github.com/stretchr/testify/assert"
)

/************** Error Handling Tests **************/
func TestPing404Errors1Of3(t *testing.T) {
    router := setupServer()

    w := httptest.NewRecorder()
    req, _ := http.NewRequest("GET", "/", nil)
    router.ServeHTTP(w, req)

    assert.Equal(t, 404, w.Code)
}

Since i have to use the same package main to access the setupServer() function, i am running into issues when deploying my API to digital ocean. The build is failing. If i change the package name to another, it deploys successfully but then my unit testing won't run.

I want to be able to deploy my app and also keep my unit tests.

Digital Ocean just says "Build Failed"

I ran into the same issues when deploying on Heroku but i could alleviate this by creating a .slugignore and put the test files in there so that Heroku can ignore these.

Can someone provide a fix to that ?

来源:https://stackoverflow.com/questions/64839861/go-gin-gonic-unit-testing-deployment-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!