From be6502cd63e5508037bb14d86233856c679e2909 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 12 Dec 2017 12:08:55 +0800 Subject: [PATCH] support new validator format, e.g. {'eq': ['status_code', 200]} --- httprunner/__init__.py | 2 +- httprunner/response.py | 81 +++++++++++++++++---------- tests/data/demo_testset_hardcode.json | 6 ++ tests/data/demo_testset_hardcode.yml | 6 ++ 4 files changed, 63 insertions(+), 32 deletions(-) diff --git a/httprunner/__init__.py b/httprunner/__init__.py index 78bbd491..d2825abd 100644 --- a/httprunner/__init__.py +++ b/httprunner/__init__.py @@ -1 +1 @@ -__version__ = '0.8.2' \ No newline at end of file +__version__ = '0.8.3' \ No newline at end of file diff --git a/httprunner/response.py b/httprunner/response.py index b59feae6..84beaca9 100644 --- a/httprunner/response.py +++ b/httprunner/response.py @@ -124,50 +124,69 @@ class ResponseObject(object): return extracted_variables_mapping - def validate(self, validators, variables_mapping): - """ Bind named validators to value within the context. - @param (list) validators - [ - {"check": "status_code", "comparator": "eq", "expect": 201}, + def parse_validator(self, validator, variables_mapping): + """ parse validator, validator maybe in two format + @param (dict) validator + format1: this is kept for compatiblity with the previous versions. + {"check": "status_code", "comparator": "eq", "expect": 201} {"check": "resp_body_success", "comparator": "eq", "expect": True} - ] + format2: recommended new version + {'eq': ['status_code', 201]} + {'eq': ['resp_body_success', True]} @param (dict) variables_mapping { "resp_body_success": True } - @return (list) content differences - [ - { - "check": "status_code", - "comparator": "eq", "expect": 201, "value": 200 - } - ] + @return validator info + check_item, check_value, expect_value, comparator """ - for validator_dict in validators: + if not isinstance(validator, dict): + raise exception.ParamsError("invalid validator: {}".format(validator)) - check_item = validator_dict.get("check") - if not check_item: - raise exception.ParamsError("check item invalid: {}".format(check_item)) + if "check" in validator and len(validator) > 1: + # format1 + check_item = validator.get("check") - if "expect" in validator_dict: - expect_value = validator_dict.get("expect") - elif "expected" in validator_dict: - expect_value = validator_dict.get("expected") + if "expect" in validator: + expect_value = validator.get("expect") + elif "expected" in validator: + expect_value = validator.get("expected") else: - raise exception.ParamsError("expected value missed in testcase validator!") + raise exception.ParamsError("invalid validator: {}".format(validator)) - comparator = validator_dict.get("comparator", "eq") + comparator = validator.get("comparator", "eq") - if check_item in variables_mapping: - validator_dict["actual_value"] = variables_mapping[check_item] - else: - try: - validator_dict["actual_value"] = self.extract_field(check_item) - except exception.ParseResponseError: - raise exception.ParseResponseError("failed to extract check item in response!") + elif len(validator) == 1: + # format2 + comparator = list(validator.keys())[0] + compare_values = validator[comparator] + + if not isinstance(compare_values, list) or len(compare_values) != 2: + raise exception.ParamsError("invalid validator: {}".format(validator)) + + check_item, expect_value = compare_values + + else: + raise exception.ParamsError("invalid validator: {}".format(validator)) + + if check_item in variables_mapping: + check_value = variables_mapping[check_item] + else: + try: + check_value = self.extract_field(check_item) + except exception.ParseResponseError: + raise exception.ParseResponseError("failed to extract check item in response!") + + return check_item, check_value, expect_value, comparator + + def validate(self, validators, variables_mapping): + """ check validators with the context variable mapping. + """ + for validator in validators: + check_item, check_value, expect_value, comparator = self.parse_validator(validator, variables_mapping) utils.match_expected( - validator_dict["actual_value"], + check_value, expect_value, comparator, check_item diff --git a/tests/data/demo_testset_hardcode.json b/tests/data/demo_testset_hardcode.json index df7b220c..d0ae92b7 100644 --- a/tests/data/demo_testset_hardcode.json +++ b/tests/data/demo_testset_hardcode.json @@ -22,6 +22,8 @@ } ], "validate": [ + {"eq": ["status_code", 200]}, + {"len_eq": ["content.token", 16]}, {"check": "status_code", "comparator": "eq", "expect": 200}, {"check": "content.token", "comparator": "len_eq", "expect": 16} ] @@ -44,6 +46,8 @@ } }, "validate": [ + {"eq": ["status_code", 201]}, + {"eq": ["content.success", true]}, {"check": "status_code", "comparator": "eq", "expect": 201}, {"check": "content.success", "comparator": "eq", "expect": true} ] @@ -66,6 +70,8 @@ } }, "validate": [ + {"eq": ["status_code", 500]}, + {"eq": ["content.success", false]}, {"check": "status_code", "comparator": "eq", "expect": 500}, {"check": "content.success", "comparator": "eq", "expect": false} ] diff --git a/tests/data/demo_testset_hardcode.yml b/tests/data/demo_testset_hardcode.yml index fd869cb9..c22627ac 100644 --- a/tests/data/demo_testset_hardcode.yml +++ b/tests/data/demo_testset_hardcode.yml @@ -14,6 +14,8 @@ extract: - token: content.token validate: + - eq: ["status_code", 200] + - len_eq: ["content.token", 16] - {"check": "status_code", "comparator": "eq", "expect": 200} - {"check": "content.token", "comparator": "len_eq", "expect": 16} @@ -30,6 +32,8 @@ name: "user1" password: "123456" validate: + - eq: ["status_code", 201] + - eq: ["content.success", True] - {"check": "status_code", "comparator": "eq", "expect": 201} - {"check": "content.success", "comparator": "eq", "expect": true} @@ -46,5 +50,7 @@ name: "user1" password: "123456" validate: + - "eq": ["status_code", 500] + - "eq": ["content.success", false] - {"check": "status_code", "comparator": "eq", "expect": 500} - {"check": "content.success", "comparator": "eq", "expect": false} \ No newline at end of file