Description
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 -
junitxml
does not haveextras
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 multiplerecord_xml_property
calls in the test.
def test_function(record_xml_property):
record_xml_property("key", "value")
assert 0
- 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>