关键字 开发-09 validate断言
1. yaml文件中添加validate进行接口断言
首先我们在utils/validate.py文件添加需要的断言方式
import re
def equals(check_value, expect_value):
assert check_value == expect_value, f'{check_value} == {expect_value}'
def less_than(check_value, expect_value):
assert check_value < expect_value, f'{check_value} < {expect_value}'
def less_than_or_equals(check_value, expect_value):
assert check_value <= expect_value, f'{check_value} <= {expect_value}'
def greater_than(check_value, expect_value):
assert check_value > expect_value, f'{check_value} > {expect_value})'
def greater_than_or_equals(check_value, expect_value):
assert check_value >= expect_value, f'{check_value} >= {expect_value}'
def not_equals(check_value, expect_value):
assert check_value != expect_value, f'{check_value} != {expect_value}'
def string_equals(check_value, expect_value):
assert str(check_value) == str(expect_value), f'{check_value} == {expect_value}'
def length_equals(check_value, expect_value):
expect_len = _cast_to_int(expect_value)
if check_value is None:
check_value = []
if isinstance(check_value, list):
assert len(check_value) == expect_len, f'{len(check_value)} == {expect_value}'
elif isinstance(check_value, (int, float)):
assert len(str(check_value)) == expect_len, f'{len(str(check_value))} == {expect_value}'
else:
assert len(check_value) == expect_len, f'{len(check_value)} == {expect_value}'
def length_greater_than(check_value, expect_value):
expect_len = _cast_to_int(expect_value)
assert len(check_value) > expect_len, f'{len(check_value)} > {expect_value}'
def length_greater_than_or_equals(check_value, expect_value):
expect_len = _cast_to_int(expect_value)
assert len(check_value) >= expect_len, f'{len(check_value)} >= {expect_value}'
def length_less_than(check_value, expect_value):
expect_len = _cast_to_int(expect_value)
assert len(check_value) < expect_len, f'{len(check_value)} < {expect_value}'
def length_less_than_or_equals(check_value, expect_value):
expect_len = _cast_to_int(expect_value)
assert len(check_value) <= expect_len, f'{len(check_value)} <= {expect_value}'
def contains(check_value, expect_value):
if isinstance(check_value, (list, tuple, dict, str)):
assert expect_value in check_value, f'{expect_value} in {check_value}'
else:
# 数字类型包含
assert expect_value in str(check_value), f'{expect_value} in {check_value}'
def contained_by(check_value, expect_value):
if isinstance(expect_value, (list, tuple, dict, str)):
assert check_value in expect_value, f'{check_value} in {expect_value}'
else:
# 数字类型包含
assert str(check_value) in expect_value, f'{check_value} in {check_value}'
def regex_match(check_value, expect_value):
assert isinstance(expect_value, str)
assert isinstance(check_value, str)
assert re.match(expect_value, check_value)
def startswith(check_value, expect_value):
assert str(check_value).startswith(str(expect_value)), f'{str(check_value)} startswith {str(expect_value)}'
def endswith(check_value, expect_value):
assert str(check_value).endswith(str(expect_value)), f'{str(check_value)} endswith {str(expect_value)}'
def _cast_to_int(expect_value):
try:
return int(expect_value)
except Exception:
raise AssertionError(f"%{expect_value} can't cast to int")
def bool_equals(check_value, expect_value):
assert bool(check_value) == bool(expect_value), f'{check_value} -> {bool(check_value)} == {expect_value}'
def _cast_to_int(expect_value):
try:
return int(expect_value)
except Exception:
raise AssertionError(f"%{expect_value} can't cast to int")
在run.py文件中新增断言函数
def validate_response(self, response, validate_check: list):
"""校验结果, eg: - eq: [check_type, expect_value]"""
if not isinstance(validate_check, list):
log.error(f'validate_check object is not list type!')
for check in validate_check:
for check_type, check_value in check.items():
actual_value = extract.extract_by_object(response, check_value[0]) # 实际结果
expect_value = check_value[1] # 期望结果
log.info(f'validate校验结果->{check_type}: [{actual_value}, {expect_value}]')
if check_type in ['eq', 'equals', 'equal']:
validate.equals(actual_value, expect_value)
elif check_type in ["lt", "less_than"]:
validate.less_than(actual_value, expect_value)
elif check_type in ["le", "less_or_equals"]:
validate.less_than_or_equals(actual_value, expect_value)
elif check_type in ["gt", "greater_than"]:
validate.greater_than(actual_value, expect_value)
elif check_type in ["ne", "not_equal"]:
validate.not_equals(actual_value, expect_value)
elif check_type in ["str_eq", "string_equals"]:
validate.string_equals(actual_value, expect_value)
elif check_type in ["len_eq", "length_equal"]:
validate.length_equals(actual_value, expect_value)
elif check_type in ["len_gt", "length_greater_than"]:
validate.length_greater_than(actual_value, expect_value)
elif check_type in ["len_ge", "length_greater_or_equals"]:
validate.length_greater_than_or_equals(actual_value, expect_value)
elif check_type in ["len_lt", "length_less_than"]:
validate.length_less_than(actual_value, expect_value)
elif check_type in ["len_le", "length_less_or_equals"]:
validate.length_less_than_or_equals(actual_value, expect_value)
elif check_type in ["contains"]:
validate.contains(actual_value, expect_value)
else:
if hasattr(validate, check_type):
getattr(validate, check_type)(actual_value2, expect_value)
else:
log.error(f'{check_type} not valid check type!')
execute_yaml_case
用例函数中,添加validate处理逻辑
elif item == 'validate':
log.info(f'validate校验的内容:{value}')
validate_value = self.validate_response(response, value)
运行,查看结果,发现断言成功,用例pass