From a221afb51d282f1e63ca495eaeacc578ea0b07bb Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 3 Oct 2021 21:52:47 +0800 Subject: [PATCH] feat: add length equal --- builtin/assertion.go | 11 ++++++++++- examples/function_test.go | 38 ++++++++++++++++++++++++++++++++++++++ examples/validate_test.go | 2 +- validate.go | 33 ++++++++++++++++++++++----------- 4 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 examples/function_test.go diff --git a/builtin/assertion.go b/builtin/assertion.go index 6634207f..99782626 100644 --- a/builtin/assertion.go +++ b/builtin/assertion.go @@ -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...) } diff --git a/examples/function_test.go b/examples/function_test.go new file mode 100644 index 00000000..81140e20 --- /dev/null +++ b/examples/function_test.go @@ -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) + } +} diff --git a/examples/validate_test.go b/examples/validate_test.go index 4f25c128..ab6bbf96 100644 --- a/examples/validate_test.go +++ b/examples/validate_test.go @@ -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, }, diff --git a/validate.go b/validate.go index 8bc1f922..4244edb7 100644 --- a/validate.go +++ b/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 +}