介绍
Simpleassertions library for unit testing in Python with a nice fluentAPI. SupportsbothPython 2 and 3.
Assertpy works best with a python test runner like pytest(http://pytest.org/) (our favorite) or Nose(http://nose.readthedocs.org/).
总结一下:一个非常Nice的断言库,同时支持Python2和Python3。可用于的pytest、nose等单元测试框架。
文档
安装
pip install assertpy
用法
例子一:
from assertpy import assert_thatassert_that(1 + 2).is_equal_to(3)assert_that('foobar').is_length(6).starts_with('foo').ends_with('bar')assert_that('a', 'b', 'c').contains('a').does_not_contain('x')

例子二:
from assertpy import assert_warnassert_warn('foo').is_length(4)assert_warn('foo').is_empty()assert_warn('foo').is_false()assert_warn('foo').is_digit()assert_warn('123').is_alpha()

详细介绍
要么看官方文档,要么看后面给的一个文档,介绍的还是蛮全面的。
官方文档:

文章链接:
类型判断assert_that(’’).is_not_none()#不是nullassert_that(’’).is_empty()#是空assert_that(’’).is_false()#是falseassert_that(’’).is_type_of(str)#是str的类型assert_that(’’).is_instance_of(str)#是str的实例常用assert_that(‘foo’).is_length(3)#字符串长度是3assert_that(‘foo’).is_not_empty()#不是空的assert_that(‘foo’).is_true()#是trueassert_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()#是falseassert_that(0).is_type_of(int)#是int类型assert_that(0).is_instance_of(int)#是int的实例整数0正负判断assert_that(0).is_zero()#是0assert_that(1).is_not_zero()#不是0assert_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()#是falseassert_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和infassert_that(float(‘NaN’)).is_nan()#是NaN(未定义或不接可表述的值)assert_that(123.4).is_not_nan()#不是NaNassert_that(float(‘Inf’)).is_inf()#是inf(无穷大)assert_that(123.4).is_not_inf()#不是infnan是无效数字,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博主「xia@xia」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/xiadanying/article/details/90748630
这玩意用着真是太方便了,以后多用用吧。
