码迷,mamicode.com
首页 > 编程语言 > 详细

python第三方库assertpy

时间:2019-08-18 09:23:41      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:extra   http   one   list   大于等于   game   pip   mes   import   

https://github.com/ActivisionGameScience/assertpy
assertpy是基于python的断言库
简介

安装

pip install assertpy
引用
from assertpy import assert_that
可用于python的pytest、nose等单元测试框架
可用于断言strings、numbers、lists、tuples、dicts、sets、Booleans、dates、files、object等
匹配字符串

类型判断

assert_that(’’).is_not_none()#不是null
assert_that(’’).is_empty()#是空
assert_that(’’).is_false()#是false
assert_that(’’).is_type_of(str)#是str的类型
assert_that(’’).is_instance_of(str)#是str的实例

常用

assert_that(‘foo’).is_length(3)#字符串长度是3
assert_that(‘foo’).is_not_empty()#不是空的
assert_that(‘foo’).is_true()#是true
assert_that(‘foo’).is_alpha()#是字母
assert_that(‘123’).is_digit()#是数字
assert_that(‘foo’).is_lower()#是小写的
assert_that(‘FOO’).is_upper()#是大写的
assert_that(‘foo’).is_iterable()#是可迭代类型
assert_that(‘foo’).is_equal_to(‘foo’)#相同
assert_that(‘foo’).is_not_equal_to(‘bar’)#不相同
assert_that(‘foo’).is_equal_to_ignoring_case(‘FOO’)#忽略大小写等于

编码

assert_that(u’foo’).is_unicode() # on python 2#是unicode编码
assert_that(‘foo’).is_unicode() # on python 3#是unicode编码

是否含有部分字符或子字符串

assert_that(‘foo’).contains(‘f’)#字符串包含该字符
assert_that(‘foo’).contains(‘f’,‘oo’)#包含这个字符和这个字符串
assert_that(‘foo’).contains_ignoring_case(‘F’,‘oO’)#忽略大小写包含这个字符和这个字符串
assert_that(‘foo’).does_not_contain(‘x’)#不包含该字符
assert_that(‘foo’).contains_only(‘f’,‘o’)#仅包含f和0字符
assert_that(‘foo’).contains_sequence(‘o’,‘o’)#包含’o’,‘o’序列

是否含有重复字符

assert_that(‘foo’).contains_duplicates()#包含重复字符
assert_that(‘fox’).does_not_contain_duplicates()#不包含重复字符

是否属于几个字符串中的一个,或者大字符串的部分字符串

assert_that(‘foo’).is_in(‘foo’,‘bar’,‘baz’)#在这几个字符串中
assert_that(‘foo’).is_not_in(‘boo’,‘bar’,‘baz’)#不在这几个字符串中
assert_that(‘foo’).is_subset_of(‘abcdefghijklmnopqrstuvwxyz’)#是后面字符串的子集
字符串的头尾字符或子字符串
assert_that(‘foo’).starts_with(‘f’)#字符串以f字符开始
assert_that(‘foo’).ends_with(‘oo’)#字符串以oo字符串结束

匹配正则

assert_that(‘foo’).matches(r’\w’)
assert_that(‘123-456-7890’).matches(r’\d{3}-\d{3}-\d{4}’)
assert_that(‘foo’).does_not_match(r’\d+’)

匹配数字

整数

整数类型判断

assert_that(0).is_not_none()#不是空
assert_that(0).is_false()#是false
assert_that(0).is_type_of(int)#是int类型
assert_that(0).is_instance_of(int)#是int的实例

整数0正负判断

assert_that(0).is_zero()#是0
assert_that(1).is_not_zero()#不是0
assert_that(1).is_positive()#是正数
assert_that(-1).is_negative()#是负数

整数是否等于判断

assert_that(123).is_equal_to(123)#等于
assert_that(123).is_not_equal_to(456)#不等于

整数 区间、大小判断

assert_that(123).is_greater_than(100)#大于
assert_that(123).is_greater_than_or_equal_to(123)#大于等于
assert_that(123).is_less_than(200)#小于
assert_that(123).is_less_than_or_equal_to(200)#小于等于
assert_that(123).is_between(100, 200)#之间
assert_that(123).is_close_to(100, 25)#接近于

整数是否属于判断

assert_that(1).is_in(0,1,2,3)#是后面的某一个
assert_that(1).is_not_in(-1,-2,-3)#不是后面的任何一个
浮点数

浮点数类型判断

assert_that(0.0).is_not_none()#不是空
assert_that(0.0).is_false()#是false
assert_that(0.0).is_type_of(float)#是浮点类型
assert_that(0.0).is_instance_of(float)#是浮点的实例

浮点数是否等于判断

assert_that(123.4).is_equal_to(123.4)#等于
assert_that(123.4).is_not_equal_to(456.7)#不等于

浮点数区间、大小判断

assert_that(123.4).is_greater_than(100.1)#大于
assert_that(123.4).is_greater_than_or_equal_to(123.4)#大于等于
assert_that(123.4).is_less_than(200.2)#小于
assert_that(123.4).is_less_than_or_equal_to(123.4)#小于等于
assert_that(123.4).is_between(100.1, 200.2)#之间
assert_that(123.4).is_close_to(123, 0.5)#接近于

nan和inf

assert_that(float(‘NaN’)).is_nan()#是NaN(未定义或不接可表述的值)
assert_that(123.4).is_not_nan()#不是NaN
assert_that(float(‘Inf’)).is_inf()#是inf(无穷大)
assert_that(123.4).is_not_inf()#不是inf
nan是无效数字,inf是无穷大数字

列表

多层列表时,可通过extracting取出子列表的值
people = [[‘Fred’, ‘Smith’], [‘Bob’, ‘Barr’]]
assert_that(people).extracting(0).is_equal_to([‘Fred’,‘Bob’])
assert_that(people).extracting(-1).is_equal_to([‘Smith’,‘Barr’])
列表、元祖和字符串的断言类似
————————————————
版权声明:本文为CSDN博主「xiadanying」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xiadanying/article/details/90748630

python第三方库assertpy

标签:extra   http   one   list   大于等于   game   pip   mes   import   

原文地址:https://www.cnblogs.com/wsy1103/p/11371152.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!