I am currently trying to use pytest's parameterization feature in the following context:
I have multiple functions that should be tested with a universal set of test cases. Depending on the tested function, the same test case should either pass or xfail. I came up with a silly example to illustrate this:
import pytest
# Functions to test
def sum_int(a, b):
assert isinstance(a, int)
assert isinstance(b, int)
return a + b
def sum_any(a, b):
return a + b
# Universal test cases
TESTCASES = [
"a,b,result", [
(1, 1, 2),
("a", "a", "aa")
]
]
class Tests:
@pytest.mark.parametrize(*TESTCASES, ids=["int_pass", "str_fail"])
def test_sum_int(self, a, b, result):
assert sum_int(a, b) == result
@pytest.mark.parametrize(*TESTCASES, ids=["int_pass", "str_pass"])
def test_sum_any(self, a, b, result):
assert sum_any(a, b) == result
Unfortunately, it seems not to be possible to just pass additional marks (like pytest.mark.xfail(reason=AssertionError)
to parametrize()
like it can be done with IDs.
# Does not work
@pytest.mark.parametrize(*TESTCASES,
ids=["int_pass", "str_fail"],
marks=[None, pytest.mark.xfail(reason=AssertionError)])
def test_sum_int(self, a, b, result):
assert sum_int(a, b) == result
What could be a good way to achieve this?