Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于预期故障的nose插件_Python_Testing_Nose - Fatal编程技术网

Python 用于预期故障的nose插件

Python 用于预期故障的nose插件,python,testing,nose,Python,Testing,Nose,是否有可以使用的现有插件,如: @nose.plugins.expectedfailure def not_done_yet(): a = Thingamajig().fancynewthing() assert a == "example" 如果测试失败,它将显示为跳过的测试: $ nosetests ...S.. …但如果意外通过,它将类似于失败,可能类似于: ================================= UNEXPECTED PASS: not_d

是否有可以使用的现有插件,如:

@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
...
=================================
意外通过:尚未完成
---------------------------------

-->>开始捕获stdout我不知道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
如果我运行这些测试:

@expected_failure
def test_not_implemented():
    assert False

@expected_failure
def test_unexpected_success():
    assert True
我从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)

如果我误解了,请原谅,但是核心python的
unittest
库提供的
expectedFailure
decorator(扩展后与
nose
兼容)不就是您想要的行为吗


有关使用示例,请参见和。

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

  • 装饰师

    from nose.tools import raises
    
    @raises(TypeError)
    def test_raises_type_error():
        raise TypeError("This test passes")
    
  • nose.tools.assert\u

    from nose.tools import assert_raises
    
    def test_raises_type_error():
        with assert_raises(TypeError):
            raise TypeError("This test passes")
    
  • 如果未引发异常,测试将失败


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

    哦,当然!只需稍加调整即可在测试失败时提高SkipTest,这很完美-谢谢\o/是的,这是真的,但我喜欢nose的一点是它能够将测试作为函数编写,而不是像内置unittest模块所要求的那样在子类上编写方法(例如
    def test\u exampleblah():pass
    )如果这是问题所在,那么您可能希望,也就是说,还支持并拥有decorator。根据我的经验,
    unittest.expectedFailure
    与Nose不兼容。同意“与鼻子不兼容”。在2018年,对我来说似乎足够兼容。为什么会被否决?这是最好的答案。