python的无痛断言和验证库。

verif的Python项目详细描述


versiontraviscoverallslicense

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())

多种语法样式

有几种语法样式可以帮助构建更自然的断言链。

期望…成为

expectto_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()

确保…是

ensureis别名一起使用。所有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断言使用expectensure

ensure(something).Int().LessOrEqual(5).GreaterOrEqual(1)expect(something_else).Float().Number()

注意:虽然建议不要混合样式,但是每个断言语法都可以与expectensure一起使用。所以您可以调用expect(..).is_int()ensure(..).to_be_int()

命名约定异常

如上所述,有些断言具有非标准别名:

  • Notnot_does_notto_fail,和fails
  • Predicatedoesto_pass,和passes
  • Allall_does_all,和passes_all
  • NotAllnot_alldoes_not_all,和fails_all
  • Anyany_does_any,和passes_any
  • NotAnynot_anydoes_not_any,和fails_any
  • Matchto_matchis_matchmatches
  • NotMatchto_not_matchis_not_matchdoes_not_match
  • Isto_beis_
  • Containsto_containcontains
  • NotContainsto_not_containdoes_not_contain
  • ContainsOnlyto_contain_onlycontains_only
  • NotContainsOnlyto_not_contain_onlydoes_not_contain_only
  • Lengthto_have_lengthhas_length
  • NotLengthto_not_have_lengthdoes_not_have_length

验证器

verify中的所有验证器都是可调用的,可以在两个上下文中使用:

  1. Equal(a, b)中那样,如果为false,则会引发AssertionError
  2. expect结合使用,如expect(a, Equal(b)),它还可以提高AssertionError

可用的验证器是:

ValidatorDescription
^{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上的完整文档。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
servlets Java EE+GlassFish:如果用户在其他地方登录,则强制用户注销   java在不更改源代码的情况下添加钩子   安卓 Java TextView Onclick()返回表达式错误   java Excel文件未使用Apache存储内部内存   java getRowCount()和getSelectedColumn()不起作用   无法在java中生成视图绑定。util。Android Gradle插件2.1的NoTouchElementException   Java中的类型转换运算符百分比计算为零   主java中的调用方法   java在Javasound中播放多个示例   java如何知道webDriver成功打开URL   java jmock,每次调用时返回新对象   eclipse使用Java在Selenium WebDriver中生成可执行文件   macos捆绑Java OS X应用程序默认为MacRoman编码   java我应该把我的休息网关变成一个图书馆吗?   java Eclipse插件:查找包含子字符串的行号无效   Java IOException:在Linux上发送UDP数据包时没有可用的缓冲区空间   java无法解析jaxbxjc2中的名称“xjc:globalJavaType”。2.52.罐子   java是否有HibernateAPI来检查是否创建了数据库?