美文网首页
python接口自动化-Hamcrest断言框架

python接口自动化-Hamcrest断言框架

作者: 疯子李 | 来源:发表于2022-06-21 18:46 被阅读0次

1、核心api

对象

equal_to - 匹配相等对象
has_length - 长度匹配 len()
has_property - 匹配给定名称的属性值
has_properties - 匹配具有所给定属性的对象
has_string - 匹配字符串 str()
instance_of - 匹配对象类型
none, not_none - 匹配none或not none
same_instance - 匹配相同的对象
calling, raises - 封装一个方法调用并断言它引发异常
数字

close_to - 匹配接近给定的数字值
greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to - 匹配数字顺序
文本
print(assert_that(0.1 * 0.1, close_to(0.01,0.000000000000001))) #close_to(预期比较的值,数字所对应的值之间最大差值被认为是接近的)
print(assert_that(0.1 * 0.1, greater_than(0.01))) #大于
print(assert_that(0.1 * 0.1, greater_than_or_equal_to(0.01))) #大于或等于
print(assert_that(0.1 * 0.1, less_than(0.02))) #小于
print(assert_that(0.1 * 0.1, less_than_or_equal_to(0.02))) #小于或等于

contains_string - 匹配部分字符串
ends_with - 匹配字符串的结尾
equal_to_ignoring_case - 匹配完整的字符串但忽略大小写
equal_to_ignoring_whitespace - 匹配完整的字符串,但忽略多余的空格
matches_regexp - 使用正则表达式匹配字符串
starts_with - 匹配字符串的开头
string_contains_in_order - 按相对顺序匹配字符串的各个部分
逻辑

all_of - 如果所有匹配器都匹配才匹配,像Java里的&&
any_of - - 如果任何匹配器匹配就匹配,像Java里的||
anything - 匹配任何东西,如果不关心特定值则在复合匹配器中很有用
is_not, not_ -如果包装的匹配器不匹配器时匹配,反之亦然
序列

contains - 完全匹配整个序列
contains_inanyorder - 以任何顺序匹配整个序列
has_item - 只要给定项目在序列中出现则匹配
has_items - 如果所有给定项以任意顺序出现在序列中则匹配
is_in - 在给定顺序中如果给定项出现则匹配
only_contains - 在给定顺序中如果序列的项目出现则匹配
empty - 如果序列为空则匹配
字典

has_entries - 匹配具有键值对列表的字典
has_entry - 匹配包含键值对的字典
has_key - 使用键匹配字典
has_value - 使用值匹配字典

2、实战

场景一 软断言2个状态码都通过

is_in

场景二 返回数据大于等于1

greater_than_or_equal_to

相关文章

网友评论

      本文标题:python接口自动化-Hamcrest断言框架

      本文链接:https://www.haomeiwen.com/subject/tktyvrtx.html