以流畅的风格提供unittest断言。
pytruth的Python项目详细描述
pytruth:python中的真理
以流畅的风格提供unittest断言。 从Java实现中转换, google/truth
许可证
pytruth是在apache 2.0许可下获得许可的。
免责声明
pytruth不是谷歌的官方产品。
贡献
请参见贡献指南 在创建拉取请求之前。
支架
pytruth不是一个积极维护的项目。不提供任何支持。
它与社区共享,带来一个表达性的、一致的断言 样式到可能使用以下组合的项目 单元测试, 降落伞, 谷歌测试, mox,和 模拟-尤其是 熟悉AHRFF ="HTTPS://GITHUBCOM/GoGOL/Error"Re="NoFoLLoW"> Java真值< /A>
用户组: pytruth users@googlegroups.com
安装
pytruth可以使用pip安装
pip install pytruth
概述
导入 然后,不要写 一个人会写 测试应该更容易读写,并且更清晰地流动。 unittest断言接受一个 被测对象的类型(传递给 在python 2中, 在python 3中,使用 如果共享值上的迭代器(预期的或实际的)更改了
值或其基本元素,行为未定义:
所有断言、任何断言或某些断言都可能任意地成功或失败。 此库是线程安全的;您可以并行执行多个断言。 pytruth支持比unittest更细粒度的真实性区分。
特别是,它区分了"is 传递给匹配函数的 传递给 有序的意味着iterable的元素必须增加(或减少,
从开始到结束取决于 默认情况下, 当使用 当使用 带有(…)truth
模块并将asserttha()方法别名为
asserttha()
以开始断言
事物:< >fromtruth.truthimportAssertThat
self.assertEqual(a,b)self.assertTrue(c)self.assertIn(a,d)self.assertTrue(aindandbind)self.assertTrue(aindorbindorcind)withself.assertRaises(Error):Explode()
AssertThat(a).IsEqualTo(b)AssertThat(c).IsTrue()AssertThat(d).Contains(a)AssertThat(d).ContainsAllOf(a,b)AssertThat(d).ContainsAnyOf(a,b,c)withAssertThat(Error).IsRaised():Explode()
限制
msg
参数,以便在断言失败时显示。
pytruth没有这样的机制,尽管它的失败消息往往更多
提供信息。assertthat()
的参数)将
直到运行时才知道,不像在编译时已知类型的Java。
IDE可能无法正确地自动完成断言主题上的可用谓词。none
比其他任何东西都要小,除了none
本身。
none
小于nan
,且小于负无穷大。因此,使用
当函数可能返回none
时,请注意。断言
assertthan(func()).islessthan(0)
是否成功func()
返回负数
数字或无
。相反,首先用
isnone()
或isnotnone()
在执行不等式断言之前。<;
>;
<;=
>;=
时,none
不再具有可比性。
pytruth检测python解释器的版本并进行比较或失败
适当地,而不是让python 3的typeerror
冒泡起来。转换配方
一般
<表><广告> < /广告><正文>单元测试
pytruth 资产质量(A,B)
断言(a).isequalto(b)
assertnotequal(a,b)
断言(a).isnotequalto(b)
asserttrue(a)
断言(a).istruthy()
assertfalse(a)
断言(a).isfalsy()
断言(a,true)
断言(a).istrue()
断言(a,false)
断言(a).isfalse()
断言(a,b)
断言(a).issameas(b)
断言不(a,b)
断言(a).isnotsameas(b)
断言无(a)
断言(a).isnone()
断言没有(a)
断言(a).isnotnone()
断言(a,b)
断言(a).isin(b)
断言(a,[b,c,d])
断言(a).isanyof(b,c,d)
assertnotin(a,b)
断言(a).isnotin(b)
assertnotin(a,[b,c,d])
断言(a).isnoneof(b,c,d)
断言声明(a,b)
断言(a).i停止(b)
断言状态(a,b)
断言(a).isnotinstanceof(b)
asserttrue(hasatt(a,b))
assertthat(a).hasattribute(b)
assertfalse(hasatt(a,b))
断言(a).doesnothaveattribute(b)
asserttrue(可调用(a))
断言(a).iscallable()
assertfalse(可调用(a))
断言(a).isnotcallable()
真实性
true
"和"istruthy"
unittest的asserttrue(x)
等价于断言(bool(x),true)
,
它的assertfalse(x)
等价于断言(bool(x),false)
。
pytruth的istrue()
和isfalse()
谓词只匹配布尔值
受试者自己。
因此,不要盲目地将asserttrue()
转换为istrue()
,
同样,对于assertfalse()
和isfalse()
真理的断言
< /广告><正文>结果
错误的断言
结果 asserttrue(真)
成功 assertfalse(假)
成功 asserttrue(1)
成功 assertfalse(0)
成功 asserttrue(无)
失败 assertfalse(无)
成功 断言(true).istrue()
成功 断言(false).isfalse()
成功 断言(1).istrue()
失败 断言(0.isfalse()
失败 断言(none).istrue()
失败 断言(none).isfalse()
失败 断言(true).istruthy()
成功 断言(false).isfalsy()
成功 断言(1).istruthy()
成功 断言(0).isfalsy()
成功 断言(无).istruthy()
失败 断言(none).isfalsy()
成功 字符串
<表><广告> < /广告><正文>单元测试
pytruth 资产质量(len(s),n)
assertthat(s).haslength(n)
asserttrue(s.startswith('a'))< /代码>
assertthat(s).startswith('a')
asserttrue(s.endswith('a'))
断言.endswith('a')
assertregex(s,r)
assertregexpmatches(s,r)
assertthat(s).containsmatch(r)
assertnotregex(s,r)
assertnotregexpmatches(s,r)
断言不包含匹配项(r)
assertregex(s,'^r')
assertregexpmatches(s,'^r')
assertthat(s).matches('r')
assertnotregex(s,'^r')
assertnotregexpmatches(s,'^r')
断言.doesnotmatch('r')
匹配字符串
r
参数可以是
r'raw string'
,或从re.compile()返回的模式对象
的数字、字符串和其他类似的东西
<表><广告> < /广告><正文>单元测试
pytruth 无资产(A,B)
断言(a).islessthan(b)
assertgreater(a,b)
断言(a)大于(b)
资产负债表(A,B)
断言(a).isatmost(b)
资产净值(A,B)
断言(a).isatelast(b)
数字
<表><广告> < /广告><正文>单元测试
pytruth 资产质量(A,0)
断言(a).iszero()
assertnotequal(a,0)
断言(a).isnonzero()
assertequal(a,float('inf'))
断言(a).ispositiveInfinity()
asserteQual(a,float('-inf'))
断言(a).isnegativeinfinity()
assertfalse(a.isinf()或a.isnan())
断言(a).isfinite()
asserttrue(a.isnan())
断言(a).isnan()
assertfalse(a.isnan())
断言(a).isnotnan()
资产质量(a,b,delta=d)
断言(a).iswithin(d).of(b)
assertnotalmostequal(a,b,delta=d)
断言(a).isnotwithin(d).of(b)
列表、字符串和其他iterable
<表><广告> < /广告><正文>单元测试
pytruth 资产质量(len(a),n)
断言(a).hassize(n)
资产质量(len(a),0)
断言(a).isempty()
assertgreaterthan(len(a),0)
断言(a).isnotempty()
断言(b,a)
assertthat(a).包含(b)
assertnotin(b,a)
断言(a)不包含(b)
asserttrue(a中的b和a中的c)
assertthat(a).containsallof(b,c)
assertthat(a).containsallin([b,c])
asserttrue(a中的b或a中的c)
assertthat(a).containsanyof(b,c)
assertthat(a).containsanyin([b,c])
asserttrue(a中的b和a中的c,len(a)==2)
断言(a).实际包含(b,c)
assertcountequal(a,b)
assertitemsqual(a,b)
assertthat(sorted(a)).containsActlyElementsin(sorted(b)).inorder()
asserttrue(b不在a中,c不在a中)
assertthat(a).containsnoneof(b,c)
assertthat(a).containsnonein([b,c])
n/a 断言(a).containsNoDuplicates()
n/a 断言(a).isordered()
n/a assertthat(a).isordereddaccording to(cf)
n/a 断言(a).isstrictlyOrdered()
<表><广告> n/a assertthat(a).isstrictlyorderedaccordingto(cf)
< /广告><正文>退出
pytruth assertlen(a,n)
断言(a).hassize(n)
断言(a)
断言(a).isempty()
assertnotempty(a)
断言(a).isnotempty()
定义顺序
的
和
cf
参数isorderedchardingto()isstrictlyorderedaccordingto()
应该是合同之后的可调用的
ofcmp(x,y)
:如果x<;y,则返回负数,如果x==y,则返回零,
如果x>;y,则为正值。cf
)。允许相邻元素
相等。严格排序意味着另外,元素必须是唯一的
(即,单调递增或递减)。isordered()
等同于isordereddaccording to(cmp)
isstrictlyordered()
等同于isstrictlyorderedaccordingto(cmp)
断言顺序
containsAll…
和containsExactly…
不强制
测试对象中元素的顺序与预期的顺序匹配
价值。为此,将inorder()
附加到返回的谓词。 < /广告><正文>包含断言
结果 断言([2,4,6])。包含所有(6,2)
成功 断言([2,4,6])。包含所有(6,2).inorder()
失败 断言([2,4,6])。包含所有(2,6).inorder()
成功 断言([2,4,6])。实际包含(2,6,4)
成功 断言([2,4,6])。实际包含(2,6,4).inorder()
失败 断言([2,4,6])。实际包含(2,4,6).inorder()
成功 inorder()
时,确保被测对象和预期的
值具有可预测的顺序,否则结果未定义。例如,
断言(list a).containsExactlyElementsin(set a).inorder()
可能成功,也可能失败,这取决于集合如何实现排序。
词典,除上表外
<表><广告> < /广告><正文>单元测试
pytruth 断言(k,d)
assertthat(d).containskey(k)
assertnotin(k,d)
assertthat(d).doesnotcontainkey(k)
断言((k,v),d.items())
assertthat(d).containsitem(k,v)
assertnotin((k,v),d.items())
断言(d).doesnotcontainitem(k,v)
资产质量(d,{k1:v1,k2:v2})
断言(d).实际包含(k1,v1,k2,v2)
资产质量(d1,d2)
assertthat(d1).containseActlyitemsin(d2)
代码>assertdictcontainssubset(d1,d2) 断言(d1.items()).containsAllin(d2.items())
例外情况
<表><广告> < /广告><正文>单元测试
pytruth 使用assertraises(e):
使用assertthat(e.israised():
使用assertraiseregex(e,r):
有资产(e).israised(matching=r):
n/a 与assertthat(e).israised(containing='a'):
资产质量(e.message,m)
assertthat(e).hasmessage(m)
asserttrue(e.message.startswith('a'))
assertthat(e).hasmessagethat().startswith('a')
断言(a,e.args)
assertthat(e).hasargsthat()。包含(a)
匹配引发的异常
assertthat(e.israised()
上下文预期出现异常时,任何
接受类型等于e
或e
的子类的异常。
如果引发的异常不是e
的子类,则断言失败。assertthat(e).israised()
上下文中的e
参数可以是
异常类
valueerror('some error')
。如果传递了实例,则会引发任何异常
被测代码还必须具有匹配的消息
和参数
属性,
除了作为预期异常的子类之外。模拟函数
<表><广告> < /广告><正文>单元测试
pytruth m.assert_called()
断言(m).wasCalled()
m.assert_not_called()
断言(m).wasnotCall()
m.assert_called_once()
断言(m).wasCalled().once()
assertequal(m.call_count,n)
断言(m).wasCall().times(n)
m.assert_called_with(*a,**k)
断言(m).wasCall().lastWith(*a,**k)
m.assert用(*a,**k)调用"u once"
assertthat(m).wasmalled().once().with(*a,**k)
n/a assertthat(m).was called()。with(*a,**k).once()
m.assert_has_calls(calls,
any_order=true)
assertthat(m).hascalls(calls)
m.assert_has_calls(calls,
any_order=false)
assertthat(m).hascalls(calls).inorder()
n/a 断言(m).hasExactlyCalls(c1,c2)
n/a 断言(m).hasExactlyCalls(c1,c2).inorder()
m.assert_any_call(*a,**k)
assertthat(m).wasCall()。带有(*a,**k)
被调用一次,带有参数
被调用().once().和带有(…).once()
的被调用().
是微妙的不同。
wasCall().once().with(…)
断言函数
曾经有一次,那一次,它被称为
论据。wasCall().with(…).once()
断言函数已被传递
这些论点只有一次,但可以用
其他无关的论点。因此,wasCall().once().with(…)
是
更严格的断言。考虑使用hasExactlyCalls()
以获得更清晰的信息。课程
<表><广告> < /广告><正文>单元测试
pytruth <鳕鱼>鳕鱼e>asserttrue(a.issubclass(b) 断言(a).issubclassof(b)
推荐PyPI第三方库