Эх сурвалжийг харах

Code cleanup and added test filename to results.

Sander Mathijs van Veen 14 жил өмнө
parent
commit
1e95b6a224
3 өөрчлөгдсөн 46 нэмэгдсэн , 19 устгасан
  1. 7 0
      __init__.py
  2. 1 3
      __main__.py
  3. 38 16
      runner.py

+ 7 - 0
__init__.py

@@ -1,2 +1,9 @@
+#
+# Python unit test runner
+#
+# Author : Sander Mathijs van Veen <smvv@kompiler.org>
+# License: GPL version 3, see also the file `LICENSE'.
+#
+
 from runner import TextTestRunner, main
 __all__ = ['TextTestRunner', 'main']

+ 1 - 3
__main__.py

@@ -6,9 +6,7 @@
 #
 
 import sys
-import unittest
-
-from testrunner import TextTestRunner, main
+from testrunner import main
 
 if len(sys.argv) < 2:
     print 'Usage: %s tests/test_COMPONENT.py' % sys.argv[0]

+ 38 - 16
runner.py

@@ -1,3 +1,9 @@
+#
+# Python unit test runner
+#
+# Author : Sander Mathijs van Veen <smvv@kompiler.org>
+# License: GPL version 3, see also the file `LICENSE'.
+#
 import os
 import sys
 import time
@@ -24,6 +30,7 @@ class TextTestRunner(unittest.TextTestRunner):
 
         if startTestRun is not None:
             startTestRun()
+
         try:
             test(result)
         finally:
@@ -35,11 +42,12 @@ class TextTestRunner(unittest.TextTestRunner):
         timeTaken = stopTime - startTime
         result.printErrors()
 
-        run = result.testsRun
-        msg = 'Ran %d test%s in %.3fs '
-        self.stream.write(msg % (run, run != 1 and 's' or '', timeTaken))
+        self.stream.write('%s: %d test%s in %.3fs ' \
+                          % (test.filename, result.testsRun,
+                             result.testsRun != 1 and 's' or '', timeTaken))
 
         expectedFails = unexpectedSuccesses = skipped = 0
+
         try:
             results = map(len, (result.expectedFailures,
                                 result.unexpectedSuccesses,
@@ -59,12 +67,14 @@ class TextTestRunner(unittest.TextTestRunner):
             self.stream.write(msg)
 
             failed, errored = map(len, (result.failures, result.errors))
+
             if failed: 
                 if self.color:
                     msg = 'failures=\033[1;31m%d\033[1;m'
                 else:
                     msg = 'failures=%d'
                 infos.append(msg % failed)
+
             if errored:
                 if self.color:
                     msg = 'errors=\033[1;31m%d\033[1;m'
@@ -76,6 +86,7 @@ class TextTestRunner(unittest.TextTestRunner):
                 msg = '\033[0;32mOK\033[0;m'
             else:
                 msg = 'OK'
+
             self.stream.write(msg)
 
         if skipped:
@@ -83,6 +94,7 @@ class TextTestRunner(unittest.TextTestRunner):
                 msg = 'skipped=\033[1;33m%d\033[1;m'
             else:
                 msg = 'skipped=%d'
+
             infos.append(msg % skipped)
 
         if expectedFails:
@@ -99,28 +111,38 @@ class TextTestRunner(unittest.TextTestRunner):
         return result
 
 def main(tests, verbose=0, color=True):
-    suites = []
+    testcases = []
 
     # Dynamic load the requested module containing the test cases.
     for testfile in tests:
 
-        module_dir, module_file = os.path.split(testfile)
-        module_name, module_ext = os.path.splitext(module_file)
-
-        # XXX: this looks really hacky, so cleanup is necessary.
-        module_obj = __import__(module_dir.replace('/', '.'),
-                                fromlist=[module_name])
-        module_obj.__file__ = testfile
-        globals()[module_name] = module_obj
+        try:
+            module_name = os.path.splitext(testfile)[0].replace('/', '.')
+            module_obj = __import__(module_name)
+        except:
+            print 'testfile:   ', testfile
+            print 'module_name:', module_name
+            raise
 
         # Start the test runner and display the results to stdout.
-        container = module_obj.__dict__[module_name]
-        class_parts = module_name[5:].split('_')
+        try:
+            suite_name = module_name.split('.')[-1]
+            container = module_obj.__dict__[suite_name]
+        except:
+            print 'testfile:   ', testfile
+            print 'module_name:', module_name
+            print 'module_obj: ', module_obj, dir(module_obj)
+            raise
+
+        # Convert lowercase, underscored suite name to Python class name.
+        class_parts = suite_name[5:].split('_')
         class_name = 'Test' + ''.join([p.capitalize() for p in class_parts])
 
         testcase = container.__dict__[class_name]
-        suites += [unittest.TestLoader().loadTestsFromTestCase(testcase)]
+        testcases += [unittest.TestLoader().loadTestsFromTestCase(testcase)]
 
     # Create the text runner and execute the tests.
     runner = TextTestRunner(verbosity=verbose, color=color)
-    runner.run(unittest.TestSuite(suites))
+    suite = unittest.TestSuite(testcases)
+    suite.filename = testfile
+    runner.run(suite)