fix #255: validate with custom functions

This commit is contained in:
debugtalk
2018-06-11 21:52:21 +08:00
parent 6539640fc5
commit 696a393776
4 changed files with 22 additions and 9 deletions

View File

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

View File

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

View File

@@ -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():

View File

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