#29: refactor validator:

1, relocate validate functions;
2, add unittest for custom defined validators.
This commit is contained in:
httprunner
2017-12-12 23:59:54 +08:00
parent 0e1ef7f78f
commit 6ac43e39ea
9 changed files with 139 additions and 117 deletions

View File

@@ -4,7 +4,7 @@ import re
import sys
from collections import OrderedDict
from httprunner import utils
from httprunner import exception, utils
from httprunner.testcase import TestcaseParser
@@ -165,10 +165,47 @@ class Context(object):
def get_testcase_variables_mapping(self):
return self.testcase_variables_mapping
def get_testcase_functions_mapping(self):
return self.testcase_functions_config
def exec_content_functions(self, content):
""" execute functions in content.
"""
self.testcase_parser.eval_content_functions(content)
def do_validation(self, validator_dict):
""" validate with functions
"""
comparator = utils.get_uniform_comparator(validator_dict["comparator"])
validate_func = self.testcase_parser.get_bind_item("function", 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, resp_obj):
""" check validators with the context variable mapping.
@param (list) validators
@param (object) resp_obj
"""
variables_mapping = self.get_testcase_variables_mapping()
for validator in validators:
validator_dict = resp_obj.parse_validator(validator, variables_mapping)
self.do_validation(validator_dict)
return True