mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 11:29:48 +08:00
#29: refactor validator, now support custom defined validators
This commit is contained in:
@@ -137,8 +137,13 @@ class ResponseObject(object):
|
||||
{
|
||||
"resp_body_success": True
|
||||
}
|
||||
@return validator info
|
||||
check_item, check_value, expect_value, comparator
|
||||
@return (dict) validator info
|
||||
{
|
||||
"check_item": check_item,
|
||||
"check_value": check_value,
|
||||
"expect_value": expect_value,
|
||||
"comparator": comparator
|
||||
}
|
||||
"""
|
||||
if not isinstance(validator, dict):
|
||||
raise exception.ParamsError("invalid validator: {}".format(validator))
|
||||
@@ -177,19 +182,46 @@ class ResponseObject(object):
|
||||
except exception.ParseResponseError:
|
||||
raise exception.ParseResponseError("failed to extract check item in response!")
|
||||
|
||||
return check_item, check_value, expect_value, comparator
|
||||
validator_dict = {
|
||||
"check_item": check_item,
|
||||
"check_value": check_value,
|
||||
"expect_value": expect_value,
|
||||
"comparator": comparator
|
||||
}
|
||||
return validator_dict
|
||||
|
||||
def validate(self, validators, variables_mapping):
|
||||
def do_validation(self, validator_dict, functions_mapping):
|
||||
""" validate with functions
|
||||
"""
|
||||
comparator = utils.get_uniform_comparator(validator_dict["comparator"])
|
||||
|
||||
validate_func = functions_mapping.get(comparator)
|
||||
if not validate_func:
|
||||
raise exception.FunctionNotFound("comparator not found: {}".format(comparator))
|
||||
|
||||
check_item = validator_dict["check_item"]
|
||||
check_value = validator_dict["check_value"]
|
||||
expect_value = validator_dict["expect_value"]
|
||||
|
||||
try:
|
||||
if check_value is None or expect_value is None:
|
||||
assert comparator in ["is", "eq", "equals", "=="]
|
||||
|
||||
validate_func(validator_dict["check_value"], validator_dict["expect_value"])
|
||||
except (AssertionError, TypeError):
|
||||
err_msg = "\n" + "\n".join([
|
||||
"\tcheck item name: %s;" % check_item,
|
||||
"\tcheck item value: %s (%s);" % (check_value, type(check_value).__name__),
|
||||
"\tcomparator: %s;" % comparator,
|
||||
"\texpected value: %s (%s)." % (expect_value, type(expect_value).__name__)
|
||||
])
|
||||
raise exception.ValidationError(err_msg)
|
||||
|
||||
def validate(self, validators, variables_mapping, functions_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(
|
||||
check_value,
|
||||
expect_value,
|
||||
comparator,
|
||||
check_item
|
||||
)
|
||||
validator_dict = self.parse_validator(validator, variables_mapping)
|
||||
self.do_validation(validator_dict, functions_mapping)
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user