[Python-checkins] r54201 - in python/trunk: Doc/lib/libunittest.tex Lib/unittest.py Misc/NEWS

georg.brandl python-checkins at python.org
Wed Mar 7 10:21:12 CET 2007


Author: georg.brandl
Date: Wed Mar  7 10:21:06 2007
New Revision: 54201

Modified:
   python/trunk/Doc/lib/libunittest.tex
   python/trunk/Lib/unittest.py
   python/trunk/Misc/NEWS
Log:
Patch #787789: allow to pass custom TestRunner instances to unittest's
main() function.


Modified: python/trunk/Doc/lib/libunittest.tex
==============================================================================
--- python/trunk/Doc/lib/libunittest.tex	(original)
+++ python/trunk/Doc/lib/libunittest.tex	Wed Mar  7 10:21:06 2007
@@ -290,6 +290,7 @@
 we would end up subclassing \class{SimpleWidgetTestCase} into many
 small one-method classes such as
 \class{DefaultWidgetSizeTestCase}.  This is time-consuming and
+
 discouraging, so in the same vein as JUnit, \module{unittest} provides
 a simpler mechanism:
 
@@ -540,7 +541,7 @@
 
 \begin{funcdesc}{main}{\optional{module\optional{,
                  defaultTest\optional{, argv\optional{,
-                 testRunner\optional{, testRunner}}}}}}
+                 testRunner\optional{, testLoader}}}}}}
   A command-line program that runs a set of tests; this is primarily
   for making test modules conveniently executable.  The simplest use
   for this function is to include the following line at the end of a
@@ -550,6 +551,9 @@
 if __name__ == '__main__':
     unittest.main()
 \end{verbatim}
+
+  The \var{testRunner} argument can either be a test runner class or
+  an already created instance of it.
 \end{funcdesc}
 
 In some cases, the existing tests may have been written using the

Modified: python/trunk/Lib/unittest.py
==============================================================================
--- python/trunk/Lib/unittest.py	(original)
+++ python/trunk/Lib/unittest.py	Wed Mar  7 10:21:06 2007
@@ -776,7 +776,8 @@
                                                in MyTestCase
 """
     def __init__(self, module='__main__', defaultTest=None,
-                 argv=None, testRunner=None, testLoader=defaultTestLoader):
+                 argv=None, testRunner=TextTestRunner,
+                 testLoader=defaultTestLoader):
         if type(module) == type(''):
             self.module = __import__(module)
             for part in module.split('.')[1:]:
@@ -826,9 +827,16 @@
                                                        self.module)
 
     def runTests(self):
-        if self.testRunner is None:
-            self.testRunner = TextTestRunner(verbosity=self.verbosity)
-        result = self.testRunner.run(self.test)
+        if isinstance(self.testRunner, (type, types.ClassType)):
+            try:
+                testRunner = self.testRunner(verbosity=self.verbosity)
+            except TypeError:
+                # didn't accept the verbosity argument
+                testRunner = self.testRunner()
+        else:
+            # it is assumed to be a TestRunner instance
+            testRunner = self.testRunner
+        result = testRunner.run(self.test)
         sys.exit(not result.wasSuccessful())
 
 main = TestProgram

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Wed Mar  7 10:21:06 2007
@@ -152,6 +152,9 @@
 Library
 -------
 
+- Patch #787789: allow to pass custom TestRunner instances to unittest's
+  main() function.
+
 - Patches #1550273, #1550272: fix a few bugs in unittest and add a
   comprehensive test suite for the module.
 


More information about the Python-checkins mailing list