学习笔记-断言
2019-12-10 本文已影响0人
sofiiii
1. 元素是否存在
find_elements
2. 元素属性是否正确
get_attribute
assert 'xxxx' in element.get_attribute('resource-id')
3. 查找元素文本
assert float(find_element_by_id('xxxx).text)>20
4. hamcrest断言
-
Hamcrest是用于Java的单元测试的框架,它使用matcher匹配器来进行断言。在Spring-boot-starter中集成了Hamcrest,无需而外引入。
[https://github.com/hamcrest/PyHamcrest](https://github.com/hamcrest/PyHamcrest)
安装
pip install PyHamcrest
- IS
String str1 = "text";
String str2 = " text ";
assertThat(str1, is(equalToIgnoringWhiteSpace(str2)));
String str1 = "text";
String str2 = "text";
assertThat(str1, is(str2));
- NOT
String str1 = "text";
String str2 = " text ";
assertThat(str1, not(equalToIgnoringWhiteSpace(str2)));
String str1 = "text";
String str2 = "text";
assertThat(str1, not(str2));
- containsString
String str1 = "text123";
String str2 = "text";
assertThat(str1, containsString(str2));
- 以为某个字段开头/结尾
String str1 = "text123";
assertThat(str1, startsWith("text"));// 以某个字符开头
assertThat(str1, endsWith("text"));// 以某个字符开头
- 判断两个对象是否为同一个实体
Cat cat=new Cat();
assertThat(cat, sameInstance(cat));
- 类似OR的效果
String str = "calligraphy";
String start = "call";
String end = "foo";
assertThat(str, anyOf(startsWith(start), containsString(end)));
- 类似AND的效果
String str = "calligraphy";
String start = "call";
String end = "phy";
assertThat(str, allOf(startsWith(start), endsWith(end)));
- Number 匹配器
assertThat(1, greaterThan(0)); // 大于
assertThat(5, greaterThanOrEqualTo(5)); //大于等于
assertThat(-1, lessThan(0)); // 小于
assertThat(-1, lessThanOrEqualTo(5)); // 小于等于
- Text 匹配器
String str = "";
assertThat(str, isEmptyString()); // 空字符串
assertThat(str, isEmptyOrNullString()); // 空字符串或者null
- 匹配字符串相等
String str1 = "text";
String str2 = " text ";
assertThat(str1, equalToIgnoringWhiteSpace(str2)); // 忽略左右空白
assertThat(a, equalToIgnoringCase(b)); //忽略大小写
-
Collections 匹配器
-
检查某个元素是否在集合中
List<String> collection = Lists.newArrayList("ab","cd","ef");
assertThat(collection, hasItem("cd"));
assertThat(collection, not(hasItem("zz")));
assertThat(collection, hasItems("cd", "ab")); // 检查多个元素是否在集合中
- 检查所有元素
List<String> collection = Lists.newArrayList("ab","cd","ef");
assertThat(collection, hasItems("ab","cd","ef"));
assertThat(collection, hasItems("cd", "ab","ef")); // 错误
assertThat(collection, hasItems("ab","cd")); // 错误
assertThat(collection, containsInAnyOrder("cd", "ab","ef")); //正确,不区分顺序
- 集合为空检查
List<String> collection = Lists.newArrayList("ab","cd","ef");
assertThat(collection, empty()); // false, 用于检查集合类型
- 数组为空检查
String[] array = new String[] { "ab" };
assertThat(array, emptyArray()); // false
5. 总结
核心:
anything - 总是匹配,如果你不关心测试下的对象是什么是有用的
describedAs - 添加一个定制的失败表述装饰器
is - 改进可读性装饰器 - 见下 “Sugar”
逻辑:
allOf - 如果所有匹配器都匹配才匹配,像Java里的&&
anyOf - 如果任何匹配器匹配就匹配,像Java里的||
not - 如果包装的匹配器不匹配器时匹配,反之亦然
对象:
equalTo - 测试对象相等使用Object.equals方法
hasToString - 测试Object.toString方法
instanceOf, isCompatibleType - 测试类型
notNullValue, nullValue - 测试null
sameInstance - 测试对象实例
Beans:
hasProperty - 测试JavaBeans属性
集合:
array - 测试一个数组元素test an array’s elements against an array of matchers
hasEntry, hasKey, hasValue - 测试一个Map包含一个实体,键或者值
hasItem, hasItems - 测试一个集合包含一个元素
hasItemInArray - 测试一个数组包含一个元素
数字:
closeTo - 测试浮点值接近给定的值
greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo - 测试次序
文本:
equalToIgnoringCase - 测试字符串相等忽略大小写
equalToIgnoringWhiteSpace - 测试字符串忽略空白
containsString, endsWith, startsWith - 测试字符串匹配