Ticket #4501: 4501-coverage-2.diff
File 4501-coverage-2.diff, 6.5 KB (added by , 16 years ago) |
---|
-
django/core/management/commands/test.py
diff --git a/django/core/management/commands/test.py b/django/core/management/commands/test.py index 8ebf3da..d7f4caf 100644
a b 1 1 from django.core.management.base import BaseCommand 2 2 from optparse import make_option 3 3 import sys 4 from django.test.utils import start_coverage, stop_coverage 4 5 5 6 class Command(BaseCommand): 6 7 option_list = BaseCommand.option_list + ( 7 8 make_option('--noinput', action='store_false', dest='interactive', default=True, 8 9 help='Tells Django to NOT prompt the user for input of any kind.'), 10 make_option('--coverage', action='store_true', dest='coverage', default=False, 11 help='Tells Django to run coverage analysis on your test modules.'), 9 12 ) 10 13 help = 'Runs the test suite for the specified applications, or the entire site if no apps are specified.' 11 14 args = '[appname ...]' … … class Command(BaseCommand): 18 21 19 22 verbosity = int(options.get('verbosity', 1)) 20 23 interactive = options.get('interactive', True) 24 use_coverage = options.get('coverage', False) 21 25 test_runner = get_runner(settings) 22 26 23 failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive) 27 if use_coverage: 28 use_coverage = start_coverage(verbosity) 29 30 failures = test_runner(test_labels, verbosity=verbosity, 31 interactive=interactive) 32 24 33 if failures: 25 34 sys.exit(failures) 35 36 if use_coverage: 37 stop_coverage(test_labels) -
django/test/utils.py
diff --git a/django/test/utils.py b/django/test/utils.py index 29babec..3b70467 100644
a b def teardown_test_environment(): 65 65 66 66 del mail.outbox 67 67 68 69 68 def get_runner(settings): 70 69 test_path = settings.TEST_RUNNER.split('.') 71 70 # Allow for Python 2.5 relative paths … … def get_runner(settings): 76 75 test_module = __import__(test_module_name, {}, {}, test_path[-1]) 77 76 test_runner = getattr(test_module, test_path[-1]) 78 77 return test_runner 78 79 def start_coverage(verbosity=1): 80 """ 81 Try to start running coverage analysis. 82 This tries to import coverage.py, returning False if it can't import. 83 """ 84 try: 85 import coverage 86 if verbosity > 1: 87 print "Running test runner with coverage." 88 coverage.start() 89 except: 90 if verbosity > 0: 91 print "coverage.py module is not available." 92 return False 93 94 def stop_coverage(test_labels, exclude_admin=True): 95 #This was undefined if it wasn't imported. 96 #If we get to this point, coverage exists (since it was imported in start) 97 import coverage 98 coverage.stop() 99 coverage_models = [] 100 if test_labels: 101 for label in test_labels: 102 if '.' in label: 103 #Test the app, don't know if we really want coverage here. 104 label = label.split('.')[0] 105 try: 106 #This is for the common case of non-runtests 107 coverage_models.append(__import__(label, globals(), locals(), [''])) 108 continue 109 except: 110 #This is for the runtests case, this makes me feel that they 111 #should be seperated totally in code :/ 112 for app in ('modeltests','regressiontests'): 113 try: 114 coverage_models.append(__import__("%s.%s" % (app,label), globals(), locals(), [''])) 115 except: 116 pass 117 else: 118 from django.db.models.loading import get_apps 119 for app in get_apps(): 120 if not exclude_admin or app.__name__.find('django.contrib') == -1: 121 coverage_models.append(app) 122 print '---------------------------------------------------------------' 123 print ' Unit Test Code Coverage Results' 124 print '---------------------------------------------------------------' 125 coverage.report(coverage_models, show_missing=1) 126 print '---------------------------------------------------------------' -
tests/runtests.py
diff --git a/tests/runtests.py b/tests/runtests.py index bd7f59b..a7b3ba1 100755
a b class InvalidModelTestCase(unittest.TestCase): 85 85 self.assert_(not unexpected, "Unexpected Errors: " + '\n'.join(unexpected)) 86 86 self.assert_(not missing, "Missing Errors: " + '\n'.join(missing)) 87 87 88 def django_tests(verbosity, interactive, test_labels):88 def django_tests(verbosity, interactive, use_coverage, test_labels): 89 89 from django.conf import settings 90 from django.test.utils import start_coverage, stop_coverage 90 91 91 92 old_installed_apps = settings.INSTALLED_APPS 92 93 old_test_database_name = settings.TEST_DATABASE_NAME … … def django_tests(verbosity, interactive, test_labels): 154 155 settings.TEST_RUNNER = 'django.test.simple.run_tests' 155 156 test_runner = get_runner(settings) 156 157 158 if use_coverage: 159 use_coverage = start_coverage(verbosity) 160 157 161 failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive, extra_tests=extra_tests) 162 158 163 if failures: 159 164 sys.exit(failures) 160 165 166 if use_coverage: 167 stop_coverage(test_labels, exclude_admin=False) 168 161 169 # Restore the old settings. 162 170 settings.INSTALLED_APPS = old_installed_apps 163 171 settings.ROOT_URLCONF = old_root_urlconf … … if __name__ == "__main__": 176 184 help='Verbosity level; 0=minimal output, 1=normal output, 2=all output') 177 185 parser.add_option('--noinput', action='store_false', dest='interactive', default=True, 178 186 help='Tells Django to NOT prompt the user for input of any kind.') 187 parser.add_option('--coverage', action='store_true', dest='coverage', default=False, 188 help='Tells Django to run coverage analysis on your test modules.'), 179 189 parser.add_option('--settings', 180 190 help='Python path to settings module, e.g. "myproject.settings". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.') 181 191 options, args = parser.parse_args() … … if __name__ == "__main__": 184 194 elif "DJANGO_SETTINGS_MODULE" not in os.environ: 185 195 parser.error("DJANGO_SETTINGS_MODULE is not set in the environment. " 186 196 "Set it or use --settings.") 187 django_tests(int(options.verbosity), options.interactive, args)197 django_tests(int(options.verbosity), options.interactive, options.coverage, args)