code: def testcases_from_testmod(testmod):
      """Gather tests from a 'test_*' module.
  
      Returns a list of TestCase-subclass instances. One instance for each
      found test function.
  
      In general the normal unittest TestLoader.loadTests*() semantics are
      used for loading tests with some differences:
      - TestCase subclasses beginning with '_' are skipped (presumed to be
        internal).
      - If the module has a top-level "test_cases", it is called for a list of
        TestCase subclasses from which to load tests (can be a generator). This
        allows for run-time setup of test cases.
      - If the module has a top-level "test_suite_class", it is used to group
        all test cases from that module into an instance of that TestSuite
        subclass. This allows for overriding of test running behaviour.
      """
  
      class TestListLoader(unittest.TestLoader):
          suiteClass = list
  
      loader = TestListLoader()
      if hasattr(testmod, "test_cases"):
          try:
              for testcase_class in testmod.test_cases():
                  if testcase_class.__name__.startswith("_"):
                      log.debug(
                          "skip private TestCase class '%s'",
                          testcase_class.__name__,
                      )
                      continue
                  for testcase in loader.loadTestsFromTestCase(
                      testcase_class
                  ):
                      yield testcase
          except Exception:
              _, ex, _ = sys.exc_info()
              testmod_path = testmod.__file__
              if testmod_path.endswith(".pyc"):
                  testmod_path = testmod_path[:-1]
              log.warning(
                  "error running test_cases() in '%s': %s (skipping, "
                  "run with '-d' for full traceback)",
                  testmod_path,
                  ex,
              )
              if log.isEnabledFor(logging.DEBUG):
                  traceback.print_exc()
      else:
          class_names_skipped = []
          for testcases in loader.loadTestsFromModule(testmod):
              for testcase in testcases:
                  class_name = testcase.__class__.__name__
                  if class_name in class_names_skipped:
                      pass
                  elif class_name.startswith("_"):
                      log.debug(
                          "skip private TestCase class '%s'", class_name
                      )
                      class_names_skipped.append(class_name)
                  else:
                      yield testcase
  
imports: import os
  from os.path import join, basename, abspath, splitext, isfile, isdir, normpath, exists
  import sys
  import getopt
  import glob
  import time
  import unittest
  import importlib
  import logging
  import textwrap
  import traceback
label_desc: Use image node_id 5 for calling a global function with example usage: testcases_from_testmod(testmod) without return types