Ticket #12191: tests_specify_module.diff
File tests_specify_module.diff, 4.5 KB (added by , 15 years ago) |
---|
-
django/test/simple.py
5 5 from django.test.utils import setup_test_environment, teardown_test_environment 6 6 from django.test.testcases import OutputChecker, DocTestRunner, TestCase 7 7 8 # The module name for tests outside models.py9 TEST_MODULE = 'tests'10 11 8 doctestOutputChecker = OutputChecker() 12 9 13 def get_tests(app_module ):10 def get_tests(app_module, submodule_name): 14 11 try: 15 12 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) 17 14 except ImportError, e: 18 15 # Couldn't import tests.py. Was it due to a missing file, or 19 16 # due to an import error in a tests.py that actually exists? 20 17 import os.path 21 18 from imp import find_module 22 19 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__)]) 24 21 except ImportError: 25 22 # 'tests' module doesn't exist. Move on. 26 23 test_module = None … … 55 52 56 53 # Check to see if a separate 'tests' module exists parallel to the 57 54 # models module 58 test_module = get_tests(app_module )55 test_module = get_tests(app_module, 'tests') 59 56 if test_module: 60 57 # Load unit and doctests in the tests.py module. If module has 61 58 # a suite() method, use it. Otherwise build the test suite ourselves. … … 79 76 80 77 """ 81 78 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) 84 81 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] 87 99 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) 91 112 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 93 116 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) 99 123 else: # label is app.TestClass.test_method 100 101 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) 103 127 104 128 # Python 2.3 compatibility: TestSuites were made iterable in 2.4. 105 129 # We need to iterate over them, so we add the missing method when