'How to mark individual parameterized tests with a marker?

I have been trying to parameterize my tests using @pytest.mark.parametrize, and I have a marketer @pytest.mark.test("1234"), I use the value from the test marker to do post the results to JIRA. Note the value given for the marker changes for every test_data. Essentially the code looks something like below.

@pytest.mark.foo
@pytest.mark.parametrize(("n", "expected"),[
    (1, 2),
    (2, 3)])
def test_increment(n, expected):
     assert n + 1 == expected

I want to do something like

@pytest.mark.foo
@pytest.mark.parametrize(("n", "expected"), [
    (1, 2,@pytest.mark.test("T1")),
    (2, 3,@pytest.mark.test("T2"))
])

How to add the marker when using parameterized tests given that the value of the marker is expected to change with each test?



Solution 1:[1]

It's explained here in the documentation: https://docs.pytest.org/en/stable/example/markers.html#marking-individual-tests-when-using-parametrize

To show it here as well, it'd be:

@pytest.mark.foo
@pytest.mark.parametrize(("n", "expected"), [
    pytest.param(1, 2, marks=pytest.mark.T1),
    pytest.param(2, 3, marks=pytest.mark.T2),
    (4, 5)
])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pavelsaman