从其他装饰器访问 parametrize 参数
Access parametrize parameter from other decorator
我正在编写一个 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
) 外,一切正常。
您可以访问 item.callspec.params
中的 parametrize
个参数。
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))
我正在编写一个 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
) 外,一切正常。
您可以访问 item.callspec.params
中的 parametrize
个参数。
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))