[Piglit] [PATCH 1/3] junit.py: Refactor write_test a little

Dylan Baker baker.dylan.c at gmail.com
Thu Oct 2 16:59:59 PDT 2014


This patch splits some of the code into a helper function. This makes it
a lot easier to read and work with.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/backends/junit.py | 77 ++++++++++++++++++++++++---------------------
 1 file changed, 41 insertions(+), 36 deletions(-)

diff --git a/framework/backends/junit.py b/framework/backends/junit.py
index 09faea5..6115e34 100644
--- a/framework/backends/junit.py
+++ b/framework/backends/junit.py
@@ -104,6 +104,46 @@ class JUnitBackend(FileBackend):
         shutil.rmtree(os.path.join(self._dest, 'tests'))
 
     def write_test(self, name, data):
+
+        def calculate_result():
+            """Set the result."""
+            expected_result = "pass"
+
+            # replace special characters and make case insensitive
+            lname = (classname + "." + testname).lower()
+            lname = lname.replace("=", ".")
+            lname = lname.replace(":", ".")
+
+            if lname in self._expected_failures:
+                expected_result = "failure"
+                # a test can either fail or crash, but not both
+                assert( lname not in self._expected_crashes )
+
+            if lname in self._expected_crashes:
+                expected_result = "error"
+
+            # Add relevant result value, if the result is pass then it doesn't
+            # need one of these statuses
+            if data['result'] == 'skip':
+                etree.SubElement(element, 'skipped')
+
+            elif data['result'] in ['warn', 'fail', 'dmesg-warn', 'dmesg-fail']:
+                if expected_result == "failure":
+                    err.text += "\n\nWARN: passing test as an expected failure"
+                else:
+                    etree.SubElement(element, 'failure')
+
+            elif data['result'] == 'crash':
+                if expected_result == "error":
+                    err.text += "\n\nWARN: passing test as an expected crash"
+                else:
+                    etree.SubElement(element, 'error')
+
+            elif expected_result != "pass":
+                err.text += "\n\nERROR: This test passed when it "\
+                            "expected {0}".format(expected_result)
+                etree.SubElement(element, 'failure')
+
         # Split the name of the test and the group (what junit refers to as
         # classname), and replace piglits '/' separated groups with '.', after
         # replacing any '.' with '_' (so we don't get false groups). Also
@@ -119,21 +159,6 @@ class JUnitBackend(FileBackend):
         # set different root names.
         classname = 'piglit.' + classname
 
-        expected_result = "pass"
-
-        # replace special characters and make case insensitive
-        lname = (classname + "." + testname).lower()
-        lname = lname.replace("=", ".")
-        lname = lname.replace(":", ".")
-
-        if lname in self._expected_failures:
-            expected_result = "failure"
-            # a test can either fail or crash, but not both
-            assert( lname not in self._expected_crashes )
-
-        if lname in self._expected_crashes:
-            expected_result = "error"
-
         # Create the root element
         element = etree.Element('testcase', name=testname + self._test_suffix,
                                 classname=classname,
@@ -148,27 +173,7 @@ class JUnitBackend(FileBackend):
         err = etree.SubElement(element, 'system-err')
         err.text = data['err']
 
-        # Add relevant result value, if the result is pass then it doesn't need
-        # one of these statuses
-        if data['result'] == 'skip':
-            etree.SubElement(element, 'skipped')
-
-        elif data['result'] in ['warn', 'fail', 'dmesg-warn', 'dmesg-fail']:
-            if expected_result == "failure":
-                err.text += "\n\nWARN: passing test as an expected failure"
-            else:
-                etree.SubElement(element, 'failure')
-
-        elif data['result'] == 'crash':
-            if expected_result == "error":
-                err.text += "\n\nWARN: passing test as an expected crash"
-            else:
-                etree.SubElement(element, 'error')
-
-        elif expected_result != "pass":
-            err.text += "\n\nERROR: This test passed when it "\
-                        "expected {0}".format(expected_result)
-            etree.SubElement(element, 'failure')
+        calculate_result()
 
         t = os.path.join(self._dest, 'tests',
                          '{}.xml'.format(self._counter.next()))
-- 
2.1.1



More information about the Piglit mailing list