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 , 15 years ago) |
---|
-
django/test/simple.py
1 1 import unittest 2 import os.path 3 import glob 4 from imp import find_module 2 5 from django.conf import settings 3 6 from django.db.models import get_app, get_apps 4 7 from django.test import _doctest as doctest … … 11 14 doctestOutputChecker = OutputChecker() 12 15 13 16 def 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): 22 19 try: 23 mod = find_module( TEST_MODULE, [os.path.dirname(app_module.__file__)])20 mod = find_module(module_name, path) 24 21 except ImportError: 25 22 # 'tests' module doesn't exist. Move on. 26 test_module = None23 pass 27 24 else: 28 25 # The module exists, so there must be an import error in the 29 26 # test module itself. We don't need the module; so if the … … 33 30 if mod[0]: 34 31 mod[0].close() 35 32 raise 36 return test_module37 33 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 38 62 def build_suite(app_module): 39 63 "Create a complete Django test suite for the provided application module" 40 64 suite = unittest.TestSuite() 41 65 42 66 # Load unit and doctests in the models.py module. If module has 43 67 # 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.py54 pass55 56 68 # 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 61 72 # a suite() method, use it. Otherwise build the test suite ourselves. 62 73 if hasattr(test_module, 'suite'): 63 74 suite.addTest(test_module.suite()) … … 68 79 checker=doctestOutputChecker, 69 80 runner=DocTestRunner)) 70 81 except ValueError: 71 # No doc tests in tests.py82 # No doctests in the module 72 83 pass 73 84 return suite 74 85 … … 85 96 app_module = get_app(parts[0]) 86 97 TestClass = getattr(app_module, parts[1], None) 87 98 88 # Couldn't find the test class in models.py; look in test s.py99 # Couldn't find the test class in models.py; look in test modules 89 100 if TestClass is None: 90 test_module = get_tests(app_module) 91 if test_module: 101 for test_module in get_tests(app_module): 92 102 TestClass = getattr(test_module, parts[1], None) 103 if TestClass is not None: 104 break 93 105 94 106 if len(parts) == 2: # label is app.TestClass 95 107 try: -
django/conf/global_settings.py
392 392 TEST_DATABASE_CHARSET = None 393 393 TEST_DATABASE_COLLATION = None 394 394 395 # The default naming convetions for files containing test cases for them 396 # to be automatically imported from the (hardcoded) tests/ directory. 397 TEST_MODULES_GLOB = 'test_*.py' 398 395 399 ############ 396 400 # FIXTURES # 397 401 ############