test

package module
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 17 Imported by: 0

README

Test

The fantastic TEST framework for Gin, aims to be developer friendly.

Overview

  • Full-Featured testing
  • Every feature comes with tests
  • Developer Friendly

Getting Started

Add folder t to your project root.

Use init.go to add initialization actions just like in main.go

package t

import (
	"io"

	"github.com/gin-gonic/gin"
)

func testInit() (*gin.Engine, error) {
	gin.DefaultWriter = io.Discard

	route := routers.InitRouter()
	return route, nil
}

Add new TestCase file, such as dummy_test.go

package t

import (
	"testing"

	"gitee.com/wx-rdc/go-test"
)

func TestDummy(t *testing.T) {
	var caseList = []test.TestCase{
		{
			Method:      "GET",
			Url:         "/api/v1/kms/rsa/generateKeyPair",
			ContentType: "application/x-www-form-urlencoded",
			Param:       "",
			Code:        200,
			Desc:        "生成RSA密钥对",
			ShowBody:    true,
			ErrMsg:      "",
			Ext1:        nil,
			Ext2:        nil,
		},
	}
	
	test.RegisterInitCallback(testInit)
	test.Exec(t, caseList)
}

Now, you can run test like usual

go test -v ./t

Authorization

This framework support Bear TOKEN authorization header, add token to cast

{
	Method:      "POST",
	Url:         "/auth",
	ContentType: "application/json",
	Param:       `{"username": "admin", "password": "123456"}`,
	Code:        200,
	Desc:        "登录",
	ShowBody:    true,
	ErrMsg:      "",
	Ext1:        nil,
	Ext2:        nil,
},
{
	Method:        "GET",
	Url:           "/api/v1/deviceList",
	ContentType:   "application/json",
	Authorization: `$token`,
	Param:         "",
	Code:          200,
	Desc:          "获取设备列表",
	ShowBody:      true,
	ErrMsg:        "",
	Ext1:          nil,
	Ext2:          nil,
}

License

© Edward.Zhu, 2023~time.Now

Released under the MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoRequest

func DoRequest(ts *TestCase, vars map[string]interface{}) (c *gin.Context, r *http.Request, w *httptest.ResponseRecorder)

具体的每一个请求

func Exec

func Exec(t *testing.T, cs []TestCase)

执行 测试

func ExecWithVars added in v0.1.3

func ExecWithVars(t *testing.T, cs []TestCase, vars map[string]interface{})

执行 测试

func MakeGetRequest

func MakeGetRequest(uri string, param url.Values) (r *http.Request)

func MakePostJSONRequest

func MakePostJSONRequest(uri string, data map[string]interface{}) (r *http.Request)

func MakePostRequest

func MakePostRequest(uri string, param url.Values) (r *http.Request)

func MergeMap

func MergeMap(mObj ...map[string]interface{}) map[string]interface{}

func NewBufferString

func NewBufferString(body string) io.Reader

func RegisterInitCallback

func RegisterInitCallback(cb InitCallback)

Types

type InitCallback

type InitCallback func() (*gin.Engine, error)

type TestCallback

type TestCallback struct {
	Init InitCallback
}

type TestCase

type TestCase struct {
	testing.InternalTest
	Method        string //请求类型
	Url           string //链接
	ContentType   string //
	Authorization string
	Param         string      //参数
	Code          int         //状态码
	Desc          string      //描述
	ShowBody      bool        //是否展示返回
	ErrMsg        string      //错误信息
	Ext1          interface{} //自定义1
	Ext2          interface{} //自定义2
}

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL