[Python-checkins] r74783 - in python/trunk: Doc/whatsnew/2.7.rst Lib/test/test_unittest.py Lib/unittest/loader.py

michael.foord python-checkins at python.org
Sun Sep 13 19:28:35 CEST 2009


Author: michael.foord
Date: Sun Sep 13 19:28:35 2009
New Revision: 74783

Log:
unittest.TestLoader.loadTestsFromName honors the loader suiteClass attribute. Issue 6866.

Modified:
   python/trunk/Doc/whatsnew/2.7.rst
   python/trunk/Lib/test/test_unittest.py
   python/trunk/Lib/unittest/loader.py

Modified: python/trunk/Doc/whatsnew/2.7.rst
==============================================================================
--- python/trunk/Doc/whatsnew/2.7.rst	(original)
+++ python/trunk/Doc/whatsnew/2.7.rst	Sun Sep 13 19:28:35 2009
@@ -509,6 +509,9 @@
     (automatically pass or fail without checking decimal places) if the objects
     are equal.
 
+  * :meth:`loadTestsFromName` properly honors the ``suiteClass`` attribute of
+    the :class:`TestLoader`. (Fixed by Mark Roddy; :issue:`6866`.)
+
   * A new hook, :meth:`addTypeEqualityFunc` takes a type object and a
     function.  The :meth:`assertEqual` method will use the function
     when both of the objects being compared are of the specified type.

Modified: python/trunk/Lib/test/test_unittest.py
==============================================================================
--- python/trunk/Lib/test/test_unittest.py	(original)
+++ python/trunk/Lib/test/test_unittest.py	Sun Sep 13 19:28:35 2009
@@ -555,6 +555,47 @@
         self.assertEqual(list(suite), [testcase_1])
 
     # "The specifier name is a ``dotted name'' that may resolve ... to
+    # ... a callable object which returns a TestCase ... instance"
+    #*****************************************************************
+    #Override the suiteClass attribute to ensure that the suiteClass
+    #attribute is used
+    def test_loadTestsFromName__callable__TestCase_instance_ProperSuiteClass(self):
+        class SubTestSuite(unittest.TestSuite):
+            pass
+        m = types.ModuleType('m')
+        testcase_1 = unittest.FunctionTestCase(lambda: None)
+        def return_TestCase():
+            return testcase_1
+        m.return_TestCase = return_TestCase
+
+        loader = unittest.TestLoader()
+        loader.suiteClass = SubTestSuite
+        suite = loader.loadTestsFromName('return_TestCase', m)
+        self.assertTrue(isinstance(suite, loader.suiteClass))
+        self.assertEqual(list(suite), [testcase_1])
+
+    # "The specifier name is a ``dotted name'' that may resolve ... to
+    # ... a test method within a test case class"
+    #*****************************************************************
+    #Override the suiteClass attribute to ensure that the suiteClass
+    #attribute is used
+    def test_loadTestsFromName__relative_testmethod_ProperSuiteClass(self):
+        class SubTestSuite(unittest.TestSuite):
+            pass
+        m = types.ModuleType('m')
+        class MyTestCase(unittest.TestCase):
+            def test(self):
+                pass
+        m.testcase_1 = MyTestCase
+
+        loader = unittest.TestLoader()
+        loader.suiteClass=SubTestSuite
+        suite = loader.loadTestsFromName('testcase_1.test', m)
+        self.assertTrue(isinstance(suite, loader.suiteClass))
+
+        self.assertEqual(list(suite), [MyTestCase('test')])
+
+    # "The specifier name is a ``dotted name'' that may resolve ... to
     # ... a callable object which returns a TestCase or TestSuite instance"
     #
     # What happens if the callable returns something else?

Modified: python/trunk/Lib/unittest/loader.py
==============================================================================
--- python/trunk/Lib/unittest/loader.py	(original)
+++ python/trunk/Lib/unittest/loader.py	Sun Sep 13 19:28:35 2009
@@ -85,7 +85,7 @@
         elif (isinstance(obj, types.UnboundMethodType) and
               isinstance(parent, type) and
               issubclass(parent, case.TestCase)):
-            return suite.TestSuite([parent(obj.__name__)])
+            return self.suiteClass([parent(obj.__name__)])
         elif isinstance(obj, suite.TestSuite):
             return obj
         elif hasattr(obj, '__call__'):
@@ -93,7 +93,7 @@
             if isinstance(test, suite.TestSuite):
                 return test
             elif isinstance(test, case.TestCase):
-                return suite.TestSuite([test])
+                return self.suiteClass([test])
             else:
                 raise TypeError("calling %s returned %s, not a test" %
                                 (obj, test))


More information about the Python-checkins mailing list