Python单元测试unittest中提供了一下四种装饰器实现测试跳过和预期故障。(使用Python 2.7.13)
请查考Python手册中:
https://docs.python.org/dev/library/unittest.html
The following decorators implement test skipping and expected failures:
#以下装饰器实施测试跳过和预期故障:
@unittest.skip(原因)
Unconditionally skip the decorated test. reason should describe why the test is being skipped.
#无条件跳过装饰测试。 原因应该说明为什么要跳过测试。
@unittest.skipIf(条件,原因)
Skip the decorated test if condition is true.
#如果条件为真,跳过装饰测试。
@unittest.skipUnless(条件,原因)
Skip the decorated test unless condition is true.
# 跳过装饰的测试,除非条件是真的。
@unittest.expectedFailure
Mark the test as an expected failure. If the test fails when run, the test is not counted as a failure.
#将测试标记为预期的失败。 如果测试在运行时失败,则测试不会被视为失败。
(以上采用谷歌翻译,可能会有差异)
好了,写段代码看下,test.py ,使用的Eclipse
?
好的,运行一下
?
好的,我们对第1行代码进行分析:
s:全称是skipped(跳过)
s:条件为真,所以也是skipped(跳过)
F:条件为真,所以忽略装饰器,执行断言代码,显然是failures(失败)
x:断言结果显然是失败的,但是这是在我们意料之中,所以是expected failures(预期的失败)
u:断言结果显然是pass,但是我们预计可能不通过,所以是unexpected successes(意想不到的成功)
即第13行代码 所示 FAILED (failures=1, skipped=2, expected failures=1, unexpected successes=1)
以上这篇Python单元测试_使用装饰器实现测试跳过和预期故障的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。