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