python的无痛断言和验证库。
verif的Python项目详细描述
verify是python的一个无痛断言库。
链接
快速启动
使用pip安装:
pip install verify
使用多个断言验证某些值:
fromverifyimportexpect,Not,Truthy,Falsy,Less,Greaterexpect(5*5,Truthy(),Not(Falsy),Greater(15),Less(30))
使用自己的断言函数进行验证:
defis_just_right(value):assertvalue=='just right','Not just right!'# Passesexpect('just right',is_just_right)# Failstry:expect('too cold',is_just_right)exceptAssertionError:raise
注意:assert函数应该返回一个truthy值,否则,expect将把函数的falsy返回视为失败的指示,并随后提升它自己的AssertionError。
使用自己的谓词函数进行验证:
defis_awesome(value):return'awesome'invaluedefis_more_awesome(value):returnvalue>'awesome'expect('so awesome',is_awesome,is_more_awesome)
使用链接语法进行验证:
expect(1).Truthy().Number().NotBoolean().Not(is_awesome)
不使用expect进行验证,因为verify断言可以单独使用:
importverify# These would pass.verify.Truthy(1)verify.Equal(2,2)verify.Greater(3,2)# These would fail with an AssertionErrorverify.Truthy(0)verify.Equal(2,3)verify.Greater(2,3)
如果您希望看到使用assert,那么如果没有AssertionError,则所有verify断言都将返回True:
assertTruthy(1)assertexpect(1,Truthy(),Number())
多种语法样式
有几种语法样式可以帮助构建更自然的断言链。
期望…成为
将expect与to_be别名一起使用。所有pascal case断言都有to_be_*和to_not_be_*前缀(带有一些期望值)。
expect(something).to_be_int().to_be_less_or_equal(5).to_be_greater_or_equal(1)expect(something_else).to_not_be_float().to_be_number()
确保…是
将ensure与is别名一起使用。所有pascal case断言都有is_*和is_not_*前缀(带有一些期望值)。
ensure(something).is_int().is_less_or_equal(5).is_greater_or_equal(1)ensure(something_else).is_not_float().is_number()
经典
对pascal case断言使用expect或ensure。
ensure(something).Int().LessOrEqual(5).GreaterOrEqual(1)expect(something_else).Float().Number()
注意:虽然建议不要混合样式,但是每个断言语法都可以与expect和ensure一起使用。所以您可以调用expect(..).is_int()和ensure(..).to_be_int()。
命名约定异常
如上所述,有些断言具有非标准别名:
- Not:not_,does_not,to_fail,和fails
- Predicate:does,to_pass,和passes
- All:all_,does_all,和passes_all
- NotAll:not_all,does_not_all,和fails_all
- Any:any_,does_any,和passes_any
- NotAny:not_any,does_not_any,和fails_any
- Match:to_match,is_match和matches
- NotMatch:to_not_match,is_not_match和does_not_match
- Is:to_be和is_
- Contains:to_contain和contains
- NotContains:to_not_contain和does_not_contain
- ContainsOnly:to_contain_only和contains_only
- NotContainsOnly:to_not_contain_only和does_not_contain_only
- Length:to_have_length和has_length
- NotLength:to_not_have_length和does_not_have_length
验证器
verify中的所有验证器都是可调用的,可以在两个上下文中使用:
- 如Equal(a, b)中那样,如果为false,则会引发AssertionError。
- 与expect结合使用,如expect(a, Equal(b)),它还可以提高AssertionError。
可用的验证器是:
Validator | Description |
---|---|
^{tt83}$ | Assert that ^{tt84}$. |
^{tt85}$ | Assert that ^{tt86}$. |
^{tt23}$ | Assert that a callable doesn’t raise an ^{tt2}$. |
^{tt28}$ | Assert that ^{tt90}$. |
^{tt32}$ | Assert that all of the list of predicates evaluate ^{tt92}$ as truthy. |
^{tt36}$ | Assert ^{tt94}$. |
^{tt40}$ | Assert that any of the list of predicates evaluate ^{tt92}$ as truthy. |
^{tt44}$ | Assert ^{tt98}$. |
^{tt99}$ | Assert that ^{tt100}$. |
^{tt101}$ | Assert ^{tt102}$. |
^{tt48}$ | Assert that ^{tt92}$ matches regular expression ^{tt105}$. |
^{tt52}$ | Assert ^{tt107}$. |
^{tt56}$ | Assert that ^{tt109}$. |
^{tt110}$ | Assert ^{tt111}$. |
^{tt112}$ | Assert that ^{tt113}$. |
^{tt114}$ | Assert ^{tt115}$. |
^{tt116}$ | Assert that ^{tt117}$. |
^{tt118}$ | Assert ^{tt119}$. |
^{tt120}$ | Assert that ^{tt121}$. |
^{tt122}$ | Assert ^{tt123}$. |
^{tt124}$ | Assert that ^{tt125}$. |
^{tt126}$ | Assert ^{tt127}$. |
^{tt128}$ | Assert that ^{tt129}$. |
^{tt130}$ | Assert ^{tt131}$. |
^{tt132}$ | Assert that ^{tt133}$. |
^{tt134}$ | Assert ^{tt135}$. |
^{tt136}$ | Assert that ^{tt137}$. |
^{tt138}$ | Assert ^{tt139}$. |
^{tt140}$ | Assert that ^{tt141}$. |
^{tt142}$ | Assert ^{tt143}$. |
^{tt144}$ | Assert that ^{tt145}$. |
^{tt146}$ | Assert ^{tt147}$. |
^{tt148}$ | Assert that ^{tt149}$. |
^{tt150}$ | Assert ^{tt151}$. |
^{tt152}$ | Assert that ^{tt92}$ matches the datetime format string ^{tt105}$. |
^{tt155}$ | Assert ^{tt156}$. |
^{tt157}$ | Assert that ^{tt158}$. |
^{tt159}$ | Assert ^{tt160}$. |
^{tt161}$ | Assert that ^{tt162}$. |
^{tt163}$ | Assert ^{tt164}$. |
^{tt165}$ | Assert that ^{tt166}$. |
^{tt167}$ | Assert ^{tt168}$. |
^{tt169}$ | Assert that ^{tt170}$. |
^{tt171}$ | Assert ^{tt172}$. |
^{tt59}$ | Assert that ^{tt174}$. |
^{tt62}$ | Assert ^{tt176}$. |
^{tt65}$ | Assert that values from ^{tt105}$ are the only ones contained in ^{tt92}$. |
^{tt68}$ | Assert ^{tt181}$. |
^{tt182}$ | Assert that ^{tt92}$ is a subset of ^{tt105}$. |
^{tt185}$ | Assert ^{tt186}$. |
^{tt187}$ | Assert that ^{tt92}$ is a superset of ^{tt105}$. |
^{tt190}$ | Assert ^{tt191}$. |
^{tt192}$ | Assert that ^{tt92}$ contains unique items. |
^{tt194}$ | Assert ^{tt195}$. |
^{tt71}$ | Assert that ^{tt197}$. |
^{tt74}$ | Assert that ^{tt199}$. |
^{tt200}$/^{tt201}$ | Assert that ^{tt202}$. |
^{tt203}$/^{tt204}$ | Assert that ^{tt205}$. |
^{tt206}$/^{tt207}$ | Assert that ^{tt208}$. |
^{tt209}$/^{tt210}$ | Assert that ^{tt211}$. |
^{tt212}$ | Assert that ^{tt213}$. |
^{tt214}$ | Assert ^{tt215}$. |
^{tt216}$ | Assert that ^{tt217}$. |
^{tt218}$ | Assert that ^{tt219}$. |
^{tt220}$ | Assert that ^{tt221}$. |
^{tt222}$ | Assert that ^{tt223}$. |
^{tt224}$ | Assert that ^{tt92}$ is monotonic with respect to ^{tt226}$. |
^{tt227}$ | Assert that ^{tt92}$ is monotonically increasing. |
^{tt229}$ | Assert that ^{tt92}$ is strictly increasing. |
^{tt231}$ | Assert that ^{tt92}$ is monotonically decreasing. |
^{tt233}$ | Assert that ^{tt92}$ is strictly decreasing. |
有关详细信息,请参阅http://verify.readthedocs.org上的完整文档。