Ticket #8010: django-management-test-multifile-tests.patch

File django-management-test-multifile-tests.patch, 4.1 KB (added by esizikov, 7 years ago)

multi-file test suites support for Django management infrastructure

  • django/test/simple.py

     
    1212doctestOutputChecker = OutputChecker()
    1313
    1414def get_tests(app_module):
     15    import os.path
     16    from imp import find_module
     17    import glob
     18
    1519    try:
    1620        app_path = app_module.__name__.split('.')[:-1]
    1721        test_module = __import__('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE)
     22        yield test_module
    1823    except ImportError, e:
    1924        # Couldn't import tests.py. Was it due to a missing file, or
    2025        # due to an import error in a tests.py that actually exists?
    21         import os.path
    22         from imp import find_module
    2326        try:
    2427            mod = find_module(TEST_MODULE, [os.path.dirname(app_module.__file__)])
    2528        except ImportError:
    2629            # 'tests' module doesn't exist. Move on.
    27             test_module = None
     30            pass
    2831        else:
    2932            # The module exists, so there must be an import error in the
    3033            # test module itself. We don't need the module; so if the
     
    3437            if mod[0]:
    3538                mod[0].close()
    3639            raise
    37     return test_module
     40
     41    app_path = app_module.__name__.split('.')[:-1]
     42    app_full_path = os.path.abspath(os.path.join(app_module.__file__, os.pardir))
     43    for test_filename in glob.glob(os.path.join(app_full_path, TEST_MODULE, 'test_*.py')):
     44        module_name, _ext = os.path.splitext(os.path.basename(test_filename))
     45        try:
     46            test_module_path = '.'.join(app_path + [TEST_MODULE, module_name])
     47            test_module = __import__(test_module_path, {}, {}, module_name)
     48            yield test_module
     49        except ImportError, e:
     50            # Couldn't import test module. Was it due to a missing file, or
     51            # due to an import error in a test module that actually exists?
     52            try:
     53                mod = find_module(module_name, [os.path.join(os.path.dirname(app_module.__file__, TEST_MODULE))])
     54            except ImportError:
     55                # 'tests' module doesn't exist. Move on.
     56                pass
     57            else:
     58                # The module exists, so there must be an import error in the
     59                # test module itself. We don't need the module; so if the
     60                # module was a single file module (i.e., test_*.py), close the file
     61                # handle returned by find_module. Otherwise, the test module
     62                # is a directory, and there is nothing to close.
     63                if mod[0]:
     64                    mod[0].close()
     65                raise
    3866   
    3967def build_suite(app_module):
    4068    "Create a complete Django test suite for the provided application module"
     
    5684   
    5785    # Check to see if a separate 'tests' module exists parallel to the
    5886    # models module
    59     test_module = get_tests(app_module)
    60     if test_module:
    61         # Load unit and doctests in the tests.py module. If module has
     87    for test_module in get_tests(app_module):
     88        # Load unit and doctests in test modules. If module has
    6289        # a suite() method, use it. Otherwise build the test suite ourselves.
    6390        if hasattr(test_module, 'suite'):
    6491            suite.addTest(test_module.suite())
     
    6996                                                   checker=doctestOutputChecker,
    7097                                                   runner=DocTestRunner))
    7198            except ValueError:
    72                 # No doc tests in tests.py
     99                # No doc tests in the test module
    73100                pass
    74101    return suite
    75102
     
    86113    app_module = get_app(parts[0])
    87114    TestClass = getattr(app_module, parts[1], None)
    88115
    89     # Couldn't find the test class in models.py; look in tests.py
    90     if TestClass is None:
    91         test_module = get_tests(app_module)
    92         if test_module:
     116    # Couldn't find the test class in models.py; look in test modules
     117    while TestClass is None:
     118        for test_module in get_tests(app_module):
    93119            TestClass = getattr(test_module, parts[1], None)
    94120
    95121    if len(parts) == 2: # label is app.TestClass
Back to Top