用于预期故障的鼻子插件

2024-09-29 23:19:01 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有可用的插件:

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

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

^{pr2}$

……但如果意外通过,则类似于失败,比如:

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

属于like SkipTest,但不是作为阻止测试运行的异常实现的。在

我唯一能找到的是this ticket关于支持unittest2expectedFailure修饰符(尽管我宁愿不使用unittest2,即使nose支持它)


Tags: 插件exampledefnotpluginspassassertnose
3条回答

您可以使用以下两种方法之一:

  1. ^{}装饰工

    from nose.tools import raises
    
    @raises(TypeError)
    def test_raises_type_error():
        raise TypeError("This test passes")
    
  2. nose.tools.assert_raises

    from nose.tools import assert_raises
    
    def test_raises_type_error():
        with assert_raises(TypeError):
            raise TypeError("This test passes")
    

如果不引发异常,测试将失败。在

是的,我知道,三年前问过:)

如果我误解了,请原谅,但是您所希望的行为不是由corepython的unittest库和expectedFailure修饰符一起提供的吗,它在扩展上与nose兼容?在

有关使用示例,请参见docs和a post about its implementation。在

我不知道nose插件,但是你可以很容易地编写你自己的decorator来完成。下面是一个简单的实现:

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

如果我运行这些测试:

^{pr2}$

我从nose得到以下输出:

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)

相关问题 更多 >

    热门问题