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.
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 ?
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
@AswathyAshokan You can see the example: https://github.com/appleboy/gofight/blob/master/example/gin_test.go
Most helpful comment
Hi @otraore @AlbinOS @widnyana @jcmartins
I write testing tool for gin framework. Please try it.
https://github.com/appleboy/gofight
gin_test.go
if you have any problem, please contact me.