预期失败的鼻子插件

dbr*_*dbr 9 python testing nose

是否有一个现有的插件可以使用如下:

@nose.plugins.expectedfailure
def not_done_yet():
    a = Thingamajig().fancynewthing()
    assert a == "example"
Run Code Online (Sandbox Code Playgroud)

如果测试失败,它将显示为跳过的测试:

$ nosetests
...S..
Run Code Online (Sandbox Code Playgroud)

..但如果它意外通过,它会出现类似于失败,可能像:

=================================
UNEXPECTED PASS: not_done_yet
---------------------------------
-- >> begin captured stdout << --
Things and etc
...
Run Code Online (Sandbox Code Playgroud)

有点像SkipTest,但没有实现为阻止测试运行的异常.

我唯一能找到的是这张关于支持unittest2expectedFailure装饰器的(虽然我不想使用unittest2,即使是鼻支持它)

aqu*_*tae 11

我不知道鼻子插件,但你可以轻松编写自己的装饰器来做到这一点.这是一个简单的实现:

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
Run Code Online (Sandbox Code Playgroud)

如果我运行这些测试:

@expected_failure
def test_not_implemented():
    assert False

@expected_failure
def test_unexpected_success():
    assert True
Run Code Online (Sandbox Code Playgroud)

我从鼻子得到以下输出:

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)
Run Code Online (Sandbox Code Playgroud)


Gri*_*ees 5

如果我误解了,请原谅我,但是核心 pythonunittest库与expectedFailure装饰器(扩展名 xe2x80x94 与 xe2x80x94 兼容)是否提供了您想要的行为nose

\n\n

有关使用示例,请参阅文档有关其实现的帖子

\n

  • 如果这就是问题所在,那么也许您需要 [`pytest`](http://pytest.org/latest/contents.html),它 [与 `nose`](http://pytest.org/latest 兼容) /nose.html),还支持[测试作为函数](http://pytest.org/latest/assert.html#asserting-with-the-assert-statement)并具有[`xfail`](http:// /pytest.org/latest/skipping.html#mark-a-test-function-as-expected-to-fail) 装饰器。 (2认同)
  • 根据我的经验,“unittest.expectedFailure”与 Nose *不*兼容。[鼻子错误 33](https://github.com/nose-devs/nose/issues/33) 同意。 (2认同)