python (3.11.7)
       1  import os
       2  import sys
       3  import unittest
       4  
       5  from test import support
       6  
       7  from .filter import match_test, set_match_tests
       8  from .utils import (
       9      StrPath, TestName, TestTuple, TestList, TestFilter,
      10      abs_module_name, count, printlist)
      11  
      12  
      13  # If these test directories are encountered recurse into them and treat each
      14  # "test_*.py" file or each sub-directory as a separate test module. This can
      15  # increase parallelism.
      16  #
      17  # Beware this can't generally be done for any directory with sub-tests as the
      18  # __init__.py may do things which alter what tests are to be run.
      19  SPLITTESTDIRS: set[TestName] = {
      20      "test_asyncio",
      21      "test_concurrent_futures",
      22      "test_future_stmt",
      23      "test_gdb",
      24      "test_inspect",
      25      "test_multiprocessing_fork",
      26      "test_multiprocessing_forkserver",
      27      "test_multiprocessing_spawn",
      28  }
      29  
      30  
      31  def findtestdir(path: StrPath | None = None) -> StrPath:
      32      return path or os.path.dirname(os.path.dirname(__file__)) or os.curdir
      33  
      34  
      35  def findtests(*, testdir: StrPath | None = None, exclude=(),
      36                split_test_dirs: set[TestName] = SPLITTESTDIRS,
      37                base_mod: str = "") -> TestList:
      38      """Return a list of all applicable test modules."""
      39      testdir = findtestdir(testdir)
      40      tests = []
      41      for name in os.listdir(testdir):
      42          mod, ext = os.path.splitext(name)
      43          if (not mod.startswith("test_")) or (mod in exclude):
      44              continue
      45          if base_mod:
      46              fullname = f"{base_mod}.{mod}"
      47          else:
      48              fullname = mod
      49          if fullname in split_test_dirs:
      50              subdir = os.path.join(testdir, mod)
      51              if not base_mod:
      52                  fullname = f"test.{mod}"
      53              tests.extend(findtests(testdir=subdir, exclude=exclude,
      54                                     split_test_dirs=split_test_dirs,
      55                                     base_mod=fullname))
      56          elif ext in (".py", ""):
      57              tests.append(fullname)
      58      return sorted(tests)
      59  
      60  
      61  def split_test_packages(tests, *, testdir: StrPath | None = None, exclude=(),
      62                          split_test_dirs=SPLITTESTDIRS):
      63      testdir = findtestdir(testdir)
      64      splitted = []
      65      for name in tests:
      66          if name in split_test_dirs:
      67              subdir = os.path.join(testdir, name)
      68              splitted.extend(findtests(testdir=subdir, exclude=exclude,
      69                                        split_test_dirs=split_test_dirs,
      70                                        base_mod=name))
      71          else:
      72              splitted.append(name)
      73      return splitted
      74  
      75  
      76  def _list_cases(suite):
      77      for test in suite:
      78          if isinstance(test, unittest.loader._FailedTest):
      79              continue
      80          if isinstance(test, unittest.TestSuite):
      81              _list_cases(test)
      82          elif isinstance(test, unittest.TestCase):
      83              if match_test(test):
      84                  print(test.id())
      85  
      86  def list_cases(tests: TestTuple, *,
      87                 match_tests: TestFilter | None = None,
      88                 test_dir: StrPath | None = None):
      89      support.verbose = False
      90      set_match_tests(match_tests)
      91  
      92      skipped = []
      93      for test_name in tests:
      94          module_name = abs_module_name(test_name, test_dir)
      95          try:
      96              suite = unittest.defaultTestLoader.loadTestsFromName(module_name)
      97              _list_cases(suite)
      98          except unittest.SkipTest:
      99              skipped.append(test_name)
     100  
     101      if skipped:
     102          sys.stdout.flush()
     103          stderr = sys.stderr
     104          print(file=stderr)
     105          print(count(len(skipped), "test"), "skipped:", file=stderr)
     106          printlist(skipped, file=stderr)