我不知道鼻子插件,但你可以輕鬆編寫你自己的裝飾器來做到這一點。這裏有一個簡單的實現:
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