assertpy
类型判断
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’])
列表、元祖和字符串的断言类似