Commit 5a9719d6 authored by Michael Foord's avatar Michael Foord

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

parent ee2df030
...@@ -509,6 +509,9 @@ changes, or look through the Subversion logs for all the details. ...@@ -509,6 +509,9 @@ changes, or look through the Subversion logs for all the details.
(automatically pass or fail without checking decimal places) if the objects (automatically pass or fail without checking decimal places) if the objects
are equal. 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 * A new hook, :meth:`addTypeEqualityFunc` takes a type object and a
function. The :meth:`assertEqual` method will use the function function. The :meth:`assertEqual` method will use the function
when both of the objects being compared are of the specified type. when both of the objects being compared are of the specified type.
......
...@@ -554,6 +554,47 @@ class Test_TestLoader(TestCase): ...@@ -554,6 +554,47 @@ class Test_TestLoader(TestCase):
self.assertTrue(isinstance(suite, loader.suiteClass)) self.assertTrue(isinstance(suite, loader.suiteClass))
self.assertEqual(list(suite), [testcase_1]) 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 # "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a TestCase or TestSuite instance" # ... a callable object which returns a TestCase or TestSuite instance"
# #
......
...@@ -85,7 +85,7 @@ class TestLoader(object): ...@@ -85,7 +85,7 @@ class TestLoader(object):
elif (isinstance(obj, types.UnboundMethodType) and elif (isinstance(obj, types.UnboundMethodType) and
isinstance(parent, type) and isinstance(parent, type) and
issubclass(parent, case.TestCase)): issubclass(parent, case.TestCase)):
return suite.TestSuite([parent(obj.__name__)]) return self.suiteClass([parent(obj.__name__)])
elif isinstance(obj, suite.TestSuite): elif isinstance(obj, suite.TestSuite):
return obj return obj
elif hasattr(obj, '__call__'): elif hasattr(obj, '__call__'):
...@@ -93,7 +93,7 @@ class TestLoader(object): ...@@ -93,7 +93,7 @@ class TestLoader(object):
if isinstance(test, suite.TestSuite): if isinstance(test, suite.TestSuite):
return test return test
elif isinstance(test, case.TestCase): elif isinstance(test, case.TestCase):
return suite.TestSuite([test]) return self.suiteClass([test])
else: else:
raise TypeError("calling %s returned %s, not a test" % raise TypeError("calling %s returned %s, not a test" %
(obj, test)) (obj, test))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment