Skip to content

Customize junitxml Pytest report to add multiple attributes from custom marker #2827

Open
@utkjad

Description

@utkjad

Using Pytest==3.2.3 on Ubuntu 16.04
Suggestion, bug?, enhancement,"How-do-I-do-this-in-pytest"

Each test case in my test suite has multiple attributes associated with it that I wish to include in XML(junit-xml) report. Following code snippet gives a clear picture about it.

@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 

While customizing reports, it is easy to add attributes in pytest-html plugin, by adding parameters in extras attribute of pytest-html Following code snippet gives clear picture about it.

# 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

Also, I can easily get the attributes by following item.keywords.get('webtest').kwargs How can I do the same for junitxml?

A few findings -

  1. junitxml does not have extras
  2. record_xml_property - I do not want to use it as it is a feature in test phase. Also adding multiple arguments with decorator looks like a nice approach. I do not want to hamper code readability by adding multiple record_xml_property calls in the test.
def test_function(record_xml_property):
    record_xml_property("key", "value")
    assert 0
  1. The following approach
if hasattr(request.config, "_xml"):
request.config._xml.add_custom_property(name, value)

I was not able to get hold of request object here in the hook
def pytest_runtest_makereport(item, call):
The item.config._xml points to LogXML in junit-xml which in turn has no method add_custom_property associated with it.
So, What would be the best way to add more attributes to junitxml, so that it would roughly look like this -

<testcase classname="test_function" file="test_function.py" line="0" 
          name="test_function" time="0.0009">
  <properties>
    <property name="jira" value="1234566" />
  </properties>
</testcase>

or like this

<testcase classname="test_function" file="test_function.py" line="0"
          name="test_function" time="0.0009" jira="2345667">
</testcase>

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: junitxmlrelated to the junitxml builtin plugintype: enhancementnew feature or API change, should be merged into features branchtype: feature-branchnew feature or API change, should be merged into features branch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions