Add '--junit-suite-name' CLI option

This commit is contained in:
Dmitri Pribysh 2017-02-22 23:31:30 +03:00 committed by Bruno Oliveira
parent b6125d9a13
commit fe7d89f033
2 changed files with 37 additions and 3 deletions

View File

@ -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 <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)
@ -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'],

View File

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