Parametrized tests in pytest with varying marks for different test functions

Viewed 108

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?

2 Answers

I had to discover that the answer to my problem is relatively simple. The pytest.param mechanism allows to specify marks for specific test cases:

@pytest.mark.parametrize(
    TESTCASES[0],
    [
        pytest.param(*args, marks=marks)
        for args, marks
        in zip(TESTCASES[1], [[], [pytest.mark.xfail(reason=AssertionError)]])
    ],
    ids=["int_pass", "str_fail"],
    )
def test_sum_int(self, a, b, result):
    assert sum_int(a, b) == result
Related