I want to test a Python function via Pytest. With the parametrize decorator I pass 2 expected passing and one expecting failing (via ValueError in the method).
@pytest.mark.parametrize("input_nr, expected", [
(123.456, (123, 456)),
(456.789, (456, 789)),
pytest.param("123.123", (123, 123), marks=pytest.mark.xfail(raises=ValueError, strict=True))]
)
def test_my_func(input_nr, expected):
res = my_func(input_nr)
print("\n" str(res))
assert res == expected
From the docs of pytest, I think to understod it right, I have to use pytest.param
for adding expected failing tests.
But if I run this in pytest, only the two passing runs are passed and the expected failing run was skipped/ignored.
What do I have to change in parametrize to fail successfully?
CodePudding user response:
xfail will not report a pass. So my test report won't be showing the expected result I hoped for.
So my way will be to handle expected expection in the test code, not via parametrize.
CodePudding user response:
If you want to test your function to fail for specific inputs then write a function that tests exactly those cases separately.
I needed to assume what your my_func
does, but it could look something like this:
import pytest
def my_func(num: float):
if not isinstance(num, float):
raise ValueError("Wrong format")
return tuple([int(n) for n in str(num).split(".")])
@pytest.mark.parametrize("input_nr, expected", [
(123.456, (123, 456)),
(456.789, (456, 789))
]
)
def test_my_func_valid(input_nr, expected):
"""Test function for valid input"""
res = my_func(input_nr)
print("\n" str(res))
assert res == expected
@pytest.mark.parametrize("input_nr, expected", [("123.123", (123, 123))])
def test_my_func_invalid(input_nr, expected):
"""Test if a ValueError is thrown"""
with pytest.raises(ValueError) as e:
my_func(input_nr)
assert str(e.value) == "Wrong format"