Ticket #8010: django-management-test-multifile-tests.patch
File django-management-test-multifile-tests.patch, 4.1 KB (added by , 16 years ago) |
---|
-
django/test/simple.py
12 12 doctestOutputChecker = OutputChecker() 13 13 14 14 def get_tests(app_module): 15 import os.path 16 from imp import find_module 17 import glob 18 15 19 try: 16 20 app_path = app_module.__name__.split('.')[:-1] 17 21 test_module = __import__('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE) 22 yield test_module 18 23 except ImportError, e: 19 24 # Couldn't import tests.py. Was it due to a missing file, or 20 25 # due to an import error in a tests.py that actually exists? 21 import os.path22 from imp import find_module23 26 try: 24 27 mod = find_module(TEST_MODULE, [os.path.dirname(app_module.__file__)]) 25 28 except ImportError: 26 29 # 'tests' module doesn't exist. Move on. 27 test_module = None30 pass 28 31 else: 29 32 # The module exists, so there must be an import error in the 30 33 # test module itself. We don't need the module; so if the … … 34 37 if mod[0]: 35 38 mod[0].close() 36 39 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 38 66 39 67 def build_suite(app_module): 40 68 "Create a complete Django test suite for the provided application module" … … 56 84 57 85 # Check to see if a separate 'tests' module exists parallel to the 58 86 # 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 62 89 # a suite() method, use it. Otherwise build the test suite ourselves. 63 90 if hasattr(test_module, 'suite'): 64 91 suite.addTest(test_module.suite()) … … 69 96 checker=doctestOutputChecker, 70 97 runner=DocTestRunner)) 71 98 except ValueError: 72 # No doc tests in t ests.py99 # No doc tests in the test module 73 100 pass 74 101 return suite 75 102 … … 86 113 app_module = get_app(parts[0]) 87 114 TestClass = getattr(app_module, parts[1], None) 88 115 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): 93 119 TestClass = getattr(test_module, parts[1], None) 94 120 95 121 if len(parts) == 2: # label is app.TestClass