Gin: Test handlers and end to end tests

Created on 1 Mar 2016  路  15Comments  路  Source: gin-gonic/gin

Hi everyone,

First I'd like to thanks every contributor for this great project, it's working marvelously well.

Second, I have question about handler tests and end to end tests with Gin. I'm relatively new to Go and I have trouble to write something that suit me and seems idiomatic.

I tried to search for example but nothing stood up. Do you have any example in mind in a good way to test routes handlers or the whole application itself using net/http/httptest ?

Thanks,
Albin.

Most helpful comment

Hi @otraore @AlbinOS @widnyana @jcmartins

I write testing tool for gin framework. Please try it.

https://github.com/appleboy/gofight

package example

import (
  "github.com/gin-gonic/gin"
  "net/http"
)

func helloHandler(c *gin.Context) {
  c.String(http.StatusOK, "Hello World")
}

func GinEngine() *gin.Engine {
  gin.SetMode(gin.TestMode)
  r := gin.New()

  r.GET("/", helloHandler)

  return r
}

gin_test.go

package example

import (
  "github.com/appleboy/gofight"
  "github.com/stretchr/testify/assert"
  "net/http"
  "testing"
)

func TestGinHelloWorld(t *testing.T) {
  r := gofight.New()

  r.GET("/").
    SetDebug(true).
    Run(GinEngine(), func(r gofight.HttpResponse, rq gofight.HttpRequest) {
      assert.Equal(t, "Hello World", r.Body.String())
      assert.Equal(t, http.StatusOK, r.Code)
    })
}

if you have any problem, please contact me.

All 15 comments

Hey, never mind, I found #37 and #55.

However, it's maybe a good idea to write a section in the README.md about that to facilitate information gathering ?

@AlbinOS agreed with you, if the maintainer agreed too, IMHO you can submit a pull request for this. :)

@widnyana, indeed, I'd like to submit such pull request if it suits maintainers. I'm working on my end to end tests on my application at the moment, I'd write something once I'm done with that.

@AlbinOS That'd be amazing.

Is possible using gotests with gin ?

I didn't forget this, I'll try to catch up as soon as possible.

@jcmartins, I don't use gotests but I have some table driven test for my gin application testing the router. We can discuss this after my PR to see if it's possible to generate something similar with gotests.

Gin is compatible with any http testing mechanism out there.

httptest:

package foo

import (
    "github.com/gin-gonic/gin"
    "github.com/stretchr/testify/assert"
    "net/http"
    "net/http/httptest"
    "testing"
)

func TestFoo(t *testing.T) {
    handler := func(c *gin.Context) {
        c.String(http.StatusOK, "bar")
    }

    router := gin.New()
    router.GET("/foo", handler)

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

    assert.Equal(t, resp.Body.String(), "bar")
}

GoConvey (https://github.com/smartystreets/goconvey/) which I personally use:

func TestUsersResource(t *testing.T) {
    r := controllers.Router() //Insert your router here (Mine is defined in a package controllers)
    Convey("Empty POST request to /users should return Bad Request", t, func() {
        req, _ := http.NewRequest("POST", "/users", nil)
        resp := httptest.NewRecorder()
        r.ServeHTTP(resp, req)
        So(resp.Code, ShouldEqual, http.StatusBadRequest)
    })
    ...
}

Hi @otraore @AlbinOS @widnyana @jcmartins

I write testing tool for gin framework. Please try it.

https://github.com/appleboy/gofight

package example

import (
  "github.com/gin-gonic/gin"
  "net/http"
)

func helloHandler(c *gin.Context) {
  c.String(http.StatusOK, "Hello World")
}

func GinEngine() *gin.Engine {
  gin.SetMode(gin.TestMode)
  r := gin.New()

  r.GET("/", helloHandler)

  return r
}

gin_test.go

package example

import (
  "github.com/appleboy/gofight"
  "github.com/stretchr/testify/assert"
  "net/http"
  "testing"
)

func TestGinHelloWorld(t *testing.T) {
  r := gofight.New()

  r.GET("/").
    SetDebug(true).
    Run(GinEngine(), func(r gofight.HttpResponse, rq gofight.HttpRequest) {
      assert.Equal(t, "Hello World", r.Body.String())
      assert.Equal(t, http.StatusOK, r.Code)
    })
}

if you have any problem, please contact me.

@appleboy I've check it and try to write a rought test using your gofight, seems cool so far :+1:

I'll try to finish my test suite for gallang, then I'll catch you again :smiley:

@widnyana Thanks. If you have any problem, please feel free to contact me.

@appleboy, thanks a lot for your response and your work. I'll definitely have a look at gofight !

However, this issue was not really about creating some tools or techniques to test a gin application but more to ask for test documentation. How to do it well and in a maintenable way.

Maybe adding a short section about testing in the README of the framework is the way to go ?

@AlbinOS OK. I will add some example code about testing in the README of the framework.

cc @javierprovecho

@AlbinOS I write some testing using gin framework. Can you help to review it?

Testing code: https://github.com/appleboy/gofight/blob/master/gofight_test.go#L14-L133

if no problem, I will send the PR to update readme.

I wrote a log in and registration using gin-golang.i have no idea about how to test it.please anyone help me to start writing the testing code

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xpbliss picture xpbliss  路  3Comments

CodingPapi picture CodingPapi  路  3Comments

iiinsomnia picture iiinsomnia  路  3Comments

nxvl picture nxvl  路  3Comments

gplume picture gplume  路  3Comments