Giter Club home page Giter Club logo

gin_unit_test's Introduction

gin_unit_test

A demo about the unit test of gin-gonic/gin

Installation

Make sure you have a working Go environment (Go 1.2 or higher is required). See the install instructions.

To install gin_unit_test, simply run:

go get github.com/Valiben/gin_unit_test

To compile it from source:

cd $GOPATH/src/github.com/Valiben/gin_unit_test
go get -u -v
go build && go test -v

Example

Here is a simple handler for login. Binding the parameters of the request to the User variable, and judge whether the password and username are right.

type User struct {
	Username string `form:"username" json:"username" binding:"required"`
	Password   string `form:"password" json:"password" binding:"required"`
	Age int `form:"age" json:"age" binding:"required"`
}
func LoginHandler(c *gin.Context) {
	req := &User{}
	if err := c.Bind(req); err != nil {
		log.Printf("err:%v", err)
		c.JSON(http.StatusOK, gin.H{
			"errno":  "1",
			"errmsg": "parameters not match",
		})
		return
	}

	// judge the password and username
	if req.UserName != "Valiben" || req.Password != "123456" {
		c.JSON(http.StatusOK, gin.H{
			"errno":  "2",
			"errmsg": "password or username is wrong",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"errno":  "0",
		"errmsg": "login success",
	})
}

You can write a unit test for this handler like the following.

Firstly, you should set up the router to handle the requests.

router := gin.Default()
router.POST("/login", LoginHandler)

Secondly, you should set the router of the utils so that you can use the utils to test the handler.

SetRouter(router)

Then you can write the unit test function.

func TestLoginHandler(t *testing.T) {
	resp := OrdinaryResponse{}
	
	err := utils.TestHandlerUnMarshalResp("POST", "/login", "form", user, &resp)
	if err != nil {
		t.Errorf("TestLoginHandler: %v\n", err)
		return
	}
	
	if resp.Errno != "0" {
		t.Errorf("TestLoginHandler: response is not expected\n")
		return
	}
}

Then you can run this test to check the handler.

You can find more tests and more specific information about how to use utils in the test/handlers_test.go.

gin_unit_test's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.