Ticket #8010: django-management-test_multiple-testcases-per-model.2.patch

File django-management-test_multiple-testcases-per-model.2.patch, 5.7 KB (added by esizikov, 6 years ago)
  • django/test/simple.py

     
    11import unittest
     2import os.path
     3import glob
     4from imp import find_module
    25from django.conf import settings
    36from django.db.models import get_app, get_apps
    47from django.test import _doctest as doctest
     
    1114doctestOutputChecker = OutputChecker()
    1215
    1316def get_tests(app_module):
    14     try:
    15         app_path = app_module.__name__.split('.')[:-1]
    16         test_module = __import__('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE)
    17     except ImportError, e:
    18         # Couldn't import tests.py. Was it due to a missing file, or
    19         # due to an import error in a tests.py that actually exists?
    20         import os.path
    21         from imp import find_module
     17    "Collect test cases for the provided application module"
     18    def _find_module(module_name, path):
    2219        try:
    23             mod = find_module(TEST_MODULE, [os.path.dirname(app_module.__file__)])
     20            mod = find_module(module_name, path)
    2421        except ImportError:
    2522            # 'tests' module doesn't exist. Move on.
    26             test_module = None
     23            pass
    2724        else:
    2825            # The module exists, so there must be an import error in the
    2926            # test module itself. We don't need the module; so if the
     
    3330            if mod[0]:
    3431                mod[0].close()
    3532            raise
    36     return test_module
    3733
     34    # Going to collect modules with test cases and return them at once.
     35    test_module_list = []
     36    try:
     37        app_path = app_module.__name__.split('.')[:-1]
     38        test_module = __import__('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE)
     39        test_module_list.append(test_module)
     40    except ImportError, e:
     41        # Couldn't import tests.py. Was it due to a missing file, or
     42        # due to an import error in a tests.py that actually exists?
     43        _find_module(TEST_MODULE, [os.path.dirname(app_module.__file__)])
     44
     45    # Search for separate files inside the tests/ directory.
     46    app_full_path = os.path.abspath(os.path.join(app_module.__file__, os.pardir))
     47    glob_pattern = os.path.join(app_full_path, TEST_MODULE, settings.TEST_MODULES_GLOB)
     48    for test_filename in glob.glob(glob_pattern):
     49        module_name, _ext = os.path.splitext(os.path.basename(test_filename))
     50        try:
     51            test_module_path = '.'.join(app_path + [TEST_MODULE, module_name])
     52            test_module = __import__(test_module_path, {}, {}, module_name)
     53            test_module_list.append(test_module)
     54        except ImportError, e:
     55            # Couldn't import test module. Was it due to a missing file, or
     56            # due to an import error in a test module that actually exists?
     57            _find_module(module_name, [os.path.join(os.path.dirname(app_module.__file__, TEST_MODULE))])
     58
     59    # Return collected modules.
     60    return test_module_list
     61
    3862def build_suite(app_module):
    3963    "Create a complete Django test suite for the provided application module"
    4064    suite = unittest.TestSuite()
    4165
    4266    # Load unit and doctests in the models.py module. If module has
    4367    # a suite() method, use it. Otherwise build the test suite ourselves.
    44     if hasattr(app_module, 'suite'):
    45         suite.addTest(app_module.suite())
    46     else:
    47         suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(app_module))
    48         try:
    49             suite.addTest(doctest.DocTestSuite(app_module,
    50                                                checker=doctestOutputChecker,
    51                                                runner=DocTestRunner))
    52         except ValueError:
    53             # No doc tests in models.py
    54             pass
    55 
    5668    # Check to see if a separate 'tests' module exists parallel to the
    57     # models module
    58     test_module = get_tests(app_module)
    59     if test_module:
    60         # Load unit and doctests in the tests.py module. If module has
     69    # models module.
     70    for test_module in [app_module] + get_tests(app_module):
     71        # Load unit and doctests in the models.py and test modules. If module has
    6172        # a suite() method, use it. Otherwise build the test suite ourselves.
    6273        if hasattr(test_module, 'suite'):
    6374            suite.addTest(test_module.suite())
     
    6879                                                   checker=doctestOutputChecker,
    6980                                                   runner=DocTestRunner))
    7081            except ValueError:
    71                 # No doc tests in tests.py
     82                # No doctests in the module
    7283                pass
    7384    return suite
    7485
     
    8596    app_module = get_app(parts[0])
    8697    TestClass = getattr(app_module, parts[1], None)
    8798
    88     # Couldn't find the test class in models.py; look in tests.py
     99    # Couldn't find the test class in models.py; look in test modules
    89100    if TestClass is None:
    90         test_module = get_tests(app_module)
    91         if test_module:
     101        for test_module in get_tests(app_module):
    92102            TestClass = getattr(test_module, parts[1], None)
     103            if TestClass is not None:
     104                break
    93105
    94106    if len(parts) == 2: # label is app.TestClass
    95107        try:
  • django/conf/global_settings.py

     
    392392TEST_DATABASE_CHARSET = None
    393393TEST_DATABASE_COLLATION = None
    394394
     395# The default naming convetions for files containing test cases for them
     396# to be automatically imported from the (hardcoded) tests/ directory.
     397TEST_MODULES_GLOB = 'test_*.py'
     398
    395399############
    396400# FIXTURES #
    397401############
Back to Top