mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-12 16:01:27 +08:00
fix: contains assertion bug; feat: add type_match, contained_by, string_equals assertion methods
Change-Id: Icb15fe33a58d1ff69991435bbe70c3cd53bb8dea
This commit is contained in:
@@ -19,14 +19,18 @@ func TestCaseCallFunction(t *testing.T) {
|
|||||||
TestSteps: []hrp.IStep{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("get with params").
|
hrp.NewStep("get with params").
|
||||||
GET("/get").
|
GET("/get").
|
||||||
WithParams(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}"}).
|
WithParams(map[string]interface{}{"foo1": "${gen_random_string($n)}", "foo2": "${max($a, $b)}", "foo3": "Foo3"}).
|
||||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||||
Extract().
|
Extract().
|
||||||
WithJmesPath("body.args.foo1", "varFoo1").
|
WithJmesPath("body.args.foo1", "varFoo1").
|
||||||
Validate().
|
Validate().
|
||||||
AssertEqual("status_code", 200, "check status code").
|
AssertEqual("status_code", 200, "check status code").
|
||||||
AssertLengthEqual("body.args.foo1", 5, "check args foo1").
|
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
|
AssertEqual("body.args.foo2", "12.3", "check args foo2").
|
||||||
|
AssertTypeMatch("body.args.foo3", "str", "check args foo3 is type string").
|
||||||
|
AssertStringEqual("body.args.foo3", "foo3", "check args foo3 case-insensitivity").
|
||||||
|
AssertContains("body.args.foo3", "Foo", "check contains ").
|
||||||
|
AssertContainedBy("body.args.foo3", "this is Foo3 test", "check contained by"), // notice: request params value will be converted to string
|
||||||
hrp.NewStep("post json data with functions").
|
hrp.NewStep("post json data with functions").
|
||||||
POST("/post").
|
POST("/post").
|
||||||
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
WithHeaders(map[string]string{"User-Agent": "HttpRunnerPlus"}).
|
||||||
|
|||||||
@@ -15,13 +15,16 @@ var Assertions = map[string]func(t assert.TestingT, expected interface{}, actual
|
|||||||
"greater_or_equals": assert.GreaterOrEqual,
|
"greater_or_equals": assert.GreaterOrEqual,
|
||||||
"less_or_equals": assert.LessOrEqual,
|
"less_or_equals": assert.LessOrEqual,
|
||||||
"not_equal": assert.NotEqual,
|
"not_equal": assert.NotEqual,
|
||||||
"contains": assert.Contains,
|
"contained_by": assert.Contains,
|
||||||
"regex_match": assert.Regexp,
|
"regex_match": assert.Regexp,
|
||||||
|
"type_match": assert.IsType,
|
||||||
// custom assertions
|
// custom assertions
|
||||||
"startswith": StartsWith, // check if string starts with substring
|
"startswith": StartsWith, // check if string starts with substring
|
||||||
"endswith": EndsWith, // check if string ends with substring
|
"endswith": EndsWith, // check if string ends with substring
|
||||||
"length_equals": EqualLength,
|
"length_equals": EqualLength,
|
||||||
"length_equal": EqualLength, // alias for length_equals
|
"length_equal": EqualLength, // alias for length_equals
|
||||||
|
"contains": Contains,
|
||||||
|
"string_equals": EqualString,
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartsWith(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
func StartsWith(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||||
@@ -83,3 +86,20 @@ func convertInt(value interface{}) (int, error) {
|
|||||||
return 0, fmt.Errorf("unsupported int convertion for %v(%T)", v, v)
|
return 0, fmt.Errorf("unsupported int convertion for %v(%T)", v, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Contains assert whether actual
|
||||||
|
func Contains(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||||
|
return assert.Contains(t, actual, expected, msgAndArgs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EqualString(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||||
|
if !assert.IsType(t, "string", actual, msgAndArgs) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !assert.IsType(t, "string", expected, msgAndArgs) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
actualString := actual.(string)
|
||||||
|
expectedString := expected.(string)
|
||||||
|
return assert.True(t, strings.EqualFold(actualString, expectedString), msgAndArgs)
|
||||||
|
}
|
||||||
|
|||||||
44
validate.go
44
validate.go
@@ -35,6 +35,28 @@ func (s *StepRequestValidation) AssertEqual(jmesPath string, expected interface{
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *StepRequestValidation) AssertContains(jmesPath string, expected interface{}, msg string) *StepRequestValidation {
|
||||||
|
v := Validator{
|
||||||
|
Check: jmesPath,
|
||||||
|
Assert: "contains",
|
||||||
|
Expect: expected,
|
||||||
|
Message: msg,
|
||||||
|
}
|
||||||
|
s.step.Validators = append(s.step.Validators, v)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StepRequestValidation) AssertTypeMatch(jmesPath string, expected interface{}, msg string) *StepRequestValidation {
|
||||||
|
v := Validator{
|
||||||
|
Check: jmesPath,
|
||||||
|
Assert: "type_match",
|
||||||
|
Expect: expected,
|
||||||
|
Message: msg,
|
||||||
|
}
|
||||||
|
s.step.Validators = append(s.step.Validators, v)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
func (s *StepRequestValidation) AssertStartsWith(jmesPath string, expected interface{}, msg string) *StepRequestValidation {
|
func (s *StepRequestValidation) AssertStartsWith(jmesPath string, expected interface{}, msg string) *StepRequestValidation {
|
||||||
v := Validator{
|
v := Validator{
|
||||||
Check: jmesPath,
|
Check: jmesPath,
|
||||||
@@ -67,3 +89,25 @@ func (s *StepRequestValidation) AssertLengthEqual(jmesPath string, expected inte
|
|||||||
s.step.Validators = append(s.step.Validators, v)
|
s.step.Validators = append(s.step.Validators, v)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *StepRequestValidation) AssertContainedBy(jmesPath string, expected interface{}, msg string) *StepRequestValidation {
|
||||||
|
v := Validator{
|
||||||
|
Check: jmesPath,
|
||||||
|
Assert: "contained_by",
|
||||||
|
Expect: expected,
|
||||||
|
Message: msg,
|
||||||
|
}
|
||||||
|
s.step.Validators = append(s.step.Validators, v)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StepRequestValidation) AssertStringEqual(jmesPath string, expected interface{}, msg string) *StepRequestValidation {
|
||||||
|
v := Validator{
|
||||||
|
Check: jmesPath,
|
||||||
|
Assert: "string_equals",
|
||||||
|
Expect: expected,
|
||||||
|
Message: msg,
|
||||||
|
}
|
||||||
|
s.step.Validators = append(s.step.Validators, v)
|
||||||
|
return s
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user