当前位置:网站首页>APP自动化测试-6.断言处理assert与hamcrest
APP自动化测试-6.断言处理assert与hamcrest
2022-08-06 17:48:00 【灯塔-tester】
APP自动化测试-6.断言处理assert与hamcrest
前言
主要记录一下常规断言assert的常用场景和hamcrest断言的简单使用
提示:以下是本篇文章正文内容,下面案例可供参考
一、assert断言
自动化测试中断言通常分为两种
- 元素是否存在
- 元素的属性是否符合预期
元素存在判断,可以通过坐标或通过目标元素是否存在某个属性来判断,示例如下:
# 通过坐标判断元素是否存在
element = self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("点击登录")')
assert int(element.size['width']) > 1
assert int(element.size['height']) > 1
# 通过元素属性判断元素是否存在
ele_text = self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("点击登录")').text
assert '登录' in ele_text
注:
- assert后放的为表达式,若要定义异常信息,可以如下
assert 1 > 2, 'this is error message'
二、hamcrest断言
hamcrest是一个以测试为目的,能组合成灵活表达式的匹配器类库,用于编写断言的框架,使用这个框架编写断言,提高开发和测试的效率。
hamcrest提供了大量的匹配器方法,每个匹配器都用来执行特定的断言比较操作,同时hamcrest可扩展性比较强,能都自定义匹配器。
hamcrest语法:
def assert_that(actual, matcher=None, reason=""):
# actual 为实际值
# matcher为期望值
hamcrest匹配器:
''' 对象 equal_to - 匹配相等的对象 has_length - 匹配长度 len(item) has_property - 匹配属性值 has_properties - 匹配包含所有属性值的对象 has_string - 匹配字符串 str(item) instance_of - 匹配对象的类型 none, not_none - 匹配None或not None same_instance - 匹配相同的对象 calling, raises - calling和raise结合使用 数字 close_to - 匹配接近的数字 greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to - 匹配数字大小 文本 contains_string - 匹配包含字符串 ends_with - 匹配以字符串结尾 equal_to_ignoring_case - 匹配完整字符串且忽略大小写 equal_to_ignoring_whitespace - 匹配完整的字符串且忽略空白字符 starts_with - 匹配以字符串开始 string_contains_in_order- 在相对位置,匹配部分字符如string_contains_in_order("bc", "fg", "jkl")将会匹配abcdefghijklm 逻辑 all_of - 匹配所有指定的匹配项 and any_of - 匹配其中任意一个匹配项 or anything - 匹配任何条件,等效于true is_not - 匹配相反的条件,如assert_that(cheese, is_not(equal_to(smelly))) not_ - 等效于is_not 如 assert_that(alist, not_(has_item(item))) 序列 contains_exactly - 匹配完整的序列,排序也要一致 contains_inanyorder - 匹配完整的序列,不要求排序一致 has_item - 匹配序列中指定的元素 has_items - 匹配序列中指定的多个元素,不要求排序 is_in - 匹配元素是否在指定序列中 only_contains - 匹配序列是否在指定list中出现 empty - 匹配空序列 字典 has_entries - 匹配字典多个键值对 has_entry - 匹配字典键值对 has_key - 匹配字典的key has_value - 匹配字典的value '''
常用匹配器示例:
# equal_to 比较对象相等
assert_that(10, equal_to(10))
# close_to 匹配接近的数字,close_to中的2个参数第一个是期望的值,比如为8,第二个参数为浮动空间,比如为1,那么实际值在区间:8+1与8-1之内即为true
assert_that(9, close_to(8, 1))
# contains_string,判断实际值中是否包含某个字符串,contains_string参数为包含或匹配的字符串,实际结果的字符串包含contains_string中定义的字符串即返回True
assert_that("this is test message", contains_string("message"))
通常actual值需要根据从元素属性中获取
总结
简单记录assert的断言,后面根据项目慢慢完善断言的使用。
边栏推荐
- win7彻底重置清除所有软件 win7怎么重置系统
- `English` 2022/8/3
- 使用接口地址池为直连网段分配地址
- How to reset the power-on password of win7 without u disk
- Flyway error source code analysis - Validate failed: Migrations have failed validation
- php ini设置session过期时间
- `英语` 2022/8/6
- Principle of USB Communication
- 8.paddlepaddle之loss损失函数自定义
- js中对象合并方法整理
猜你喜欢

2022 华数杯 C 题 插层熔喷非织造材料的性能控制研究

FP6601AP5 CPC-16L Type-A HVDCP控制器与插入/输出自动检测

面向方面情感分析的双句法感知图注意网络

为什么企业不愿意升级ERP系统

论文阅读笔记(三)——YOLACT Real-time Instance Segmentation

吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第三节:浅层神经网络

2g内存可以装64位win7吗 win7内存支持多大

mysql存储过程实现 定时删除指定表中指定时间点的数据

Win7 and win10 which takes up less resources Win7 and win10 take up resources in detail

Flyway报错源码分析——Validate failed: Migrations have failed validation
随机推荐
Integer缓存策略
DMA/DMA2D概念
3年测试经验,突然很迷茫,软件测试工程师的出路在哪?
为什么企业不愿意升级ERP系统
inotify 高效改进版 sersync 结合 rsync 实现数据实时增量同步
电脑每次开机都要按f1怎么解决 电脑按f1才能启动什么原因
`Mathematics` Clamping Theorem
win7 suffix name is hidden how to open win7 file how to display suffix name
Romance for Single Dogs After Valentine's Day
小鹏政治
ubuntu18.04安装mysql5.7并配置数据存储路径
Redis 基础
下一个十年,什么样的测试最吃香?
appcrash事件怎么解决win7 win7系统修复appcrash
From VLAN to IPVLAN: Talking about virtual network devices and their cloud-native applications
ECCV 2022 | 清华&腾讯AI Lab提出REALY: 重新思考3D人脸重建的评估方法
`英语` 2022/8/4
为什么企业不愿意升级ERP系统
二极管选型指南
最全面贴片电阻知识,封装、尺寸、最大承受功率及选型统统包括-电子技术方案|电路图讲解