我的测试套件中的每个测试用例都有多个与之关联的属性,我希望将其包含在XML(junit-xml
)报告中。 以下代码片段给出了一个清晰的图片。自定义junitxml Pytest报告以添加自定义标记中的多个属性
@data(*get_csv_data("csv/blah.csv"))
@unpack
@pytest.mark.run(order=70)
@pytest.mark.webtest.with_args(jira="QA5555", second="second_arg_add")
def test_your_stuff(self,arg1, arg2):
# Actual Test
虽然定制报告,它很容易在pytest-html
插件中的pytest-html
extras
属性添加属性,通过添加参数,如下所示。
在conftest.py
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == 'call':
extra.append(pytest_html.extras.text("<blah>")
report.extra = extra
另外,我可以很容易地得到属性
item.keywords.get('webtest').kwargs
我如何可以做同样的junitxml
?
几个结论 -
junitxml
没有extras
record_xml_property
我不想使用它,因为它是在测试功能。同时添加多个参数与装饰看起来像一个不错的方法。我不想妨碍代码的可读性。def test_function(record_xml_property): record_xml_property("key", "value") assert 0
以下办法
if hasattr(request.config, "_xml"): request.config._xml.add_custom_property(name, value)
我没能在钩
junit-xml
这又没有与之相关的方法add_custom_property
这里得到request
对象保持def pytest_runtest_makereport(item, call):
的item.config._xml
点LogXML
它。
所以, 什么将更多的属性添加到junitxml
的最佳方式,以便它大致是这样的 -
<testcase classname="test_function" file="test_function.py" line="0" name="test_function" time="0.0009">
<properties>
<property name="jira" value="1234566" />
</properties>
</testcase>
或类似这样的
<testcase classname="test_function" file="test_function.py" line="0" name="test_function" time="0.0009" jira="2345667">
</testcase>