从其他装饰器访问参数化参数

dar*_*fac 7 python pytest

我正在编写一个 PyTest 插件,其中使用此函数实现了不同的自定义标记:

def pytest_configure(config):
    config.addinivalue_line("markers", "title(a_title): a title to present the test case")
Run Code Online (Sandbox Code Playgroud)

然后,使用这些标记为 jUnit 输出中的每个测试用例添加自定义属性。

现在,由于我发现自己经常使用parametrize标记,因此我想用参数化标记中定义的参数值填充标记的一部分。

这是我想要实现的目标的一个例子。代码如下:

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
Run Code Online (Sandbox Code Playgroud)

我希望 XML 输出像这样填充:

<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>
Run Code Online (Sandbox Code Playgroud)

parametrize除了将参数传递给我的自定义标记 ( )之外,一切正常title

aar*_*ron 4

您可以访问parametrize中的参数item.callspec.params

def pytest_collection_modifyitems(session, config, items):
    for item in items:
        for marker in item.iter_markers(name="title"):
            # title = marker.args[0]                               # Change this
            title = marker.args[0].format(**item.callspec.params)  # to this
            item.user_properties.append(("title", title))
Run Code Online (Sandbox Code Playgroud)

用法:

class TestClass:
    @pytest.mark.parametrize("param", ["value1", "value2"])
    # @pytest.mark.title(f"This test case checks that param is {param}")  # Change this
    @pytest.mark.title("This test case checks that param is {param}")     # to this
    def test_simple_test_case(self, param):
        pass
Run Code Online (Sandbox Code Playgroud)

处理parametrize未使用 或未指定某些格式字符串的情况:

class SafeDict(dict):
    def __missing__(self, key):
        return "{" + key + "}"


def pytest_collection_modifyitems(session, config, items):
    for item in items:
        for marker in item.iter_markers(name="title"):
            title = marker.args[0]
            if hasattr(item, "callspec"):
                title = title.format_map(SafeDict(item.callspec.params))
            item.user_properties.append(("title", title))
Run Code Online (Sandbox Code Playgroud)

参考:/sf/ask/1205078031/#17215533