test

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2023 License: MIT Imports: 12 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"

	"jnsec.net/pkg/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

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(method, uri, contentType string, auth string, params string, vars map[string]interface{}) (c *gin.Context, r *http.Request, w *httptest.ResponseRecorder)

具体的每一个请求

func Exec

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

执行 测试

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
}

Jump to

Keyboard shortcuts

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