Skip to content
Myr

Added simple testing documentation and examples (#1156)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Andrii Bubis · committed by Bo-Yi Wu · parent 9a4ecc8 · Browse files · 3 files+72 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+72 added−1 removed

  • other:+72 added−1 removed

3 files+72 added−1 removed

added:examples/basic/main_test.go+20 added
1package main23import (4 "net/http"5 "net/http/httptest"6 "testing"78 "github.com/stretchr/testify/assert"9)1011func TestPingRoute(t *testing.T) {12 router := setupRouter()1314 w := httptest.NewRecorder()15 req, _ := http.NewRequest("GET", "/ping", nil)16 router.ServeHTTP(w, req)1718 assert.Equal(t, 200, w.Code)19 assert.Equal(t, "pong", w.Body.String())20}
modified:examples/basic/main.go+6 added-1 removed
4 unmodified lines
5)67var DB = make(map[string]string)89func main() {9func setupRouter() *gin.Engine {10 // Disable Console Color11 // gin.DisableConsoleColor()12 r := gin.Default()13
38 unmodified lines
52 c.JSON(200, gin.H{"status": "ok"})53 }54 })5556 return r57}5859func main() {60 r := setupRouter()61 // Listen and Server in 0.0.0.0:808062 r.Run(":8080")63}
modified:README.md+46 added
1342 unmodified lines
1343 log.Println("Server exiting")1344}1345```13461347## Testing13481349The `net/http/httptest` package is preferable way for HTTP testing.13501351```go1352package main13531354func setupRouter() *gin.Engine {1355 r := gin.Default()1356 r.GET("/ping", func(c *gin.Context) {1357 c.String(200, "pong")1358 })1359 return r1360}13611362func main() {1363 r := setupRouter()1364 r.Run(":8080")1365}1366```13671368Test for code example above:13691370```go1371package main13721373import (1374 "net/http"1375 "net/http/httptest"1376 "testing"13771378 "github.com/stretchr/testify/assert"1379)13801381func TestPingRoute(t *testing.T) {1382 router := setupRouter()13831384 w := httptest.NewRecorder()1385 req, _ := http.NewRequest("GET", "/ping", nil)1386 router.ServeHTTP(w, req)13871388 assert.Equal(t, 200, w.Code)1389 assert.Equal(t, "pong", w.Body.String())1390}1391```13921393## Users [![Sourcegraph](https://sourcegraph.com/github.com/gin-gonic/gin/-/badge.svg)](https://sourcegraph.com/github.com/gin-gonic/gin?badge)13941395Awesome project lists using [Gin](https://github.com/gin-gonic/gin) web framework.1396
2 unmodified lines