Friday, 24 September 2021

Access parametrize parameter from other decorator

I am writing a PyTest plugin in which I implemented different custom markers using this function:

def pytest_configure(config):
    config.addinivalue_line("markers", "title(a_title): a title to present the test case")

These markers are then used to add custom properties for each test case in the jUnit output.

Now, since I find myself using very often the parametrize marker, I would like to fill part of my marker with the value of an argument defined in the parametrize marker.

Here is an example of what I am trying to accomplish. The code is the following:

class TestClass:
    @pytest.mark.parametrize("param", ["value1", "value2"])
    @pytest.mark.title(f"This test case checks that param is {param}")
    def test_simple_test_case(self, param):
        pass

I would like the XML output to be filled like this:

<testcase classname="test_example.TestClass" name="test_simple_test_case[value1]" time="0.001">
    <properties>
        <property name="title" value="This test case checks that param is value1"/>
    </properties>
</testcase>
<testcase classname="test_example.TestClass" name="test_simple_test_case[value2]" time="0.001">
    <properties>
        <property name="title" value="This test case checks that param is value2"/>
    </properties>
</testcase>
<testcase classname="test_example.TestClass" name="test_simple_test_case[value3]" time="0.001">
    <properties>
        <property name="title" value="This test case checks that param is value3"/>
    </properties>
</testcase>

Everything is working except passing the parametrize argument to my custom marker (title).



from Access parametrize parameter from other decorator

No comments:

Post a Comment