Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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 pytest.mark.Parameterize似乎只能发送预期的异常_Python_Exception_Pytest - Fatal编程技术网

Python pytest.mark.Parameterize似乎只能发送预期的异常

Python pytest.mark.Parameterize似乎只能发送预期的异常,python,exception,pytest,Python,Exception,Pytest,当参数化引发预期异常的测试时,我使用了以下形式: import pytest class SomeException(Exception): pass class AnotherException(Exception): pass @pytest.mark.parametrize( 'error', [None, pytest.mark.raises(SomeException('the message'),

当参数化引发预期异常的测试时,我使用了以下形式:

import pytest

class SomeException(Exception):
    pass

class AnotherException(Exception):
    pass

@pytest.mark.parametrize(
    'error',
    [None,
     pytest.mark.raises(SomeException('the message'),
                        exception=SomeException),
     pytest.mark.raises(AnotherException('the message'),
                        exception=AnotherException),
     pytest.mark.raises(Exception('the message'),
                        exception=Exception),
     ])
def test_mark_raises(error):
    if error:
        raise error
请注意,我已经安装了pytest包。好的,这是可行的,但我最近注意到它提出了一个弃用警告:

RemovedInPytest4Warning: Applying marks directly to parameters is deprecated, please use pytest.param(..., marks=...) instead.
好的,没关系。我们将更新参数化列表。但这一直是个问题。pytest.param有一个'marks='参数,但传入pytest.mark.raises无效。我发现在pytest.param中使用异常处理的唯一方法(实际上似乎完全有效)是:

import pytest

class SomeException(Exception):
    pass

class AnotherException(Exception):
    pass

@pytest.mark.parametrize(
    'error',
    [None,
     pytest.param(SomeException('the message'),
                  marks=pytest.mark.xfail(exception=SomeException)),
     pytest.param(AnotherException('the message'),
                  marks=pytest.mark.xfail(exception=AnotherException)),
     pytest.param(Exception('the message'),
                  marks=pytest.mark.xfail(exception=Exception)),
     ])
def test_mark_raises(error):
    if error:
        raise error
好吧,看来这确实起作用了。但它并没有通过测试,而是让他们失败了

我不喜欢这样。如果我期待一个测试会引发 特别的异常,它确实引发了异常, 然后我会考虑测试已经通过了,而不是“失败”。 如果我在检查一堆pytest结果(在某些情况下超过1500个), 识别哪些测试失败需要更多的工作 因为他们预计会出现某种失效情况,以及哪些失效情况 X失败是因为它们尚未实现(或其他原因) 这表明我们需要解决一些问题)


我不喜欢收到一大堆警告,但我也希望测试能够正确地显示它们通过了。是否有人知道在预期异常行为的情况下使用pytest.param的正确构造?

如果要禁用输出中的警告,可以使用--disable warnings标志

当您不希望测试失败时,您正在使用xfail引发异常。您应该使用pytest.raises来引发异常-。如果您想使用标记引发异常,您应该使用自定义标记并编写自己的测试收集器。 conftest.py的示例:

import pytest

def pytest_runtest_setup(item):
    envmarker = item.get_marker("passwithex")
    if envmarker is not None:
        pytest.raises(ErrorName)
测试代码:

import pytest

class SomeException(Exception):
    pass

class AnotherException(Exception):
    pass

@pytest.mark.parametrize(
    'error',
    [None,
     pytest.param(SomeException('the message'),
                  marks=pytest.mark.passwithex),
     pytest.param(AnotherException('the message'),
                  marks=pytest.mark.xfail(exception=AnotherException)),
     pytest.param(Exception('the message'),
                  marks=pytest.mark.xfail(exception=Exception)),
     ])
def test_mark_raises(error):
    if error:
        raise error

我不确定是回答我自己的问题还是简单地更新问题。由于答案有点冗长,我选择把它放在这里

pytest 0.10版现在支持我所寻找的行为。现在,您可以通过以下方式设置异常参数:

import pytest

class SomeException(Exception):
    pass

@pytest.mark.parametrize(
    'error',
    [None,
     pytest.param(SomeException('the message'),
                  marks=pytest.mark.raises(exception=SomeException)),
     ])
def test_mark_raises(error):
    if error:
        raise error

这比以前接受的将标记直接放入参数化列表的做法要详细一些,但它的工作原理与预期一样,而且弃用警告也消失了。

感谢您的帮助。虽然不是完全正确的答案,但它确实提供了一些急需的见解(将我的答案发布在下面,因为它有点冗长)。我只想提一提passwithex方法对我来说不太管用。看起来pytest.raises(ErrorName)返回了一个RaiseContext对象,但显然没有对它做任何处理。因此,测试函数中引发的异常仍然失败。也许是我错过了什么?再次感谢您抽出时间。