From 4c2736f7f1a996a2622aa7918ab0cb7ec4b3e148 Mon Sep 17 00:00:00 2001 From: Tareq Alayan Date: Mon, 29 Feb 2016 16:14:23 +0200 Subject: [PATCH] junitxml: add properties node in testsuite level The commit allow users to add a properties node in testsuite level see example below: ') + logfile.write(Junit.testsuite( + self._get_global_properties_node(), [x.to_xml() for x in self.node_reporters_ordered], name="pytest", errors=self.stats['error'], @@ -374,3 +380,18 @@ def pytest_sessionfinish(self): def pytest_terminal_summary(self, terminalreporter): terminalreporter.write_sep("-", "generated xml file: %s" % (self.logfile)) + + def add_global_property(self, name, value): + self.global_properties.append((str(name), bin_xml_escape(value))) + + def _get_global_properties_node(self): + """Return a Junit node containing custom properties, if any. + """ + if self.global_properties: + return Junit.properties( + [ + Junit.property(name=name, value=value) + for name, value in self.global_properties + ] + ) + return '' diff --git a/doc/en/usage.rst b/doc/en/usage.rst index 4b92fd1e150..adf97c067fc 100644 --- a/doc/en/usage.rst +++ b/doc/en/usage.rst @@ -182,6 +182,47 @@ This will add an extra property ``example_key="1"`` to the generated +LogXML: add_global_property +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. versionadded:: 2.10 + +If you want to add a properties node in the testsuite level, which may contains properties that are relevant +to all testcases you can use ``LogXML.add_global_properties`` +Note that this feature will be available on v2.10.1. + +.. code-block:: python + + import pytest + + @pytest.fixture(scope="session") + def log_global_env_facts(f): + + if pytest.config.pluginmanager.hasplugin('junitxml'): + my_junit = getattr(pytest.config, '_xml', None) + + my_junit.add_global_property('ARCH', 'PPC') + my_junit.add_global_property('STORAGE_TYPE', 'CEPH') + + @pytest.mark.usefixtures(log_global_env_facts) + def start_and_prepare_env(): + pass + + class TestMe: + def test_foo(self): + assert True + +This will add a property node below the testsuite node to the generated xml: +.. code-block:: xml + + + + + + + + + .. warning:: This is an experimental feature, and its interface might be replaced diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 99c59cb7a0b..8ff1028df98 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -783,3 +783,38 @@ def test_pass(): u'test_fancy_items_regression test_pass' u' test_fancy_items_regression.py', ] + + +def test_global_properties(testdir): + path = testdir.tmpdir.join("test_global_properties.xml") + log = LogXML(str(path), None) + from _pytest.runner import BaseReport + + class Report(BaseReport): + sections = [] + nodeid = "test_node_id" + + log.pytest_sessionstart() + log.add_global_property('foo', 1) + log.add_global_property('bar', 2) + log.pytest_sessionfinish() + + dom = minidom.parse(str(path)) + + properties = dom.getElementsByTagName('properties') + + assert (properties.length == 1), "There must be one node" + + property_list = dom.getElementsByTagName('property') + + assert (property_list.length == 2), "There most be only 2 property nodes" + + expected = {'foo': '1', 'bar': '2'} + actual = {} + + for p in property_list: + k = str(p.getAttribute('name')) + v = str(p.getAttribute('value')) + actual[k] = v + + assert actual == expected