ソースを参照

gunittest: count (and store) successful tests to get the right number; use gunittest for gunittest's test data

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@61394 15284696-431f-4ddb-bdfa-cd5b030d7da7
Vaclav Petras 10 年 前
コミット
09b2d3f53c

+ 5 - 4
lib/python/gunittest/reporters.py

@@ -342,7 +342,8 @@ class GrassTestFilesHtmlReporter(GrassTestFilesCountingReporter):
         if returncode:
         if returncode:
             return '<span style="color: red">FAILED</span>'
             return '<span style="color: red">FAILED</span>'
         else:
         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):
     def returncode_to_html_sentence(self, returncode):
         if returncode:
         if returncode:
@@ -368,18 +369,18 @@ class GrassTestFilesHtmlReporter(GrassTestFilesCountingReporter):
         # TODO: add success counter to GrassTestResult base class
         # TODO: add success counter to GrassTestResult base class
         skipped = test_summary.get('skipped', 0)
         skipped = test_summary.get('skipped', 0)
         expected_failures = test_summary.get('expected_failures', 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.failures += failures
         self.errors += errors
         self.errors += errors
         self.skiped += skipped
         self.skiped += skipped
         self.expected_failures += expected_failures
         self.expected_failures += expected_failures
-        self.unexpected_success += unexpected_success
+        self.unexpected_success += unexpected_successes
 
 
         if total is not None:
         if total is not None:
             # success are only the clear ones
             # success are only the clear ones
             # percentage is influenced by all but putting only failures to table
             # percentage is influenced by all but putting only failures to table
-            successes = total - failures - errors - skipped - expected_failures - unexpected_success
             self.successes += successes
             self.successes += successes
             self.total += total
             self.total += total
 
 

+ 35 - 9
lib/python/gunittest/runner.py

@@ -18,7 +18,7 @@ a template. It is not expected that something will left.
 import sys
 import sys
 import time
 import time
 
 
-from unittest.result import TestResult
+import unittest.result
 from unittest.signals import registerResult
 from unittest.signals import registerResult
 
 
 __unittest = True
 __unittest = True
@@ -40,6 +40,29 @@ class _WritelnDecorator(object):
         self.write('\n') # text-mode streams translate to \r\n if needed
         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):
 class TextTestResult(TestResult):
     """A test result class that can print formatted text results to a stream.
     """A test result class that can print formatted text results to a stream.
 
 
@@ -231,17 +254,20 @@ class KeyValueTestResult(TestResult):
 #            'date={start_datetime}\n'
 #            'date={start_datetime}\n'
 #            'date={end_datetime}\n'
 #            'date={end_datetime}\n'
 
 
+        failed, errored = map(len, (self.failures, self.errors))
+        succeeded = len(self.successes)
         results = map(len, (self.expectedFailures,
         results = map(len, (self.expectedFailures,
                             self.unexpectedSuccesses,
                             self.unexpectedSuccesses,
                             self.skipped))
                             self.skipped))
         expectedFails, unexpectedSuccesses, skipped = results
         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("failures=%d" % failed)
         infos.append("errors=%d" % errored)
         infos.append("errors=%d" % errored)
+        infos.append("successes=%d" % succeeded)
         infos.append("skipped=%d" % skipped)
         infos.append("skipped=%d" % skipped)
 
 
         # TODO: document this: if not supported by view,
         # TODO: document this: if not supported by view,
@@ -314,7 +340,7 @@ class MultiTestResult(TestResult):
         super(MultiTestResult, self).addError(test, err)
         super(MultiTestResult, self).addError(test, err)
         for result in self._results:
         for result in self._results:
             try:
             try:
-                result.addSuccess(test)
+                result.addError(test, err)
             except AttributeError:
             except AttributeError:
                 if self._forgiving:
                 if self._forgiving:
                     pass
                     pass
@@ -325,7 +351,7 @@ class MultiTestResult(TestResult):
         super(MultiTestResult, self).addFailure(test, err)
         super(MultiTestResult, self).addFailure(test, err)
         for result in self._results:
         for result in self._results:
             try:
             try:
-                result.addSuccess(test)
+                result.addFailure(test, err)
             except AttributeError:
             except AttributeError:
                 if self._forgiving:
                 if self._forgiving:
                     pass
                     pass
@@ -336,7 +362,7 @@ class MultiTestResult(TestResult):
         super(MultiTestResult, self).addSkip(test, reason)
         super(MultiTestResult, self).addSkip(test, reason)
         for result in self._results:
         for result in self._results:
             try:
             try:
-                result.addSuccess(test)
+                result.addSuccess(test, reason)
             except AttributeError:
             except AttributeError:
                 if self._forgiving:
                 if self._forgiving:
                     pass
                     pass
@@ -347,7 +373,7 @@ class MultiTestResult(TestResult):
         super(MultiTestResult, self).addExpectedFailure(test, err)
         super(MultiTestResult, self).addExpectedFailure(test, err)
         for result in self._results:
         for result in self._results:
             try:
             try:
-                result.addSuccess(test)
+                result.addExpectedFailure(test, err)
             except AttributeError:
             except AttributeError:
                 if self._forgiving:
                 if self._forgiving:
                     pass
                     pass
@@ -358,7 +384,7 @@ class MultiTestResult(TestResult):
         super(MultiTestResult, self).addUnexpectedSuccess(test)
         super(MultiTestResult, self).addUnexpectedSuccess(test)
         for result in self._results:
         for result in self._results:
             try:
             try:
-                result.addSuccess(test)
+                result.addUnexpectedSuccess(test)
             except AttributeError:
             except AttributeError:
                 if self._forgiving:
                 if self._forgiving:
                     pass
                     pass

+ 2 - 4
lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_errors/testsuite/test_error.py

@@ -1,7 +1,6 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 
 
-# TODO: change to GrassTestCase
-from unittest import TestCase
+from grass.gunittest import TestCase, test
 
 
 
 
 class TestError(TestCase):
 class TestError(TestCase):
@@ -55,5 +54,4 @@ class TestErrorClassTearDown(TestCase):
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

+ 5 - 8
lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_errors/testsuite/test_import_error.py

@@ -1,11 +1,10 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 
 
 # comment to get rid of the wrong import
 # 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
 #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):
 class TestNeverCalled(TestCase):
@@ -14,11 +13,9 @@ class TestNeverCalled(TestCase):
     def test_something(self):
     def test_something(self):
         self.assertFalse("This should not be called"
         self.assertFalse("This should not be called"
                          " if we are testing failed import."
                          " 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__':
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

+ 2 - 5
lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_gfatalerror.py

@@ -1,9 +1,7 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 
 
 import grass.lib.gis as libgis
 import grass.lib.gis as libgis
-
-# TODO: change to GrassTestCase
-from unittest import TestCase
+from grass.gunittest import TestCase, test
 
 
 
 
 class TestGFatalError(TestCase):
 class TestGFatalError(TestCase):
@@ -14,5 +12,4 @@ class TestGFatalError(TestCase):
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

+ 2 - 5
lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_osexit_one.py

@@ -1,9 +1,7 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 
 
 import os
 import os
-
-# TODO: change to GrassTestCase
-from unittest import TestCase
+from grass.gunittest import TestCase, test
 
 
 
 
 class TestOsExit(TestCase):
 class TestOsExit(TestCase):
@@ -14,5 +12,4 @@ class TestOsExit(TestCase):
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

+ 2 - 5
lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_osexit_zero.py

@@ -1,9 +1,7 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 
 
 import os
 import os
-
-# TODO: change to GrassTestCase
-from unittest import TestCase
+from grass.gunittest import TestCase, test
 
 
 
 
 class TestOsExit(TestCase):
 class TestOsExit(TestCase):
@@ -14,5 +12,4 @@ class TestOsExit(TestCase):
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

+ 2 - 5
lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_segfaut.py

@@ -1,9 +1,7 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 
 
 import ctypes
 import ctypes
-
-# TODO: change to GrassTestCase
-from unittest import TestCase
+from grass.gunittest import TestCase, test
 
 
 
 
 class TestSegfault(TestCase):
 class TestSegfault(TestCase):
@@ -21,5 +19,4 @@ class TestSegfault(TestCase):
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

+ 2 - 5
lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_sysexit_one.py

@@ -1,9 +1,7 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 
 
 import sys
 import sys
-
-# TODO: change to GrassTestCase
-from unittest import TestCase
+from grass.gunittest import TestCase, test
 
 
 
 
 class TestSysExit(TestCase):
 class TestSysExit(TestCase):
@@ -14,5 +12,4 @@ class TestSysExit(TestCase):
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

+ 2 - 5
lib/python/gunittest/testsuite/data/samplecode/submodule_errors/subsubmodule_exiting/testsuite/test_sysexit_zero.py

@@ -1,9 +1,7 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 
 
 import sys
 import sys
-
-# TODO: change to GrassTestCase
-from unittest import TestCase
+from grass.gunittest import TestCase, test
 
 
 
 
 class TestSysExit(TestCase):
 class TestSysExit(TestCase):
@@ -14,5 +12,4 @@ class TestSysExit(TestCase):
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

+ 2 - 4
lib/python/gunittest/testsuite/data/samplecode/submodule_test_fail/testsuite/test_fail.py

@@ -1,7 +1,6 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 
 
-# TODO: change to GrassTestCase
-from unittest import TestCase
+from grass.gunittest import TestCase, test
 
 
 
 
 class TestFail(TestCase):
 class TestFail(TestCase):
@@ -12,5 +11,4 @@ class TestFail(TestCase):
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()

+ 23 - 0
lib/python/gunittest/testsuite/data/samplecode/testsuite/test_good_and_bad.py

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

+ 40 - 0
lib/python/gunittest/testsuite/data/samplecode/testsuite/test_python_unittest.py

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

+ 2 - 4
lib/python/gunittest/testsuite/data/samplecode/testsuite/test_success.py

@@ -1,7 +1,6 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 
 
-# TODO: change to GrassTestCase
-from unittest import TestCase
+from grass.gunittest import TestCase, test
 
 
 
 
 class TestSuccessVerboseSetUp(TestCase):
 class TestSuccessVerboseSetUp(TestCase):
@@ -32,5 +31,4 @@ class TestSuccessVerboseClassSetUp(TestCase):
         self.assertTrue(True)
         self.assertTrue(True)
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
-    import unittest
-    unittest.main()
+    test()