Ticket #12191: tests_specify_module.diff

File tests_specify_module.diff, 4.5 KB (added by dolapo, 15 years ago)
  • django/test/simple.py

     
    55from django.test.utils import setup_test_environment, teardown_test_environment
    66from django.test.testcases import OutputChecker, DocTestRunner, TestCase
    77
    8 # The module name for tests outside models.py
    9 TEST_MODULE = 'tests'
    10 
    118doctestOutputChecker = OutputChecker()
    129
    13 def get_tests(app_module):
     10def get_tests(app_module, submodule_name):
    1411    try:
    1512        app_path = app_module.__name__.split('.')[:-1]
    16         test_module = __import__('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE)
     13        test_module = __import__('.'.join(app_path + [submodule_name]), {}, {}, submodule_name)
    1714    except ImportError, e:
    1815        # Couldn't import tests.py. Was it due to a missing file, or
    1916        # due to an import error in a tests.py that actually exists?
    2017        import os.path
    2118        from imp import find_module
    2219        try:
    23             mod = find_module(TEST_MODULE, [os.path.dirname(app_module.__file__)])
     20            mod = find_module(submodule_name, [os.path.dirname(app_module.__file__)])
    2421        except ImportError:
    2522            # 'tests' module doesn't exist. Move on.
    2623            test_module = None
     
    5552
    5653    # Check to see if a separate 'tests' module exists parallel to the
    5754    # models module
    58     test_module = get_tests(app_module)
     55    test_module = get_tests(app_module, 'tests')
    5956    if test_module:
    6057        # Load unit and doctests in the tests.py module. If module has
    6158        # a suite() method, use it. Otherwise build the test suite ourselves.
     
    7976
    8077    """
    8178    parts = label.split('.')
    82     if len(parts) < 2 or len(parts) > 3:
    83         raise ValueError("Test label '%s' should be of the form app.TestCase or app.TestCase.test_method" % label)
     79    if len(parts) < 2 or len(parts) > 4:
     80        raise ValueError("Test label '%s' should be of the form app.TestCase or app.TestCase.test_method or app.module.TestCase.test_method" % label)
    8481
    85     app_module = get_app(parts[0])
    86     TestClass = getattr(app_module, parts[1], None)
     82    # parse out the configs.
     83    label_part_app = parts[0]
     84    label_part_module = None
     85    label_part_class = None
     86    label_part_method = None
     87    if len(parts) == 2:
     88        # app.TestClass
     89        label_part_class = parts[1]
     90    elif len(parts) == 3:
     91        # app.TestClass.test_method
     92        label_part_class = parts[1]
     93        label_part_method = parts[2]
     94    else:
     95        # app.module.TestClass.test_method
     96        label_part_module = parts[1]
     97        label_part_class = parts[2]
     98        label_part_method = parts[3]
    8799
    88     # Couldn't find the test class in models.py; look in tests.py
    89     if TestClass is None:
    90         test_module = get_tests(app_module)
     100    # Figure out where we're going to look for the TestCase
     101    # If it's not explicitly specified, look for it in .models or .tests by
     102    # default.
     103    modules_to_search = ['models', 'tests']
     104    if label_part_module is not None:
     105        modules_to_search = [label_part_module]
     106
     107
     108    app_module = get_app(label_part_app)
     109    TestClass = None
     110    for submodule_name in modules_to_search:
     111        test_module = get_tests(app_module, submodule_name)
    91112        if test_module:
    92             TestClass = getattr(test_module, parts[1], None)
     113            TestClass = getattr(test_module, label_part_class, None)
     114            if TestClass:
     115              break
    93116
    94     if len(parts) == 2: # label is app.TestClass
    95         try:
    96             return unittest.TestLoader().loadTestsFromTestCase(TestClass)
    97         except TypeError:
    98             raise ValueError("Test label '%s' does not refer to a test class" % label)
     117    if label_part_method is None or label_part_method == '*':
     118      # label specifies just a TestClass or the '*' indicates all tests.
     119      try:
     120          return unittest.TestLoader().loadTestsFromTestCase(TestClass)
     121      except TypeError:
     122          raise ValueError("Test label '%s' does not refer to a test class" % label)
    99123    else: # label is app.TestClass.test_method
    100         if not TestClass:
    101             raise ValueError("Test label '%s' does not refer to a test class" % label)
    102         return TestClass(parts[2])
     124      if not TestClass:
     125          raise ValueError("Test label '%s' does not refer to a test class" % label)
     126      return TestClass(label_part_method)
    103127
    104128# Python 2.3 compatibility: TestSuites were made iterable in 2.4.
    105129# We need to iterate over them, so we add the missing method when
Back to Top