mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 11:29:48 +08:00
feat: add length equal
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package builtin
|
||||
|
||||
import "github.com/stretchr/testify/assert"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var Assertions = map[string]func(t assert.TestingT, expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool{
|
||||
"equals": assert.EqualValues,
|
||||
@@ -12,4 +14,11 @@ var Assertions = map[string]func(t assert.TestingT, expected interface{}, actual
|
||||
"not_equal": assert.NotEqual,
|
||||
"contains": assert.Contains,
|
||||
"regex_match": assert.Regexp,
|
||||
// custom assertions
|
||||
"length_equals": EqualLength,
|
||||
"length_equal": EqualLength, // alias for length_equals
|
||||
}
|
||||
|
||||
func EqualLength(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||
return assert.Len(t, actual, expected.(int), msgAndArgs...)
|
||||
}
|
||||
|
||||
38
examples/function_test.go
Normal file
38
examples/function_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package examples
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/httprunner/httpboomer"
|
||||
)
|
||||
|
||||
func TestCaseCallFunction(t *testing.T) {
|
||||
testcase := &httpboomer.TestCase{
|
||||
Config: httpboomer.TConfig{
|
||||
Name: "run request with functions",
|
||||
BaseURL: "https://postman-echo.com",
|
||||
Verify: false,
|
||||
},
|
||||
TestSteps: []httpboomer.IStep{
|
||||
httpboomer.Step("get with params").
|
||||
WithVariables(map[string]interface{}{
|
||||
"n": 5,
|
||||
"a": 12.3,
|
||||
"b": 3.45,
|
||||
}).
|
||||
GET("/get").
|
||||
WithParams(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}).
|
||||
WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}).
|
||||
Extract().
|
||||
WithJmesPath("body.args.foo1", "varFoo1").
|
||||
Validate().
|
||||
AssertLengthEqual("body.args.foo1", 5, "check args foo1").
|
||||
AssertEqual("body.args.foo2", "12.3", "check args foo2"), // notice: request params value will be converted to string
|
||||
},
|
||||
}
|
||||
|
||||
err := httpboomer.Test(t, testcase)
|
||||
if err != nil {
|
||||
t.Fatalf("run testcase error: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
func TestCaseValidateStep(t *testing.T) {
|
||||
testcase := &httpboomer.TestCase{
|
||||
Config: httpboomer.TConfig{
|
||||
Name: "run request with variables",
|
||||
Name: "run request with validation",
|
||||
BaseURL: "https://postman-echo.com",
|
||||
Verify: false,
|
||||
},
|
||||
|
||||
33
validate.go
33
validate.go
@@ -9,17 +9,6 @@ type stepRequestValidation struct {
|
||||
step *TStep
|
||||
}
|
||||
|
||||
func (s *stepRequestValidation) AssertEqual(jmesPath string, expected interface{}, msg string) *stepRequestValidation {
|
||||
validator := TValidator{
|
||||
Check: jmesPath,
|
||||
Assert: "equals",
|
||||
Expect: expected,
|
||||
Message: msg,
|
||||
}
|
||||
s.step.Validators = append(s.step.Validators, validator)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *stepRequestValidation) Name() string {
|
||||
return s.step.Name
|
||||
}
|
||||
@@ -31,3 +20,25 @@ func (s *stepRequestValidation) Type() string {
|
||||
func (s *stepRequestValidation) ToStruct() *TStep {
|
||||
return s.step
|
||||
}
|
||||
|
||||
func (s *stepRequestValidation) AssertEqual(jmesPath string, expected interface{}, msg string) *stepRequestValidation {
|
||||
validator := TValidator{
|
||||
Check: jmesPath,
|
||||
Assert: "equals",
|
||||
Expect: expected,
|
||||
Message: msg,
|
||||
}
|
||||
s.step.Validators = append(s.step.Validators, validator)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *stepRequestValidation) AssertLengthEqual(jmesPath string, expected interface{}, msg string) *stepRequestValidation {
|
||||
validator := TValidator{
|
||||
Check: jmesPath,
|
||||
Assert: "length_equals",
|
||||
Expect: expected,
|
||||
Message: msg,
|
||||
}
|
||||
s.step.Validators = append(s.step.Validators, validator)
|
||||
return s
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user