如何使用pytest xfail断言异常消息(对异常类型的断言有效)

时间:2020-05-20 14:42:07

标签: python testing pytest

假设我有一个文件test_scratch_2.py


import pytest


def my_fun(number: int):
    if number == 1:
        raise ValueError("Some Message for number 1")

    if number == 2:
        raise ValueError("Some Message for number 2")

    if number == 3:
        return int("number")

    return number ** 2


@pytest.mark.parametrize("digit", [
    # example 1
    pytest.param(1, id="first",
                 marks=pytest.mark.xfail(raises=ValueError,
                                         strict=True,
                                         reason="Some Message for number 1")),
    # example 2
    pytest.param(2, id="second",
                 marks=pytest.mark.xfail(raises=ValueError,
                                         strict=True,
                                         reason="Some Message for number 1")),
    pytest.param(3, id="third",
                 marks=pytest.mark.xfail(raises=ValueError,
                                         strict=True,
                                         reason="Some Message for number xxxxxxxxx")),

    4,
    5,
    60000
])
def test_my_fun(digit):
    assert my_fun(digit) > 0

然后我运行测试

> pytest test_scratch_2.py -vvv                                                                                                                                                                                                
======================= test session starts ============================
platform linux -- Python 3.7.5, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 --.../bin/python3
cachedir: .pytest_cache
rootdir:....config/JetBrains/PyCharm2020.1/scratches
collected 6 items                                                                                                                                                                                                             

test_scratch_2.py::test_my_fun[first] XFAIL                                                                                                                                                                             [ 16%]
test_scratch_2.py::test_my_fun[second] XFAIL                                                                                                                                                                            [ 33%]
test_scratch_2.py::test_my_fun[third] XFAIL                                                                                                                                                                             [ 50%]
test_scratch_2.py::test_my_fun[4] PASSED                                                                                                                                                                                [ 66%]
test_scratch_2.py::test_my_fun[5] PASSED                                                                                                                                                                              [ 83%]
test_scratch_2.py::test_my_fun[60000] PASSED   

test_scratch_2.py::test_my_fun[third] XFAIL的情况下存在错误。

它失败,但出现预期的异常,但不是出现预期的原因。

所有断言均基于异常类型,而不基于异常消息。

如何检查异常消息?

0 个答案:

没有答案
相关问题