[GRASS-SVN] r61146 - sandbox/wenzeslaus/gunittest

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jul 3 19:49:13 PDT 2014


Author: wenzeslaus
Date: 2014-07-03 19:49:13 -0700 (Thu, 03 Jul 2014)
New Revision: 61146

Modified:
   sandbox/wenzeslaus/gunittest/invoker.py
   sandbox/wenzeslaus/gunittest/reporters.py
Log:
gunittest: refactoring and usable HTML report for main invoker (table in main index, failed info)

Modified: sandbox/wenzeslaus/gunittest/invoker.py
===================================================================
--- sandbox/wenzeslaus/gunittest/invoker.py	2014-07-04 01:26:39 UTC (rev 61145)
+++ sandbox/wenzeslaus/gunittest/invoker.py	2014-07-04 02:49:13 UTC (rev 61146)
@@ -5,7 +5,6 @@
 import shutil
 import string
 import subprocess
-import datetime
 
 from unittest.main import TestProgram, USAGE_AS_MAIN
 TestProgram.USAGE = USAGE_AS_MAIN
@@ -81,6 +80,7 @@
         stdout = open(stdout_path, 'w')
         stderr = open(stderr_path, 'w')
 
+        self.reporter.start_file_test(module)
         # TODO: we might clean the directory here before test if non-empty
         # TODO: use some grass function to run?
         p = subprocess.Popen([sys.executable, module.abs_file_path],
@@ -89,9 +89,9 @@
         returncode = p.wait()
         stdout.close()
         stderr.close()
-        self.reporter.add_test_results(module=module, cwd=cwd,
-                                       returncode=returncode,
-                                       stdout=stdout_path, stderr=stderr_path)
+        self.reporter.end_file_test(module=module, cwd=cwd,
+                                    returncode=returncode,
+                                    stdout=stdout_path, stderr=stderr_path)
         # TODO: add some try-except or with for better error handling
         os.remove(gisrc)
         # TODO: only if clean up

Modified: sandbox/wenzeslaus/gunittest/reporters.py
===================================================================
--- sandbox/wenzeslaus/gunittest/reporters.py	2014-07-04 01:26:39 UTC (rev 61145)
+++ sandbox/wenzeslaus/gunittest/reporters.py	2014-07-04 02:49:13 UTC (rev 61146)
@@ -23,10 +23,16 @@
         # TODO: use CSS class
         # ignoring the issue with \n at the end, HTML don't mind
         line = '<span style="color: red">' + line + "</span>"
+    if line.startswith('FAIL: '):
+        # TODO: use CSS class
+        # ignoring the issue with \n at the end, HTML don't mind
+        line = '<span style="color: red">' + line + "</span>"
     if line.startswith('WARNING: '):
         # TODO: use CSS class
         # ignoring the issue with \n at the end, HTML don't mind
         line = '<span style="color: blue">' + line + "</span>"
+    #if line.startswith('Traceback ('):
+    #    line = '<span style="color: red">' + line + "</span>"
     return line
 
 
@@ -43,47 +49,82 @@
         self.main_index.write('<html><body>'
                               '<h1>Test results</h1>'
                               '{time:%Y-%m-%d %H:%M:%S}'
-                              '<ul>'.format(time=self.main_start_time))
+                              '<table>'
+                              '<thead><tr>'
+                              '<th>Tested directory</th>'
+                              '<th>Test file</th>'
+                              '<th>Status</th>'
+                              '</tr></thead><tbody>'.format(
+                                  time=self.main_start_time))
+        self.file_start_time = None
+        self._start_file_test_called = False
 
     def finish(self):
-        self.main_index.write('</ul>'
+        self.main_index.write('<tbody></table>'
                               '</body></html>')
         self.main_index.close()
 
-    def add_test_results(self, module, cwd, returncode, stdout, stderr):
-        self.main_index.write(
-            '<li><a href="{d}/{m}/index.html">{d}/{m}</a>'.format(
-                d=module.tested_dir, m=module.name))
-        # TODO: we don't do any HTML escaping, use txt file
-        stdout_html = open(os.path.join(cwd, 'stdout.html'), 'w')
-        stderr_html = open(os.path.join(cwd, 'stderr.html'), 'w')
-        stdout_html.write('<html><body><h1>%s</h1><pre>' % (module.name + ' stdout'))
-        stderr_html.write('<html><body><h1>%s</h1><pre>' % (module.name + ' stderr'))
+    def start_file_test(self, module):
+        self.file_start_time = datetime.datetime.now()
+        self._start_file_test_called = True
 
-        with open(stdout) as text:
+    def wrap_stdstream_to_html(self, infile, outfile, module, stream):
+        before = '<html><body><h1>%s</h1><pre>' % (module.name + ' ' + stream)
+        after = '</pre></body></html>'
+        html = open(outfile, 'w')
+        html.write(before)
+        with open(infile) as text:
             for line in text:
-                stdout_html.write(color_error_line(html_escape(line)))
-        with open(stderr) as text:
-            for line in text:
-                stderr_html.write(color_error_line(html_escape(line)))
+                html.write(color_error_line(html_escape(line)))
+        html.write(after)
+        html.close()
 
-        stdout_html.write('</pre></body></html>')
-        stderr_html.write('</pre></body></html>')
-        stdout_html.close()
-        stderr_html.close()
+    def returncode_to_html_text(self, returncode):
+        if returncode:
+            return '<span style="color: red">FAILED</span>'
+        else:
+            return '<span style="color: green">succeeded</span>'  # SUCCEEDED
 
+    def returncode_to_html_sentence(self, returncode):
+        if returncode:
+            return '<span style="color: red">&#x274c;</span> Test failed (return code %d)' % (returncode)
+        else:
+            return '<span style="color: green">&#x2713;</span> Test succeeded (return code %d)' % (returncode)
+
+    def end_file_test(self, module, cwd, returncode, stdout, stderr):
+        assert self._start_file_test_called
+        file_time = datetime.datetime.now() - self.file_start_time
+        self.main_index.write(
+            '<tr><td>{d}</td>'
+            '<td><a href="{d}/{m}/index.html">{m}</a></td><td>{sf}</td>'
+            '<tr>'.format(
+                d=module.tested_dir, m=module.name,
+                sf=self.returncode_to_html_text(returncode)))
+        self.wrap_stdstream_to_html(infile=stdout,
+                                    outfile=os.path.join(cwd, 'stdout.html'),
+                                    module=module, stream='stdout')
+        self.wrap_stdstream_to_html(infile=stderr,
+                                    outfile=os.path.join(cwd, 'stderr.html'),
+                                    module=module, stream='stderr')
         file_index_path = os.path.join(cwd, 'index.html')
         file_index = open(file_index_path, 'w')
         file_index.write('<html><body>'
+                         '<h1>{m.name}</h1>'
+                         '<h2>{m.tested_dir} – {m.name}</h2>'
+                         '<p>{status}'
+                         '<p>Test duration: {dur}'
                          '<ul>'
                          '<li><a href="stdout.html">standard output (stdout)</a>'
                          '<li><a href="stderr.html">standard error output (stderr)</a>'
                          '<li><a href="testcodecoverage/index.html">code coverage</a>'
                          '</ul>'
-                         '</body></html>')
+                         '</body></html>'.format(
+                             dur=file_time, m=module,
+                             status=self.returncode_to_html_sentence(returncode)))
         file_index.close()
 
         if returncode:
             sys.stderr.write('{d}/{m} failed (see {f})\n'.format(d=module.tested_dir,
                                                                  m=module.name,
                                                                  f=file_index_path))
+        self._start_file_test_called = False



More information about the grass-commit mailing list