diff makeitso/python_package/tests/test.py @ 123:8db34885ebe4

tame that beast called doctest
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 19 May 2011 10:56:16 -0700
parents ad5fd3eb6674
children 3eee157f4564
line wrap: on
line diff
--- a/makeitso/python_package/tests/test.py	Wed Jan 19 18:24:58 2011 -0800
+++ b/makeitso/python_package/tests/test.py	Thu May 19 10:56:16 2011 -0700
@@ -5,15 +5,54 @@
 """
 
 import doctest
+import optparse
 import os
+import sys
 
-def run_tests():
+def run_tests(raise_on_error=False, report_first=False):
+
+    # add results here
+    results = {}
+
+    # doctest arguments
     directory = os.path.dirname(os.path.abspath(__file__))
     extraglobs = {'here': directory}
+    doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error)
+    if report_first:
+        doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE
+
+    # gather tests
     tests =  [ test for test in os.listdir(directory)
                if test.endswith('.txt') ]
+
+    # run the tests
     for test in tests:
-        doctest.testfile(test, extraglobs=extraglobs, raise_on_error=False)
+        results[test] = doctest.testfile(test, **doctest_args)
+    except doctest.DocTestFailure, failure:
+        raise
+    except doctest.UnexpectedException, failure:
+        raise failure.exc_info[0], failure.exc_info[1], failure.exc_info[2]
+
+    return results
+
+def main(args=sys.argv[1:]):
+
+    # parse command line args
+    parser = OptionParser()
+    parser.add_option('--raise', dest='raise_on_error',
+                      default=False, action='store_true',
+                      help="raise on first error")
+    parser.add_option('--report-first', dest='report_first',
+                      default=False, action='store_true',
+                      help="report the first error only (all tests will still run)")
+    options, args = parser.parse_args(args)
+
+    # run the tests
+    results = run_tests(report_first=options.report_first,
+                        raise_on_error=options.raise_on_error)
+    if sum([i.failed for i in results.values()]):
+        sys.exit(1) # error
+                
 
 if __name__ == '__main__':
-    run_tests()
+    main()