testWithExcel

package
v0.0.0-...-e2602fe Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AssertTypeNone           = "none"   //不使用通用的断言,处理方法中自行处理
	AssertTypeEQ             = "eq"     //值相等期望值
	AssertTypeEQWithValue    = "eq:"    //值为json,提取指定字段值判断是否等于期望值,eq:为前缀,eq:后面为json的pattern,如“a.b”,下面的同理
	AssertTypeNE             = "ne"     //值不相等期望值
	AssertTypeNEWithValue    = "ne:"    //值为json,提取指定字段值判断是否不等于期望值,ne:为前缀
	AssertTypeGT             = "gt"     //值大于期望值
	AssertTypeGTWithValue    = "gt:"    //值为json,提取指定字段值判断是否大于期望值,gt:为前缀
	AssertTypeGE             = "ge"     //值大于等于期望值
	AssertTypeGEWithValue    = "ge:"    //值为json,提取指定字段值判断是否大于等于期望值,ge:为前缀
	AssertTypeLT             = "lt"     //值小于期望值
	AssertTypeLTWithValue    = "lt:"    //值为json,提取指定字段值判断是否小于期望值,lt:为前缀
	AssertTypeLE             = "le"     //值小于等于期望值
	AssertTypeLEWithValue    = "le:"    //值为json,提取指定字段值判断是否小于等于期望值,le:为前缀
	AssertTypeIN             = "in"     //值属于期望值,期望值为json数组
	AssertTypeINWithValue    = "in:"    //值为json,提取指定字段值判断属于期望值,in:为前缀,期望值为json数组
	AssertTypeNI             = "ni"     //值不属于期望值,期望值为json数组
	AssertTypeNIWithValue    = "ni:"    //值为json,提取指定字段值判断不属于期望值,ni:为前缀,期望值为json数组
	AssertTypeCntEqWithValue = "cntEq:" //值为json,提取指定字段值判断长度等于期望值,cntEq:为前缀
	AssertTypeCntLtWithValue = "cntLt:" //值为json,提取指定字段值判断长度小于期望值,cntLt:为前缀
	AssertTypeCntLeWithValue = "cntLe:" //值为json,提取指定字段值判断长度等于等于期望值,cntLe:为前缀
	AssertTypeCntGtWithValue = "cntGt:" //值为json,提取指定字段值判断长度大于期望值,cntGt:为前缀
	AssertTypeCntGeWithValue = "cntGe:" //值为json,提取指定字段值判断长度大于等于期望值,cntGe:为前缀
)
View Source
const (
	CaseSheetName = "case"      //用例的sheet名
	LoginPrefix   = "l:"        //登录信息的前缀
	BodyPrefix    = "b:"        //body信息的前缀
	DescKey       = "desc"      //用例描述的key
	AssignVarKey  = "assignVar" //变量设置的key
)
View Source
const (
	VarRegPattern = `\$\{(\w+)\}`
)

Variables

View Source
var HttpCmd = &gcmd.Command{
	Name:        "genUnitTestExcel",
	Usage:       "./main genUnitTestExcel",
	Brief:       "Gen Unit Test Excel",
	Description: "注意:执行之前请确保openApi开启,且本地http服务已启动,且配置文件中指定了服务端口,脚本会根据配置文件自动获取openApi的json文件并生成单元测试用例的excel模版",
	Arguments: append(cmd.CommonArguments, []gcmd.Argument{
		{
			Name:   "output",
			Short:  "o",
			Brief:  "输出的目录,为空则输出到`test/unitExcel/`",
			IsArg:  false,
			Orphan: false,
		},
		{
			Name:   "include",
			Short:  "i",
			Brief:  "过滤条件,要包含的接口,格式为method|path,多个则逗号分割,如: get|/user/login,post|/user/create",
			IsArg:  false,
			Orphan: false,
		},
		{
			Name:   "expel",
			Short:  "e",
			Brief:  "过滤条件,要排出的接口,格式为method|path,多个则逗号分割,如: get|/user/login,post|/user/create",
			IsArg:  false,
			Orphan: false,
		},
	}...),
	Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {

		if gcfg.Instance().MustGet(ctx, "apollo") != nil {
			adapter, err := gcfg_apollo.CreateAdapterApollo(ctx, cmd.GetCommonArguments(ctx, parser, cmd.ApolloIP).String())
			if err != nil {
				panic(err)
			}
			gcfg.Instance().SetAdapter(adapter)
		}

		address := cmd.GetCommonArguments(ctx, parser, cmd.Port).String()
		if !strings.Contains(address, ":") {
			panic(fmt.Errorf("配置server.address格式错误: %s", address))
		}
		port := gstr.Split(g.Cfg().MustGet(ctx, "server.address").String(), ":")[1]
		if gconv.Int(port) == 0 {
			panic(fmt.Errorf("配置server.address中端口格式错误: %s", port))
		}

		openapiPath := g.Cfg().MustGet(ctx, "server.openapiPath").String()
		if openapiPath == "" {
			panic(fmt.Errorf("配置server.openapiPath未配置"))
		}

		output := parser.GetOpt("output").String()
		if output == "" {

			output = "test/unitExcel"
		}

		if !gfile.IsDir(output) {
			err = gfile.Mkdir(output)
			if err != nil {
				panic(err)
			}
		}

		//过滤条件
		var filters []*Filter
		include := strings.Trim(parser.GetOpt("include").String(), " ")
		if include != "" {
			for _, v := range strings.Split(include, ",") {
				if !strings.Contains(v, "|") {
					panic(fmt.Errorf("参数%s格式不正确", "include"))
				}
				arr := strings.Split(v, "|")
				filters = append(filters, &Filter{
					Path:   arr[1],
					Method: arr[0],
					Type:   FilterTypeInclude,
				})
			}
		}
		expel := strings.Trim(parser.GetOpt("expel").String(), " ")
		if expel != "" {
			for _, v := range strings.Split(expel, ",") {
				if !strings.Contains(v, "|") {
					panic(fmt.Errorf("参数%s格式不正确", "expel"))
				}
				arr := strings.Split(v, "|")
				filters = append(filters, &Filter{
					Path:   arr[1],
					Method: arr[0],
					Type:   FilterTypeExpel,
				})
			}
		}

		prefix := fmt.Sprintf("http://127.0.0.1:%s", port)
		client := g.Client()
		client.SetPrefix(prefix)

		jsonContent := client.GetContent(ctx, openapiPath)

		if err = GenTestCaseExcelByOpenApiJson(jsonContent, output, filters); err != nil {
			panic(err)
		}

		return
	},
}

Functions

func Assert

func Assert(caseName string, value, expect interface{})

Assert checks `value` and `expect` EQUAL.

func AssertByType

func AssertByType(type_ string, caseName string, value interface{}, expect string)

AssertByType checks `value` and `expect` by AssertType.

func AssertEQ

func AssertEQ(caseName string, value, expect interface{})

AssertEQ checks `value` and `expect` EQUAL, including their TYPES.

func AssertGE

func AssertGE(caseName string, value, expect interface{})

AssertGE checks `value` is GREATER OR EQUAL THAN `expect`. Notice that, only string, integer and float types can be compared by AssertGTE, others are invalid.

func AssertGT

func AssertGT(caseName string, value, expect interface{})

AssertGT checks `value` is GREATER THAN `expect`. Notice that, only string, integer and float types can be compared by AssertGT, others are invalid.

func AssertIN

func AssertIN(caseName string, value, expect interface{})

AssertIN checks `value` is IN `expect`. The `expect` should be a slice, but the `value` can be a slice or a basic type variable.

func AssertLE

func AssertLE(caseName string, value, expect interface{})

AssertLE checks `value` is LESS OR EQUAL THAN `expect`. Notice that, only string, integer and float types can be compared by AssertLTE, others are invalid.

func AssertLT

func AssertLT(caseName string, value, expect interface{})

AssertLT checks `value` is LESS EQUAL THAN `expect`. Notice that, only string, integer and float types can be compared by AssertLT, others are invalid.

func AssertNE

func AssertNE(caseName string, value, expect interface{})

AssertNE checks `value` and `expect` NOT EQUAL.

func AssertNI

func AssertNI(caseName string, value, expect interface{})

AssertNI checks `value` is NOT IN `expect`. The `expect` should be a slice, but the `value` can be a slice or a basic type variable.

func AssertNQ

func AssertNQ(caseName string, value, expect interface{})

AssertNQ checks `value` and `expect` NOT EQUAL, including their TYPES.

func GenTestCaseExcelByOpenApiJson

func GenTestCaseExcelByOpenApiJson(openApiJsonStr string, outputDir string, filter ...[]*Filter) error

GenTestCaseExcelByOpenApiJson 根据openApi的json文件生成接口测试用例excel

func GetVar

func GetVar(varStr string) string

func GetVarMap

func GetVarMap() map[string]string

func HandleVar

func HandleVar(m *map[string]string)

func IsVar

func IsVar(key string) bool

IsVar 检查给定的字符串是否符合变量的命名规则。

func ReplayVar

func ReplayVar(str string) string

func SetVar

func SetVar(key string, value string)

func SetVarByAssignVarPattern

func SetVarByAssignVarPattern(jsonStr string, assignVar string) error

SetVarByAssignVarPattern 根据指定的变量赋值模式,解析JSON字符串并设置变量。 jsonStr 是待解析的JSON字符串。 assignVar 是变量赋值模式,格式为"变量名1:JSON路径1,变量名2:JSON路径2"。 返回错误如果赋值模式不正确或JSON解析失败。

Types

type AfterCaseFunc

type AfterCaseFunc func(ctx context.Context, caseInfo CaseInfo, caseRet interface{}, isCasePass bool)

type AfterFunc

type AfterFunc func(ctx context.Context, prepareData PrepareData, caseData CaseData, failCase *CaseInfo)

type BeforeFunc

type BeforeFunc func(ctx context.Context, prepareData PrepareData)

type Case

type Case struct {
	Name       string            `json:"name"`
	IsOpen     string            `json:"isOpen"`
	AssertType string            `json:"assertType"`
	Expect     string            `json:"expect"`
	LoginAttrs []string          `json:"loginAttrs"`
	ReqAttrs   []string          `json:"reqAttrs"`
	ReqVals    map[string]string `json:"reqVals"`
	AssignVar  string            `json:"assignVar"`
	Desc       string            `json:"desc"`
	Method     string            `json:"method"`
	Path       string            `json:"path"`
}

type CaseData

type CaseData []CaseInfo

type CaseHandleFunc

type CaseHandleFunc func(ctx context.Context, t *testing.T, caseInfo CaseInfo) (ret interface{}, err error)

type CaseInfo

type CaseInfo struct {
	Cfg       *CaseInfoCfg      //配置信息
	Body      map[string]string //body信息
	Login     map[string]string //登录信息
	Extend    map[string]string //自定义扩展参数
	Desc      string            //描述信息
	AssignVar string            //变量设置
}

type CaseInfoCfg

type CaseInfoCfg struct {
	Name       string //用例名称
	IsOpen     bool   //是否开启
	AssertType string //用例断言类型
	Expect     string //用例期望结果
}

type Filter

type Filter struct {
	Path   string
	Method string
	Type   FilterType
}

type FilterType

type FilterType string
const (
	FilterTypeInclude FilterType = "include" //包含
	FilterTypeExpel   FilterType = "expel"   //排出
)

type OptionsFunc

type OptionsFunc func(o *defaultTestWithExcel)

func WithAfterCaseFunc

func WithAfterCaseFunc(f AfterCaseFunc) OptionsFunc

func WithAfterFunc

func WithAfterFunc(f AfterFunc) OptionsFunc

func WithBeforeFunc

func WithBeforeFunc(f BeforeFunc) OptionsFunc

func WithCaseHandleFunc

func WithCaseHandleFunc(f CaseHandleFunc) OptionsFunc

func WithFailfast

func WithFailfast(f bool) OptionsFunc

type Path

type Path struct {
	Path    string      `json:"path"`
	Method  string      `json:"method"`
	Summary string      `json:"summary"`
	Req     []*Property `json:"req"`
}

type PrepareData

type PrepareData map[string][]map[string]string

type Property

type Property struct {
	Name     string `json:"name"`
	Format   string `json:"format"`
	Desc     string `json:"desc"`
	Required bool   `json:"required"`
}

type TestWithExcel

type TestWithExcel interface {
	Run(ctx context.Context)
}

func New

func New(t *testing.T, testDataFile string, funcs ...OptionsFunc) (ret TestWithExcel, err error)

Jump to

Keyboard shortcuts

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