介绍

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的断言库,同时支持Python2Python3。可用于的pytestnose等单元测试框架。

文档

https://assertpy.github.io/docs.html

安装

  1. pip install assertpy

用法

例子一:

  1. from assertpy import assert_that
  2. assert_that(1 + 2).is_equal_to(3)
  3. assert_that('foobar').is_length(6).starts_with('foo').ends_with('bar')
  4. assert_that('a', 'b', 'c').contains('a').does_not_contain('x')

python assertpy 断言 - 图1

例子二:

  1. from assertpy import assert_warn
  2. assert_warn('foo').is_length(4)
  3. assert_warn('foo').is_empty()
  4. assert_warn('foo').is_false()
  5. assert_warn('foo').is_digit()
  6. assert_warn('123').is_alpha()

python assertpy 断言 - 图2

详细介绍

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

官方文档:

https://github.com/assertpy/assertpy

python assertpy 断言 - 图3

文章链接:

https://blog.csdn.net/xiadanying/article/details/90748630

  1. 类型判断
  2. assert_that(’’).is_not_none()#不是null
  3. assert_that(’’).is_empty()#是空
  4. assert_that(’’).is_false()#是false
  5. assert_that(’’).is_type_of(str)#是str的类型
  6. assert_that(’’).is_instance_of(str)#是str的实例
  7. 常用
  8. assert_that(‘foo’).is_length(3)#字符串长度是3
  9. assert_that(‘foo’).is_not_empty()#不是空的
  10. assert_that(‘foo’).is_true()#是true
  11. assert_that(‘foo’).is_alpha()#是字母
  12. assert_that(‘123’).is_digit()#是数字
  13. assert_that(‘foo’).is_lower()#是小写的
  14. assert_that(‘FOO’).is_upper()#是大写的
  15. assert_that(‘foo’).is_iterable()#是可迭代类型
  16. assert_that(‘foo’).is_equal_to(‘foo’)#相同
  17. assert_that(‘foo’).is_not_equal_to(‘bar’)#不相同
  18. assert_that(‘foo’).is_equal_to_ignoring_case(‘FOO’)#忽略大小写等于
  19. 编码
  20. assert_that(ufoo’).is_unicode() # on python 2#是unicode编码
  21. assert_that(‘foo’).is_unicode() # on python 3#是unicode编码
  22. 是否含有部分字符或子字符串
  23. assert_that(‘foo’).contains(‘f’)#字符串包含该字符
  24. assert_that(‘foo’).contains(‘f’,‘oo’)#包含这个字符和这个字符串
  25. assert_that(‘foo’).contains_ignoring_case(‘F’,‘oO’)#忽略大小写包含这个字符和这个字符串
  26. assert_that(‘foo’).does_not_contain(‘x’)#不包含该字符
  27. assert_that(‘foo’).contains_only(‘f’,‘o’)#仅包含f0字符
  28. assert_that(‘foo’).contains_sequence(‘o’,‘o’)#包含’o’,'o’序列
  29. 是否含有重复字符
  30. assert_that(‘foo’).contains_duplicates()#包含重复字符
  31. assert_that(‘fox’).does_not_contain_duplicates()#不包含重复字符
  32. 是否属于几个字符串中的一个,或者大字符串的部分字符串
  33. assert_that(‘foo’).is_in(‘foo’,‘bar’,‘baz’)#在这几个字符串中
  34. assert_that(‘foo’).is_not_in(‘boo’,‘bar’,‘baz’)#不在这几个字符串中
  35. assert_that(‘foo’).is_subset_of(‘abcdefghijklmnopqrstuvwxyz’)#是后面字符串的子集
  36. 字符串的头尾字符或子字符串
  37. assert_that(‘foo’).starts_with(‘f’)#字符串以f字符开始
  38. assert_that(‘foo’).ends_with(‘oo’)#字符串以oo字符串结束
  39. 匹配正则
  40. assert_that(‘foo’).matches(r’\w’)
  41. assert_that(‘123-456-7890’).matches(r’\d{3}-\d{3}-\d{4}’)
  42. assert_that(‘foo’).does_not_match(r’\d+’)
  43. 匹配数字
  44. 整数
  45. 整数类型判断
  46. assert_that(0).is_not_none()#不是空
  47. assert_that(0).is_false()#是false
  48. assert_that(0).is_type_of(int)#是int类型
  49. assert_that(0).is_instance_of(int)#是int的实例
  50. 整数0正负判断
  51. assert_that(0).is_zero()#是0
  52. assert_that(1).is_not_zero()#不是0
  53. assert_that(1).is_positive()#是正数
  54. assert_that(-1).is_negative()#是负数
  55. 整数是否等于判断
  56. assert_that(123).is_equal_to(123)#等于
  57. assert_that(123).is_not_equal_to(456)#不等于
  58. ####整数 区间、大小判断
  59. assert_that(123).is_greater_than(100)#大于
  60. assert_that(123).is_greater_than_or_equal_to(123)#大于等于
  61. assert_that(123).is_less_than(200)#小于
  62. assert_that(123).is_less_than_or_equal_to(200)#小于等于
  63. assert_that(123).is_between(100, 200)#之间
  64. assert_that(123).is_close_to(100, 25)#接近于
  65. 整数是否属于判断
  66. assert_that(1).is_in(0,1,2,3)#是后面的某一个
  67. assert_that(1).is_not_in(-1,-2,-3)#不是后面的任何一个
  68. 浮点数
  69. 浮点数类型判断
  70. assert_that(0.0).is_not_none()#不是空
  71. assert_that(0.0).is_false()#是false
  72. assert_that(0.0).is_type_of(float)#是浮点类型
  73. assert_that(0.0).is_instance_of(float)#是浮点的实例
  74. 浮点数是否等于判断
  75. assert_that(123.4).is_equal_to(123.4)#等于
  76. assert_that(123.4).is_not_equal_to(456.7)#不等于
  77. 浮点数区间、大小判断
  78. assert_that(123.4).is_greater_than(100.1)#大于
  79. assert_that(123.4).is_greater_than_or_equal_to(123.4)#大于等于
  80. assert_that(123.4).is_less_than(200.2)#小于
  81. assert_that(123.4).is_less_than_or_equal_to(123.4)#小于等于
  82. assert_that(123.4).is_between(100.1, 200.2)#之间
  83. assert_that(123.4).is_close_to(123, 0.5)#接近于
  84. nan和inf
  85. assert_that(float(‘NaN’)).is_nan()#是NaN(未定义或不接可表述的值)
  86. assert_that(123.4).is_not_nan()#不是NaN
  87. assert_that(float(‘Inf’)).is_inf()#是inf(无穷大)
  88. assert_that(123.4).is_not_inf()#不是inf
  89. nan是无效数字,inf是无穷大数字
  90. 列表
  91. 多层列表时,可通过extracting取出子列表的值
  92. people = [[‘Fred’, ‘Smith’], [‘Bob’, ‘Barr’]]
  93. assert_that(people).extracting(0).is_equal_to([‘Fred’,‘Bob’])
  94. assert_that(people).extracting(-1).is_equal_to([‘Smith’,‘Barr’])
  95. 列表、元祖和字符串的断言类似
  96. ————————————————
  97. 版权声明:本文为CSDN博主「xia@xia」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
  98. 原文链接:https://blog.csdn.net/xiadanying/article/details/90748630

这玩意用着真是太方便了,以后多用用吧。