我正在编写一个 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/