#29: refactor validator:

1, relocate validate functions;
2, add unittest for custom defined validators.
This commit is contained in:
debugtalk
2017-12-12 23:59:54 +08:00
parent 044a93d1ff
commit d079f014d9
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

View File

@@ -189,39 +189,3 @@ class ResponseObject(object):
"comparator": comparator
}
return validator_dict
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:
validator_dict = self.parse_validator(validator, variables_mapping)
self.do_validation(validator_dict, functions_mapping)
return True

View File

@@ -129,11 +129,7 @@ class Runner(object):
self.context.bind_extracted_variables(extracted_variables_mapping)
try:
resp_obj.validate(
validators,
self.context.get_testcase_variables_mapping(),
self.context.get_testcase_functions_mapping()
)
self.context.validate(validators, resp_obj)
except (exception.ParamsError, exception.ResponseError, exception.ValidationError):
err_msg = u"Exception occured.\n"
err_msg += u"HTTP request url: {}\n".format(url)