ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

assertpy

2020-06-18 11:55:42  阅读:262  来源: 互联网

标签:assertpy 123.4 equal assert 123 字符串 foo


 

类型判断

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

 

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

标签:assertpy,123.4,equal,assert,123,字符串,foo
来源: https://www.cnblogs.com/tarzen213/p/13156794.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有