From 1ffe851727e02b551bde3a932a2d31c1c9b8ca1e Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 21 Apr 2020 14:45:24 +0800 Subject: [PATCH] feat: validate with functions --- .../validate_with_functions.yml | 60 +++++++++++++++++++ .../validate_with_functions_test.py | 53 ++++++++++++++++ httprunner/v3/response.py | 12 ++-- httprunner/v3/runner.py | 2 +- 4 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 examples/postman_echo/request_methods/validate_with_functions.yml create mode 100644 examples/postman_echo/request_methods/validate_with_functions_test.py diff --git a/examples/postman_echo/request_methods/validate_with_functions.yml b/examples/postman_echo/request_methods/validate_with_functions.yml new file mode 100644 index 00000000..ea2c6a40 --- /dev/null +++ b/examples/postman_echo/request_methods/validate_with_functions.yml @@ -0,0 +1,60 @@ +config: + name: "request methods testcase: validate with functions" + variables: + foo1: session_bar1 + base_url: "https://postman-echo.com" + verify: False + +teststeps: +- + name: get with params + variables: + foo1: bar1 + foo2: session_bar2 + sum_v: "${sum_two(1, 2)}" + request: + method: GET + url: /get + params: + foo1: $foo1 + foo2: $foo2 + sum_v: $sum_v + headers: + User-Agent: HttpRunner/${get_httprunner_version()} + extract: + session_foo2: "body.args.foo2" + validate: + - eq: ["status_code", 200] + - eq: ["body.args.sum_v", "3"] + - less_than: ["body.args.sum_v", "${sum_two(2, 2)}"] +- + name: post raw text + variables: + foo1: "hello world" + foo3: "$session_foo2" + request: + method: POST + url: /post + headers: + User-Agent: HttpRunner/${get_httprunner_version()} + Content-Type: "text/plain" + data: "This is expected to be sent back as part of response body: $foo1-$foo3." + validate: + - eq: ["status_code", 200] + - eq: ["body.data", "This is expected to be sent back as part of response body: session_bar1-session_bar2."] +- + name: post form data + variables: + foo1: bar1 + foo2: bar2 + request: + method: POST + url: /post + headers: + User-Agent: HttpRunner/${get_httprunner_version()} + Content-Type: "application/x-www-form-urlencoded" + data: "foo1=$foo1&foo2=$foo2" + validate: + - eq: ["status_code", 200] + - eq: ["body.form.foo1", "session_bar1"] + - eq: ["body.form.foo2", "bar2"] diff --git a/examples/postman_echo/request_methods/validate_with_functions_test.py b/examples/postman_echo/request_methods/validate_with_functions_test.py new file mode 100644 index 00000000..8f600dfb --- /dev/null +++ b/examples/postman_echo/request_methods/validate_with_functions_test.py @@ -0,0 +1,53 @@ +from httprunner.v3.runner import TestCaseRunner +from httprunner.v3.schema import TestsConfig, TestStep +from examples.postman_echo import debugtalk + + +class TestCaseRequestMethodsValidateWithFunctions(TestCaseRunner): + config = TestsConfig(**{ + "name": "request methods testcase: validate with functions", + "variables": { + "foo1": "session_bar1" + }, + "functions": { + "get_httprunner_version": debugtalk.get_httprunner_version, + "sum_two": debugtalk.sum_two + }, + "base_url": "https://postman-echo.com", + "verify": False + }) + + teststeps = [ + TestStep(**{ + "name": "get with params", + "variables": { + "foo1": "bar1", + "foo2": "session_bar2", + "sum_v": "${sum_two(1, 2)}" + }, + "request": { + "method": "GET", + "url": "/get", + "params": { + "foo1": "$foo1", + "foo2": "$foo2", + "sum_v": "$sum_v" + }, + "headers": { + "User-Agent": "HttpRunner/${get_httprunner_version()}" + } + }, + "extract": { + "session_foo2": "body.args.foo2" + }, + "validate": [ + {"eq": ["status_code", 200]}, + {"eq": ["body.args.sum_v", 3]}, + {"less_than": ["body.args.sum_v", "${sum_two(2, 2)}"]} + ] + }) + ] + + +if __name__ == '__main__': + TestCaseRequestMethodsValidateWithFunctions().run() diff --git a/httprunner/v3/response.py b/httprunner/v3/response.py index 222e63b7..8523a688 100644 --- a/httprunner/v3/response.py +++ b/httprunner/v3/response.py @@ -5,8 +5,8 @@ import requests from loguru import logger from httprunner.v3.exceptions import ParamsError, ValidationFailure -from httprunner.v3.parser import parse_data -from httprunner.v3.schema import VariablesMapping, Validators +from httprunner.v3.parser import parse_data, parse_string_value +from httprunner.v3.schema import VariablesMapping, Validators, FunctionsMapping from httprunner.v3.validator import uniform_validator, AssertMethods @@ -25,7 +25,10 @@ class ResponseObject(object): "body": resp_obj.json() } - def validate(self, validators: Validators, variables_mapping: VariablesMapping = None) -> NoReturn: + def validate(self, + validators: Validators, + variables_mapping: VariablesMapping = None, + functions_mapping: FunctionsMapping = None) -> NoReturn: for v in validators: u_validator = uniform_validator(v) @@ -41,8 +44,9 @@ class ResponseObject(object): except AttributeError: raise ParamsError(f"Assert Method not supported: {assert_method}") + actual_value = parse_string_value(actual_value) # parse expected value with config/teststep/extracted variables - expect_value = parse_data(expect_value, variables_mapping) + expect_value = parse_data(expect_value, variables_mapping, functions_mapping) try: assert_func(actual_value, expect_value) diff --git a/httprunner/v3/runner.py b/httprunner/v3/runner.py index 0048de7e..53b27d11 100644 --- a/httprunner/v3/runner.py +++ b/httprunner/v3/runner.py @@ -53,7 +53,7 @@ class TestCaseRunner(object): # validate validators = step.validators - resp_obj.validate(validators, variables_mapping) + resp_obj.validate(validators, variables_mapping, self.config.functions) return extract_mapping