Refs #32655 -- Improved error if iter_test_cases() is passed a string.

This commit is contained in:
Chris Jerdonek 2021-06-29 13:02:57 -04:00 committed by Mariusz Felisiak
parent 5371ad1d14
commit 8bca838f4a
2 changed files with 16 additions and 1 deletions

View File

@ -242,6 +242,13 @@ def iter_test_cases(tests):
The tests argument can also be an iterable of TestCase objects.
"""
for test in tests:
if isinstance(test, str):
# Prevent an unfriendly RecursionError that can happen with
# strings.
raise TypeError(
f'Test {test!r} must be a test case or test suite not string '
f'(was found in {tests!r}).'
)
if isinstance(test, TestCase):
yield test
else:

View File

@ -36,7 +36,7 @@ class MySuite:
yield from self.tests
class TestSuiteTests(unittest.TestCase):
class TestSuiteTests(SimpleTestCase):
def build_test_suite(self, test_classes, suite=None, suite_class=None):
if suite_class is None:
suite_class = unittest.TestSuite
@ -89,6 +89,14 @@ class TestSuiteTests(unittest.TestCase):
'Tests1.test1', 'Tests1.test2', 'Tests2.test1', 'Tests2.test2',
])
def test_iter_test_cases_string_input(self):
msg = (
"Test 'a' must be a test case or test suite not string (was found "
"in 'abc')."
)
with self.assertRaisesMessage(TypeError, msg):
list(iter_test_cases('abc'))
def test_iter_test_cases_iterable_of_tests(self):
class Tests(unittest.TestCase):
def test1(self):