[GRASS-SVN] r61394 - in grass/trunk/lib/python/gunittest: . testsuite/data/samplecode/submodule_errors/subsubmodule_errors/testsuite testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite testsuite/data/samplecode/submodule_test_fail/testsuite testsuite/data/samplecode/testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jul 24 18:53:39 PDT 2014


Author: wenzeslaus
Date: 2014-07-24 18:53:39 -0700 (Thu, 24 Jul 2014)
New Revision: 61394

Added:
   grass/trunk/lib/python/gunittest/testsuite/data/samplecode/testsuite/test_good_and_bad.py
   grass/trunk/lib/python/gunittest/testsuite/data/samplecode/testsuite/test_python_unittest.py
Modified:
   grass/trunk/lib/python/gunittest/reporters.py
   grass/trunk/lib/python/gunittest/runner.py
   grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_errors/testsuite/test_error.py
   grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_errors/testsuite/test_import_error.py
   grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_gfatalerror.py
   grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_osexit_one.py
   grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_osexit_zero.py
   grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_segfaut.py
   grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_sysexit_one.py
   grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_sysexit_zero.py
   grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_test_fail/testsuite/test_fail.py
   grass/trunk/lib/python/gunittest/testsuite/data/samplecode/testsuite/test_success.py
Log:
gunittest: count (and store) successful tests to get the right number; use gunittest for gunittest's test data

Modified: grass/trunk/lib/python/gunittest/reporters.py
===================================================================
--- grass/trunk/lib/python/gunittest/reporters.py	2014-07-25 00:16:19 UTC (rev 61393)
+++ grass/trunk/lib/python/gunittest/reporters.py	2014-07-25 01:53:39 UTC (rev 61394)
@@ -342,7 +342,8 @@
         if returncode:
             return '<span style="color: red">FAILED</span>'
         else:
-            return '<span style="color: green">succeeded</span>'  # SUCCEEDED
+            # alternatives: SUCCEEDED, passed, OK
+            return '<span style="color: green">succeeded</span>'
 
     def returncode_to_html_sentence(self, returncode):
         if returncode:
@@ -368,18 +369,18 @@
         # TODO: add success counter to GrassTestResult base class
         skipped = test_summary.get('skipped', 0)
         expected_failures = test_summary.get('expected_failures', 0)
-        unexpected_success = test_summary.get('unexpected_success', 0)
+        unexpected_successes = test_summary.get('unexpected_successes', 0)
+        successes = test_summary.get('successes', 0)
 
         self.failures += failures
         self.errors += errors
         self.skiped += skipped
         self.expected_failures += expected_failures
-        self.unexpected_success += unexpected_success
+        self.unexpected_success += unexpected_successes
 
         if total is not None:
             # success are only the clear ones
             # percentage is influenced by all but putting only failures to table
-            successes = total - failures - errors - skipped - expected_failures - unexpected_success
             self.successes += successes
             self.total += total
 

Modified: grass/trunk/lib/python/gunittest/runner.py
===================================================================
--- grass/trunk/lib/python/gunittest/runner.py	2014-07-25 00:16:19 UTC (rev 61393)
+++ grass/trunk/lib/python/gunittest/runner.py	2014-07-25 01:53:39 UTC (rev 61394)
@@ -18,7 +18,7 @@
 import sys
 import time
 
-from unittest.result import TestResult
+import unittest.result
 from unittest.signals import registerResult
 
 __unittest = True
@@ -40,6 +40,29 @@
         self.write('\n') # text-mode streams translate to \r\n if needed
 
 
+class TestResult(unittest.result.TestResult):
+    # descriptions and verbosity unused
+    # included for compatibility with unittest's TestResult
+    # where are also unused, so perhaps we can remove them
+    # stream set to None and not included in interface, it would not make sense
+    def __init__(self, stream=None, descriptions=None, verbosity=None):
+        super(TestResult, self).__init__(
+            stream=stream, descriptions=descriptions, verbosity=verbosity)
+        self.successes = []
+
+    def addSuccess(self, test):
+        super(TestResult, self).addSuccess(test)
+        self.successes.append(test)
+
+    # TODO: better would be to pass start at the beginning
+    # alternative is to leave counting time on class
+    # TODO: document: we expect all grass classes to have setTimes
+    # TODO: alternatively, be more forgiving for non-unittest methods
+    def setTimes(self, start_time, end_time, time_taken):
+        pass
+        # TODO: implement this
+
+
 class TextTestResult(TestResult):
     """A test result class that can print formatted text results to a stream.
 
@@ -231,17 +254,20 @@
 #            'date={start_datetime}\n'
 #            'date={end_datetime}\n'
 
+        failed, errored = map(len, (self.failures, self.errors))
+        succeeded = len(self.successes)
         results = map(len, (self.expectedFailures,
                             self.unexpectedSuccesses,
                             self.skipped))
         expectedFails, unexpectedSuccesses, skipped = results
 
-        infos.append("status=%s" % ('failed' if self.wasSuccessful() else 'passed'))
+        status = 'succeeded' if self.wasSuccessful() else 'failed'
+        infos.append("status=%s" % status)
+        infos.append("total=%d" % (run))
 
-        infos.append("total=%d" % (run))
-        failed, errored = map(len, (self.failures, self.errors))
         infos.append("failures=%d" % failed)
         infos.append("errors=%d" % errored)
+        infos.append("successes=%d" % succeeded)
         infos.append("skipped=%d" % skipped)
 
         # TODO: document this: if not supported by view,
@@ -314,7 +340,7 @@
         super(MultiTestResult, self).addError(test, err)
         for result in self._results:
             try:
-                result.addSuccess(test)
+                result.addError(test, err)
             except AttributeError:
                 if self._forgiving:
                     pass
@@ -325,7 +351,7 @@
         super(MultiTestResult, self).addFailure(test, err)
         for result in self._results:
             try:
-                result.addSuccess(test)
+                result.addFailure(test, err)
             except AttributeError:
                 if self._forgiving:
                     pass
@@ -336,7 +362,7 @@
         super(MultiTestResult, self).addSkip(test, reason)
         for result in self._results:
             try:
-                result.addSuccess(test)
+                result.addSuccess(test, reason)
             except AttributeError:
                 if self._forgiving:
                     pass
@@ -347,7 +373,7 @@
         super(MultiTestResult, self).addExpectedFailure(test, err)
         for result in self._results:
             try:
-                result.addSuccess(test)
+                result.addExpectedFailure(test, err)
             except AttributeError:
                 if self._forgiving:
                     pass
@@ -358,7 +384,7 @@
         super(MultiTestResult, self).addUnexpectedSuccess(test)
         for result in self._results:
             try:
-                result.addSuccess(test)
+                result.addUnexpectedSuccess(test)
             except AttributeError:
                 if self._forgiving:
                     pass

Modified: grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_errors/testsuite/test_error.py
===================================================================
--- grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_errors/testsuite/test_error.py	2014-07-25 00:16:19 UTC (rev 61393)
+++ grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_errors/testsuite/test_error.py	2014-07-25 01:53:39 UTC (rev 61394)
@@ -1,7 +1,6 @@
 # -*- coding: utf-8 -*-
 
-# TODO: change to GrassTestCase
-from unittest import TestCase
+from grass.gunittest import TestCase, test
 
 
 class TestError(TestCase):
@@ -55,5 +54,4 @@
 
 
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

Modified: grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_errors/testsuite/test_import_error.py
===================================================================
--- grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_errors/testsuite/test_import_error.py	2014-07-25 00:16:19 UTC (rev 61393)
+++ grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_errors/testsuite/test_import_error.py	2014-07-25 01:53:39 UTC (rev 61394)
@@ -1,11 +1,10 @@
 # -*- coding: utf-8 -*-
 
 # comment to get rid of the wrong import
-# (if it is imported before all tests start)
+# (if it is imported before all tests start and everything would fail)
 #import this_module_or_package_does_not_exists__testing_import_error
 
-# TODO: change to GrassTestCase
-from unittest import TestCase
+from grass.gunittest import TestCase, test
 
 
 class TestNeverCalled(TestCase):
@@ -14,11 +13,9 @@
     def test_something(self):
         self.assertFalse("This should not be called"
                          " if we are testing failed import."
-                         " But it is good OK if one wrong import"
-                         " would prevent other tests from running"
-                         " due to the implementation of test invocation.")
+                         " It is all right if this fails and the wrong"
+                         " import is commented.")
 
 
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

Modified: grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_gfatalerror.py
===================================================================
--- grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_gfatalerror.py	2014-07-25 00:16:19 UTC (rev 61393)
+++ grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_gfatalerror.py	2014-07-25 01:53:39 UTC (rev 61394)
@@ -1,11 +1,9 @@
 # -*- coding: utf-8 -*-
 
 import grass.lib.gis as libgis
+from grass.gunittest import TestCase, test
 
-# TODO: change to GrassTestCase
-from unittest import TestCase
 
-
 class TestGFatalError(TestCase):
     # pylint: disable=R0904
 
@@ -14,5 +12,4 @@
 
 
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

Modified: grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_osexit_one.py
===================================================================
--- grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_osexit_one.py	2014-07-25 00:16:19 UTC (rev 61393)
+++ grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_osexit_one.py	2014-07-25 01:53:39 UTC (rev 61394)
@@ -1,11 +1,9 @@
 # -*- coding: utf-8 -*-
 
 import os
+from grass.gunittest import TestCase, test
 
-# TODO: change to GrassTestCase
-from unittest import TestCase
 
-
 class TestOsExit(TestCase):
     # pylint: disable=R0904
 
@@ -14,5 +12,4 @@
 
 
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

Modified: grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_osexit_zero.py
===================================================================
--- grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_osexit_zero.py	2014-07-25 00:16:19 UTC (rev 61393)
+++ grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_osexit_zero.py	2014-07-25 01:53:39 UTC (rev 61394)
@@ -1,11 +1,9 @@
 # -*- coding: utf-8 -*-
 
 import os
+from grass.gunittest import TestCase, test
 
-# TODO: change to GrassTestCase
-from unittest import TestCase
 
-
 class TestOsExit(TestCase):
     # pylint: disable=R0904
 
@@ -14,5 +12,4 @@
 
 
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

Modified: grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_segfaut.py
===================================================================
--- grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_segfaut.py	2014-07-25 00:16:19 UTC (rev 61393)
+++ grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_segfaut.py	2014-07-25 01:53:39 UTC (rev 61394)
@@ -1,11 +1,9 @@
 # -*- coding: utf-8 -*-
 
 import ctypes
+from grass.gunittest import TestCase, test
 
-# TODO: change to GrassTestCase
-from unittest import TestCase
 
-
 class TestSegfault(TestCase):
     # pylint: disable=R0904
 
@@ -21,5 +19,4 @@
 
 
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

Modified: grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_sysexit_one.py
===================================================================
--- grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_sysexit_one.py	2014-07-25 00:16:19 UTC (rev 61393)
+++ grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_sysexit_one.py	2014-07-25 01:53:39 UTC (rev 61394)
@@ -1,11 +1,9 @@
 # -*- coding: utf-8 -*-
 
 import sys
+from grass.gunittest import TestCase, test
 
-# TODO: change to GrassTestCase
-from unittest import TestCase
 
-
 class TestSysExit(TestCase):
     # pylint: disable=R0904
 
@@ -14,5 +12,4 @@
 
 
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

Modified: grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_sysexit_zero.py
===================================================================
--- grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_sysexit_zero.py	2014-07-25 00:16:19 UTC (rev 61393)
+++ grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_sysexit_zero.py	2014-07-25 01:53:39 UTC (rev 61394)
@@ -1,11 +1,9 @@
 # -*- coding: utf-8 -*-
 
 import sys
+from grass.gunittest import TestCase, test
 
-# TODO: change to GrassTestCase
-from unittest import TestCase
 
-
 class TestSysExit(TestCase):
     # pylint: disable=R0904
 
@@ -14,5 +12,4 @@
 
 
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

Modified: grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_test_fail/testsuite/test_fail.py
===================================================================
--- grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_test_fail/testsuite/test_fail.py	2014-07-25 00:16:19 UTC (rev 61393)
+++ grass/trunk/lib/python/gunittest/testsuite/data/samplecode/submodule_test_fail/testsuite/test_fail.py	2014-07-25 01:53:39 UTC (rev 61394)
@@ -1,7 +1,6 @@
 # -*- coding: utf-8 -*-
 
-# TODO: change to GrassTestCase
-from unittest import TestCase
+from grass.gunittest import TestCase, test
 
 
 class TestFail(TestCase):
@@ -12,5 +11,4 @@
 
 
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

Added: grass/trunk/lib/python/gunittest/testsuite/data/samplecode/testsuite/test_good_and_bad.py
===================================================================
--- grass/trunk/lib/python/gunittest/testsuite/data/samplecode/testsuite/test_good_and_bad.py	                        (rev 0)
+++ grass/trunk/lib/python/gunittest/testsuite/data/samplecode/testsuite/test_good_and_bad.py	2014-07-25 01:53:39 UTC (rev 61394)
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+
+from grass.gunittest import TestCase, test
+
+
+class TestSuccessAndFailure(TestCase):
+    # pylint: disable=R0904
+
+    def test_something(self):
+        self.assertTrue(True, msg="This should not fail in test_good_and_bad")
+
+    def test_something_else(self):
+        self.assertTrue(True, msg="This should not fail in test_good_and_bad")
+
+    def test_something_failing(self):
+        self.assertTrue(False, msg="This failed in test_good_and_bad")
+
+    def test_something_erroring(self):
+        raise RuntimeError('Some error which was raised')
+        self.assertTrue(True, msg="This should not fail in test_good_and_bad")
+
+if __name__ == '__main__':
+    test()


Property changes on: grass/trunk/lib/python/gunittest/testsuite/data/samplecode/testsuite/test_good_and_bad.py
___________________________________________________________________
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native

Added: grass/trunk/lib/python/gunittest/testsuite/data/samplecode/testsuite/test_python_unittest.py
===================================================================
--- grass/trunk/lib/python/gunittest/testsuite/data/samplecode/testsuite/test_python_unittest.py	                        (rev 0)
+++ grass/trunk/lib/python/gunittest/testsuite/data/samplecode/testsuite/test_python_unittest.py	2014-07-25 01:53:39 UTC (rev 61394)
@@ -0,0 +1,40 @@
+# -*- coding: utf-8 -*-
+
+from unittest import TestCase, main
+
+
+class TestUnittestSuccessVerboseSetUp(TestCase):
+    # pylint: disable=R0904
+
+    def setUp(self):
+        print "print from setUp"
+
+    def tearDown(self):
+        print "print from tearDown"
+
+    def test_something(self):
+        self.assertTrue(True, msg="This should not fail")
+
+    def test_something_failing(self):
+        self.assertTrue(False, msg="This should fail")
+
+
+class TestUnittestSuccessVerboseClassSetUp(TestCase):
+    # pylint: disable=R0904
+
+    @classmethod
+    def setUpClass(cls):
+        print "print from setUpClass"
+
+    @classmethod
+    def tearDownClass(cls):
+        print "print from tearDownClass"
+
+    def test_something(self):
+        self.assertTrue(True, msg="This should not fail")
+
+    def test_something_failing(self):
+        self.assertTrue(False, msg="This should fail")
+
+if __name__ == '__main__':
+    main()


Property changes on: grass/trunk/lib/python/gunittest/testsuite/data/samplecode/testsuite/test_python_unittest.py
___________________________________________________________________
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native

Modified: grass/trunk/lib/python/gunittest/testsuite/data/samplecode/testsuite/test_success.py
===================================================================
--- grass/trunk/lib/python/gunittest/testsuite/data/samplecode/testsuite/test_success.py	2014-07-25 00:16:19 UTC (rev 61393)
+++ grass/trunk/lib/python/gunittest/testsuite/data/samplecode/testsuite/test_success.py	2014-07-25 01:53:39 UTC (rev 61394)
@@ -1,7 +1,6 @@
 # -*- coding: utf-8 -*-
 
-# TODO: change to GrassTestCase
-from unittest import TestCase
+from grass.gunittest import TestCase, test
 
 
 class TestSuccessVerboseSetUp(TestCase):
@@ -32,5 +31,4 @@
         self.assertTrue(True)
 
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()



More information about the grass-commit mailing list