mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-22 12:57:30 +08:00
#29: refactor validator, now support custom defined validators
This commit is contained in:
@@ -1 +1 @@
|
||||
__version__ = '0.8.3'
|
||||
__version__ = '0.8.4'
|
||||
@@ -4,10 +4,12 @@ Built-in dependent functions used in YAML/JSON testcases.
|
||||
|
||||
import datetime
|
||||
import random
|
||||
import re
|
||||
import string
|
||||
import time
|
||||
|
||||
from httprunner.exception import ParamsError
|
||||
from httprunner.utils import string_type
|
||||
|
||||
|
||||
def gen_random_string(str_len):
|
||||
@@ -33,3 +35,69 @@ def sleep(sec):
|
||||
""" sleep specified seconds
|
||||
"""
|
||||
time.sleep(sec)
|
||||
|
||||
|
||||
""" built-in comparators
|
||||
"""
|
||||
def equals(check_value, expect_value):
|
||||
assert check_value == expect_value
|
||||
|
||||
def less_than(check_value, expect_value):
|
||||
assert check_value < expect_value
|
||||
|
||||
def less_than_or_equals(check_value, expect_value):
|
||||
assert check_value <= expect_value
|
||||
|
||||
def greater_than(check_value, expect_value):
|
||||
assert check_value > expect_value
|
||||
|
||||
def greater_than_or_equals(check_value, expect_value):
|
||||
assert check_value >= expect_value
|
||||
|
||||
def not_equals(check_value, expect_value):
|
||||
assert check_value != expect_value
|
||||
|
||||
def string_equals(check_value, expect_value):
|
||||
assert str(check_value) == str(expect_value)
|
||||
|
||||
def length_equals(check_value, expect_value):
|
||||
assert isinstance(expect_value, int)
|
||||
assert len(check_value) == expect_value
|
||||
|
||||
def length_greater_than(check_value, expect_value):
|
||||
assert isinstance(expect_value, int)
|
||||
assert len(check_value) > expect_value
|
||||
|
||||
def length_greater_than_or_equals(check_value, expect_value):
|
||||
assert isinstance(expect_value, int)
|
||||
assert len(check_value) >= expect_value
|
||||
|
||||
def length_less_than(check_value, expect_value):
|
||||
assert isinstance(expect_value, int)
|
||||
assert len(check_value) < expect_value
|
||||
|
||||
def length_less_than_or_equals(check_value, expect_value):
|
||||
assert isinstance(expect_value, int)
|
||||
assert len(check_value) <= expect_value
|
||||
|
||||
def contains(check_value, expect_value):
|
||||
assert isinstance(check_value, (list, tuple, dict, string_type))
|
||||
assert expect_value in check_value
|
||||
|
||||
def contained_by(check_value, expect_value):
|
||||
assert isinstance(expect_value, (list, tuple, dict, string_type))
|
||||
assert check_value in expect_value
|
||||
|
||||
def type_match(check_value, expect_value):
|
||||
assert isinstance(check_value, expect_value)
|
||||
|
||||
def regex_match(check_value, expect_value):
|
||||
assert isinstance(expect_value, string_type)
|
||||
assert isinstance(check_value, string_type)
|
||||
assert re.match(expect_value, check_value)
|
||||
|
||||
def startswith(check_value, expect_value):
|
||||
assert str(check_value).startswith(str(expect_value))
|
||||
|
||||
def endswith(check_value, expect_value):
|
||||
assert str(check_value).endswith(str(expect_value))
|
||||
|
||||
@@ -165,6 +165,9 @@ 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.
|
||||
"""
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -129,7 +129,11 @@ class Runner(object):
|
||||
self.context.bind_extracted_variables(extracted_variables_mapping)
|
||||
|
||||
try:
|
||||
resp_obj.validate(validators, self.context.get_testcase_variables_mapping())
|
||||
resp_obj.validate(
|
||||
validators,
|
||||
self.context.get_testcase_variables_mapping(),
|
||||
self.context.get_testcase_functions_mapping()
|
||||
)
|
||||
except (exception.ParamsError, exception.ResponseError, exception.ValidationError):
|
||||
err_msg = u"Exception occured.\n"
|
||||
err_msg += u"HTTP request url: {}\n".format(url)
|
||||
|
||||
@@ -117,79 +117,37 @@ def query_json(json_content, query, delimiter='.'):
|
||||
|
||||
return json_content
|
||||
|
||||
def match_expected(value, expected, comparator="eq", check_item=""):
|
||||
""" check if value matches expected value.
|
||||
@param value: actual value that get from response.
|
||||
@param expected: expected result described in testcase
|
||||
@param comparator: compare method
|
||||
@param check_item: check item name
|
||||
def get_uniform_comparator(comparator):
|
||||
""" convert comparator alias to uniform name
|
||||
"""
|
||||
try:
|
||||
if value is None or expected is None:
|
||||
assert comparator in ["is", "eq", "equals", "=="]
|
||||
assert value is None
|
||||
assert expected is None
|
||||
|
||||
if comparator in ["eq", "equals", "=="]:
|
||||
assert value == expected
|
||||
elif comparator in ["lt", "less_than"]:
|
||||
assert value < expected
|
||||
elif comparator in ["le", "less_than_or_equals"]:
|
||||
assert value <= expected
|
||||
elif comparator in ["gt", "greater_than"]:
|
||||
assert value > expected
|
||||
elif comparator in ["ge", "greater_than_or_equals"]:
|
||||
assert value >= expected
|
||||
elif comparator in ["ne", "not_equals"]:
|
||||
assert value != expected
|
||||
elif comparator in ["str_eq", "string_equals"]:
|
||||
assert str(value) == str(expected)
|
||||
elif comparator in ["len_eq", "length_equals", "count_eq"]:
|
||||
assert isinstance(expected, int)
|
||||
assert len(value) == expected
|
||||
elif comparator in ["len_gt", "count_gt", "length_greater_than", "count_greater_than"]:
|
||||
assert isinstance(expected, int)
|
||||
assert len(value) > expected
|
||||
elif comparator in ["len_ge", "count_ge", "length_greater_than_or_equals", \
|
||||
"count_greater_than_or_equals"]:
|
||||
assert isinstance(expected, int)
|
||||
assert len(value) >= expected
|
||||
elif comparator in ["len_lt", "count_lt", "length_less_than", "count_less_than"]:
|
||||
assert isinstance(expected, int)
|
||||
assert len(value) < expected
|
||||
elif comparator in ["len_le", "count_le", "length_less_than_or_equals", \
|
||||
"count_less_than_or_equals"]:
|
||||
assert isinstance(expected, int)
|
||||
assert len(value) <= expected
|
||||
elif comparator in ["contains"]:
|
||||
assert isinstance(value, (list, tuple, dict, string_type))
|
||||
assert expected in value
|
||||
elif comparator in ["contained_by"]:
|
||||
assert isinstance(expected, (list, tuple, dict, string_type))
|
||||
assert value in expected
|
||||
elif comparator in ["type"]:
|
||||
assert isinstance(value, expected)
|
||||
elif comparator in ["regex"]:
|
||||
assert isinstance(expected, string_type)
|
||||
assert isinstance(value, string_type)
|
||||
assert re.match(expected, value)
|
||||
elif comparator in ["startswith"]:
|
||||
assert str(value).startswith(str(expected))
|
||||
elif comparator in ["endswith"]:
|
||||
assert str(value).endswith(str(expected))
|
||||
else:
|
||||
raise exception.ParamsError("comparator not supported!")
|
||||
|
||||
return True
|
||||
|
||||
except (AssertionError, TypeError):
|
||||
err_msg = "\n".join([
|
||||
"check item name: %s;" % check_item,
|
||||
"check item value: %s (%s);" % (value, type(value).__name__),
|
||||
"comparator: %s;" % comparator,
|
||||
"expected value: %s (%s)." % (expected, type(expected).__name__)
|
||||
])
|
||||
raise exception.ValidationError(err_msg)
|
||||
if comparator in ["eq", "equals", "=="]:
|
||||
return "equals"
|
||||
elif comparator in ["lt", "less_than"]:
|
||||
return "less_than"
|
||||
elif comparator in ["le", "less_than_or_equals"]:
|
||||
return "less_than_or_equals"
|
||||
elif comparator in ["gt", "greater_than"]:
|
||||
return "greater_than"
|
||||
elif comparator in ["ge", "greater_than_or_equals"]:
|
||||
return "greater_than_or_equals"
|
||||
elif comparator in ["ne", "not_equals"]:
|
||||
return "not_equals"
|
||||
elif comparator in ["str_eq", "string_equals"]:
|
||||
return "string_equals"
|
||||
elif comparator in ["len_eq", "length_equals", "count_eq"]:
|
||||
return "length_equals"
|
||||
elif comparator in ["len_gt", "count_gt", "length_greater_than", "count_greater_than"]:
|
||||
return "length_greater_than"
|
||||
elif comparator in ["len_ge", "count_ge", "length_greater_than_or_equals", \
|
||||
"count_greater_than_or_equals"]:
|
||||
return "length_greater_than_or_equals"
|
||||
elif comparator in ["len_lt", "count_lt", "length_less_than", "count_less_than"]:
|
||||
return "length_less_than"
|
||||
elif comparator in ["len_le", "count_le", "length_less_than_or_equals", \
|
||||
"count_less_than_or_equals"]:
|
||||
return "length_less_than_or_equals"
|
||||
else:
|
||||
return comparator
|
||||
|
||||
def deep_update_dict(origin_dict, override_dict):
|
||||
""" update origin dict with override dict recursively
|
||||
|
||||
Reference in New Issue
Block a user