我不知道鼻子插件,但你可以轻松编写你自己的装饰器来做到这一点。这里有一个简单的实现:
import functools
import nose
def expected_failure(test):
@functools.wraps(test)
def inner(*args, **kwargs):
try:
test(*args, **kwargs)
except Exception:
raise nose.SkipTest
else:
raise AssertionError('Failure expected')
return inner
如果我运行这些测试:
@expected_failure
def test_not_implemented():
assert False
@expected_failure
def test_unexpected_success():
assert True
我从鼻子以下的输出:
tests.test.test_not_implemented ... SKIP
tests.test.test_unexpected_success ... FAIL
======================================================================
FAIL: tests.test.test_unexpected_success
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python32\lib\site-packages\nose-1.1.2-py3.2.egg\nose\case.py", line 198, in runTest
self.test(*self.arg)
File "G:\Projects\Programming\dt-tools\new-sanbi\tests\test.py", line 16, in inner
raise AssertionError('Failure expected')
AssertionError: Failure expected
----------------------------------------------------------------------
Ran 2 tests in 0.016s
FAILED (failures=1)
哦,当然了!如果测试失败,它会提高SkipTest,这是完美的 - 谢谢\ o/ – dbr 2012-03-08 12:08:17