Skip to content

Commit cd88704

Browse files
committed
Fix iterate_tests to handle non-TestSuite iterables like lists
iterate_tests was changed to use isinstance(unittest.TestSuite) instead of duck-typing with iter(), which broke callers like testscenarios that pass plain lists. Check for Iterable instead, excluding TestCase. Fixes #583
1 parent 9165a00 commit cd88704

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

tests/test_testsuite.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,23 @@ def test_two(self):
387387
self.assertRaises(ValueError, suite.sort_tests)
388388

389389

390+
class TestIterateTests(TestCase):
391+
def test_iterate_suite(self):
392+
a = PlaceHolder("a")
393+
b = PlaceHolder("b")
394+
suite = unittest.TestSuite([a, b]) # type: ignore[list-item]
395+
self.assertEqual([a, b], list(iterate_tests(suite)))
396+
397+
def test_iterate_single_test(self):
398+
a = PlaceHolder("a")
399+
self.assertEqual([a], list(iterate_tests(a)))
400+
401+
def test_iterate_list(self):
402+
a = PlaceHolder("a")
403+
b = PlaceHolder("b")
404+
self.assertEqual([a, b], list(iterate_tests([a, b])))
405+
406+
390407
class TestSortedTests(TestCase):
391408
def test_sorts_custom_suites(self):
392409
a = PlaceHolder("a")

testtools/testsuite.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ def iterate_tests(
5858
test_suite_or_case: TestSuiteOrCase,
5959
) -> Generator[unittest.TestCase, None, None]:
6060
"""Iterate through all of the test cases in 'test_suite_or_case'."""
61-
if isinstance(test_suite_or_case, unittest.TestSuite):
62-
# It's a suite, iterate through it
61+
if isinstance(test_suite_or_case, Iterable) and not isinstance(
62+
test_suite_or_case, unittest.TestCase
63+
):
6364
for test in test_suite_or_case:
6465
yield from iterate_tests(test)
6566
else:
66-
# It's a test case (could be unittest.TestCase or duck-typed)
6767
yield test_suite_or_case
6868

6969

0 commit comments

Comments
 (0)