diff --git a/httprunner/__about__.py b/httprunner/__about__.py index cfcc2fe6..ae75cc5f 100644 --- a/httprunner/__about__.py +++ b/httprunner/__about__.py @@ -1,7 +1,7 @@ __title__ = 'HttpRunner' __description__ = 'One-stop solution for HTTP(S) testing.' __url__ = 'https://github.com/HttpRunner/HttpRunner' -__version__ = '1.4.6' +__version__ = '1.4.7' __author__ = 'debugtalk' __author_email__ = 'mail@debugtalk.com' __license__ = 'MIT' diff --git a/httprunner/context.py b/httprunner/context.py index 9f24e83d..9059242c 100644 --- a/httprunner/context.py +++ b/httprunner/context.py @@ -188,17 +188,21 @@ class Context(object): } """ check_item = validator["check"] - # check_item should only be in 4 types: + # check_item should only be the following 5 formats: # 1, variable reference, e.g. $token - # 2, string joined by delimiter. e.g. "status_code", "headers.content-type" - # 3, regex string, e.g. "LB[\d]*(.*)RB[\d]*" - # 4, dict or list, maybe containing variables reference, e.g. {"var": "$abc"} - if isinstance(check_item, (dict, list)) or testcase.extract_variables(check_item): - # type 4 or type 1 + # 2, function reference, e.g. ${is_status_code_200($status_code)} + # 3, dict or list, maybe containing variable/function reference, e.g. {"var": "$abc"} + # 4, string joined by delimiter. e.g. "status_code", "headers.content-type" + # 5, regex string, e.g. "LB[\d]*(.*)RB[\d]*" + + if isinstance(check_item, (dict, list)) \ + or testcase.extract_variables(check_item) \ + or testcase.extract_functions(check_item): + # format 1/2/3 check_value = self.eval_content(check_item) else: try: - # type 2 or type 3 + # format 4/5 check_value = resp_obj.extract_field(check_item) except exception.ParseResponseError: msg = "failed to extract check item from response!\n" diff --git a/tests/debugtalk.py b/tests/debugtalk.py index a88684fb..5cd762ce 100644 --- a/tests/debugtalk.py +++ b/tests/debugtalk.py @@ -38,6 +38,9 @@ def sum_status_code(status_code, expect_sum): assert sum_value == expect_sum +def is_status_code_200(status_code): + return status_code == 200 + os.environ["TEST_ENV"] = "PRODUCTION" def skip_test_in_production_env(): diff --git a/tests/test_context.py b/tests/test_context.py index d9beb998..647390bc 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -262,7 +262,8 @@ class VariableBindsUnittest(ApiServerUnittest): validators = [ {"eq": ["$resp_status_code", 201]}, {"check": "$resp_status_code", "comparator": "eq", "expect": 201}, - {"check": "$resp_body_success", "comparator": "eq", "expect": True} + {"check": "$resp_body_success", "comparator": "eq", "expect": True}, + {"check": "${is_status_code_200($resp_status_code)}", "comparator": "eq", "expect": False} ] variables = [ {"resp_status_code": 200}, @@ -278,6 +279,11 @@ class VariableBindsUnittest(ApiServerUnittest): {"resp_body_success": True} ] self.context.bind_variables(variables) + from tests.debugtalk import is_status_code_200 + functions = { + "is_status_code_200": is_status_code_200 + } + self.context.bind_functions(functions) self.assertTrue(self.context.validate(validators, resp_obj))