Skip to content

Commit

Permalink
Add '--junit-suite-name' option for JUnit XML reports
Browse files Browse the repository at this point in the history
  • Loading branch information
dimp-gh committed Feb 22, 2017
1 parent 0a89db2 commit 8c359c0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
13 changes: 10 additions & 3 deletions _pytest/junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,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 <testsuite> 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)


Expand All @@ -255,10 +261,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',
Expand Down Expand Up @@ -384,7 +391,7 @@ def pytest_sessionfinish(self):
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'],
Expand Down
27 changes: 27 additions & 0 deletions testing/test_junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ def __init__(self):
self.option = self

junitprefix = None
junitsuitename = "pytest"
# XXX: shouldnt need tmpdir ?
xmlpath = str(tmpdir.join('junix.xml'))
register = gotten.append
Expand Down Expand Up @@ -981,3 +982,29 @@ class Report(BaseReport):
actual[k] = v

assert actual == expected


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")

0 comments on commit 8c359c0

Please sign in to comment.