From fe7d89f03370d087769c6093d7dac70f83160e0c Mon Sep 17 00:00:00 2001 From: Dmitri Pribysh Date: Wed, 22 Feb 2017 23:31:30 +0300 Subject: [PATCH] Add '--junit-suite-name' CLI option --- _pytest/junitxml.py | 13 ++++++++++--- testing/test_junitxml.py | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index 4bd334a16..5240e0e28 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -226,13 +226,19 @@ def pytest_addoption(parser): metavar="str", default=None, help="prepend prefix to classnames in junit-xml output") + group.addoption( + '--junitsuitename', '--junit-suite-name', + action="store", + metavar="name", + default="pytest", + help="set the name attribute of root tag") def pytest_configure(config): xmlpath = config.option.xmlpath # prevent opening xmllog on slave nodes (xdist) if xmlpath and not hasattr(config, 'slaveinput'): - config._xml = LogXML(xmlpath, config.option.junitprefix) + config._xml = LogXML(xmlpath, config.option.junitprefix, config.option.junitsuitename) config.pluginmanager.register(config._xml) @@ -259,10 +265,11 @@ def mangle_test_address(address): class LogXML(object): - def __init__(self, logfile, prefix): + def __init__(self, logfile, prefix, suite_name="pytest"): logfile = os.path.expanduser(os.path.expandvars(logfile)) self.logfile = os.path.normpath(os.path.abspath(logfile)) self.prefix = prefix + self.suite_name = suite_name self.stats = dict.fromkeys([ 'error', 'passed', @@ -422,7 +429,7 @@ class LogXML(object): logfile.write(Junit.testsuite( self._get_global_properties_node(), [x.to_xml() for x in self.node_reporters_ordered], - name="pytest", + name=self.suite_name, errors=self.stats['error'], failures=self.stats['failure'], skips=self.stats['skipped'], diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 7003d9f5f..417f8cca6 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -617,6 +617,7 @@ def test_dont_configure_on_slaves(tmpdir): self.option = self junitprefix = None + junitsuitename = "pytest" # XXX: shouldnt need tmpdir ? xmlpath = str(tmpdir.join('junix.xml')) register = gotten.append @@ -1032,3 +1033,29 @@ def test_url_property(testdir): test_case = minidom.parse(str(path)).getElementsByTagName('testcase')[0] assert (test_case.getAttribute('url') == test_url), "The URL did not get written to the xml" + + +def test_set_suite_name(testdir): + testdir.makepyfile(""" + import pytest + + def test_func(): + pass + """) + result, dom = runandparse(testdir, '--junit-suite-name', "my_suite") + assert result.ret == 0 + node = dom.find_first_by_tag("testsuite") + node.assert_attr(name="my_suite") + + +def test_set_suite_name_default(testdir): + testdir.makepyfile(""" + import pytest + + def test_func(): + pass + """) + result, dom = runandparse(testdir) + assert result.ret == 0 + node = dom.find_first_by_tag("testsuite") + node.assert_attr(name="pytest")