跳过预期失败
xfail的Python项目详细描述
xfail提供了一个decorator函数xfail来跳过预期的异常。 类似于unittest.skipIf,但是xfail可以指定应该是哪个异常 跳过,并在修饰函数意外传递时引发(仅当 strict是True。
安装
支持的python版本有:2.7、3.4、3.5、3.6
可以通过pip从PyPI安装:
pip install xfail
用法
xfail decorator
xfail接受两个参数,exceptions和strict。
第一个参数(exceptions)应该是要跳过的异常类(例如。 Exception,AssertionError等等)。如果你想跳过多个 异常,使用它们的元组,例如,@xfail((AssertionError, ValueError))。
第二个参数strict应该是布尔值。如果strict是False (默认)并意外传递,引发unittest.SkipTest异常, 这将标志着测试被跳过。这个例子与函数非常相似 由uniteest.skip函数修饰,测试将被计为 跳过。
If it is ^{tt4}$ and the decorated function did not raise (any of) the expected exception(s), ^{tt18}$ exception would be raised. In this case, the test will be counted as fail.
fromxfailimportxfail@xfail(IndexError)defget(l,index):returnl[index]l=[1,2,3]get(4)# no error
还支持多个例外:
@xfail((IndexError,ValueError))defa():'''This function passes IndexError and ValueError ...
在测试脚本中,类似于unittest.TestCase.assertRaises:
fromunittestimportTestCasefromxfailimportxfailclassMyTest(TestCase):deftest_1(self):@xfail(AssertionError)defshould_raise_error():assertFalsea()# test passesdeftest_2(self):@xfail(AssertionError,strict=True)defshould_raise_error():assertTruea()# test failes, since this function should raise AssertionError# Can be used for test function@xfail(AssertionError,strict=True)deftest_3(self)assertFalse# This test will fail@xfail(AssertionError,strict=True)deftest_3(self)assertTrue
有关更多示例,请参见test_xfail.py。