ソースを参照

Initial commit of the testrunner python module.

Sander Mathijs van Veen 14 年 前
コミット
87e29a31f3
4 ファイル変更117 行追加0 行削除
  1. 2 0
      .gitignore
  2. 2 0
      __init__.py
  3. 44 0
      __main__.py
  4. 69 0
      runner.py

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+*.pyc
+*.swp

+ 2 - 0
__init__.py

@@ -0,0 +1,2 @@
+from runner import TextTestRunner
+__all__ = ['TextTestRunner']

+ 44 - 0
__main__.py

@@ -0,0 +1,44 @@
+#
+# Python unit test runner
+#
+# Author : Sander Mathijs van Veen <smvv@kompiler.org>
+# License: GPL version 3, see also the file `LICENSE'.
+#
+
+import sys
+import os
+import unittest
+
+from testrunner import TextTestRunner
+
+if len(sys.argv) < 2:
+    print 'Usage: %s tests/test_COMPONENT.py' % sys.argv[0]
+    sys.exit(1)
+
+
+# TODO parse verbosity option '-v', given in argv.
+verbosity = 0
+
+suites = []
+
+# Dynamic load the requested module containing the test cases.
+for testfile in sys.argv[1:]:
+
+    module_dir, module_file = os.path.split(testfile)
+    module_name, module_ext = os.path.splitext(module_file)
+
+    module_obj = __import__(module_dir.replace('/', '.'),
+                            fromlist=[module_name])
+    module_obj.__file__ = testfile
+    globals()[module_name] = module_obj
+
+    # Start the test runner and display the results to stdout.
+    container = module_obj.__dict__[module_name]
+    class_parts = module_name[5:].split('_')
+    class_name = 'Test' + ''.join([p.capitalize() for p in class_parts])
+
+    testcase = container.__dict__[class_name]
+    suites += [unittest.TestLoader().loadTestsFromTestCase(testcase)]
+
+# Create the text runner and execute the tests.
+TextTestRunner(verbosity=verbosity).run(unittest.TestSuite(suites))

+ 69 - 0
runner.py

@@ -0,0 +1,69 @@
+import unittest
+import time
+
+
+class TextTestRunner(unittest.TextTestRunner):
+    """This is a wrapper class used to minimize the amount of blank lines
+    printed to stdout. The method ``run`` is modified and it's original source
+    is in Python Standard Library's ``unittest`` module."""
+
+    def run(self, test):
+        "Run the given test case or test suite."
+        result = self._makeResult()
+        result.failfast = getattr(self, 'failfast', None)
+        result.buffer = getattr(self, 'buffer', None)
+        startTime = time.time()
+        startTestRun = getattr(result, 'startTestRun', None)
+
+        if startTestRun is not None:
+            startTestRun()
+        try:
+            test(result)
+        finally:
+            stopTestRun = getattr(result, 'stopTestRun', None)
+            if stopTestRun is not None:
+                stopTestRun()
+
+        stopTime = time.time()
+        timeTaken = stopTime - startTime
+        result.printErrors()
+
+        run = result.testsRun
+        self.stream.write("Ran %d test%s in %.3fs " %
+                          (run, run != 1 and "s" or "", timeTaken))
+
+        expectedFails = unexpectedSuccesses = skipped = 0
+        try:
+            results = map(len, (result.expectedFailures,
+                                result.unexpectedSuccesses,
+                                result.skipped))
+        except AttributeError:
+            pass
+        else:
+            expectedFails, unexpectedSuccesses, skipped = results
+
+        infos = []
+
+        if not result.wasSuccessful():
+            self.stream.write("FAILED")
+            failed, errored = map(len, (result.failures, result.errors))
+            if failed:
+                infos.append("failures=%d" % failed)
+            if errored:
+                infos.append("errors=%d" % errored)
+        else:
+            self.stream.write("OK")
+
+        if skipped:
+            infos.append("skipped=%d" % skipped)
+        if expectedFails:
+            infos.append("expected failures=%d" % expectedFails)
+        if unexpectedSuccesses:
+            infos.append("unexpected successes=%d" % unexpectedSuccesses)
+
+        if infos:
+            self.stream.writeln(" (%s)" % (", ".join(infos),))
+        else:
+            self.stream.write("\n")
+
+        return result