预期失败的鼻子插件

时间:2012-03-08 06:52:38

标签: python testing nose

是否有一个现有的插件可以像:

一样使用
@nose.plugins.expectedfailure
def not_done_yet():
    a = Thingamajig().fancynewthing()
    assert a == "example"

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

$ nosetests
...S..

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

=================================
UNEXPECTED PASS: not_done_yet
---------------------------------
-- >> begin captured stdout << --
Things and etc
...

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

我唯一能找到的就是this ticket支持unittest2 expectedFailure装饰器(虽然我不想使用unittest2,即使鼻子支持它)

3 个答案:

答案 0 :(得分: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

如果我运行这些测试:

@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)

答案 1 :(得分:4)

如果我误解了,请原谅我,但不是核心python的unittest库提供expectedFailure装饰器所提供的行为,即...与[{1}}扩展兼容?

有关使用示例,请参阅docspost about its implementation

答案 2 :(得分:1)

您可以通过以下两种方式之一来实现:

  1. nose.tools.raises装饰者

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let secondController = segue.destinationViewController as UISecondViewController
        // you can initialize some properties on second view controller instance before performing segue
    }
    
  2. from nose.tools import raises @raises(TypeError) def test_raises_type_error(): raise TypeError("This test passes")

    nose.tools.assert_raises
  3. 如果未引发异常,测试将失败。

    我知道,3年前问过:)