2017-10-11 86 views
2

我的测试套件中的每个测试用例都有多个与之关联的属性,我希望将其包含在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-htmlextras属性添加属性,通过添加参数,如下所示。

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

几个结论 -

  1. junitxml没有extras
  2. record_xml_property我不想使用它,因为它是在测试功能。同时添加多个参数与装饰看起来像一个不错的方法。我不想妨碍代码的可读性。

    def test_function(record_xml_property): 
        record_xml_property("key", "value") 
        assert 0 
    
  3. 以下办法

    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._xmlLogXML它。

所以, 什么将更多的属性添加到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> 

回答

0

所以,看起来没有简单的方法来实现这一点。目前不支持pytest。这目前标记为feature request