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

标签 python pytest

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

def pytest_configure(config):
    config.addinivalue_line("markers", "title(a_title): a title to present the test case")
然后使用这些标记为 jUnit 输出中的每个测试用例添加自定义属性。
现在,因为我发现自己经常使用 parametrize marker ,我想用参数化标记中定义的参数值填充标记的一部分。
这是我试图完成的一个例子。代码如下:
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
我希望像这样填充 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>
除了通过 parametrize 之外,一切正常我的自定义标记的参数 ( title )。

最佳答案

您可以访问 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))
用法:
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

处理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))
引用:https://stackoverflow.com/questions/17215400/format-string-unused-named-arguments/17215533#17215533

关于python - 从其他装饰器访问参数化参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68228156/

相关文章:

Python - Dash 从 Excel 文件创建堆积条形图

python - selenium 中是否有任何方法可以获取文本为 "Link"或简单文本

python - 如何修复Python中的 'TypeError: argument of type '函数“不可迭代”

python - 使用单独的测试文件运行 pytest

python - 如何在不创建 conn.cursor() 的情况下检查/打印 psycopg2 动态查询 Compose

python - 如何使用选择器获取第二个和第三个元素?

python - 从函数返回多个值的最佳方法是什么?

python - 如何让 PyC​​harm 显示来自 pytest 的整个错误差异?

python - pytest:如何测试项目相关的目录创建?

python - 使用 Jenkins 中的管道执行 pytest