Search code examples
gotestingprogram-entry-pointgo-gin

How to test main function in gin application?


How can I test func main? Like this:

func main(){
     Engine := GetEngine() // returns gin router with handlers atttached 
     Engine.Run(":8080")
}

It has only 2 lines but I'd like to have them covered. TestMain' is reserved for test preparation, does that mean testing main was not planned by language creators? I can move the contents to another function mainReal but it seems to be some over engineering?

How to test gin has started well? Can I launch main in separate goroutine, check reply and stop it?

Thanks.

P.S. Possible duplicate is not precise duplicate because it is dedicated not to testing of func main() itself, but rather ideas to move in outside and so contains different issue and approach.


Solution

  • Solution.

    You may test function main() from package main the same way, just do not name it TestMain. I launch it as a separate goroutine, than try to connect to it and perform any request.

    I decided to connect to auxilary handler which should respond with a simple json {"status": "ok"}. In my case:

    func TestMainExecution(t *testing.T) {
        go main()
        resp, err := http.Get("http://127.0.0.1:8080/checkHealth") 
        if err != nil {
                t.Fatalf("Cannot make get: %v\n", err)
        }
        bodySb, err := ioutil.ReadAll(resp.Body)
        if err != nil {
                t.Fatalf("Error reading body: %v\n", err)
        }
        body := string(bodySb)
        fmt.Printf("Body: %v\n", body)
        var decodedResponse interface{}
        err = json.Unmarshal(bodySb, &decodedResponse)
        if err != nil {
                t.Fatalf("Cannot decode response <%p> from server. Err: %v", bodySb, err)
        }
        assert.Equal(t, map[string]interface{}{"status": "ok"}, decodedResponse,
                "Should return status:ok")
    }