feat: add length equal

This commit is contained in:
debugtalk
2021-10-03 21:52:47 +08:00
parent 36b3e21880
commit 9685753c4c
4 changed files with 71 additions and 13 deletions

View File

@@ -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
View 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)
}
}

View File

@@ -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,
},

View File

@@ -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
}