Ticket #4501: 4501-coverage.diff

File 4501-coverage.diff, 2.7 KB (added by Eric Holscher, 16 years ago)

Initial patch

  • django/core/management/commands/test.py

    diff --git a/django/core/management/commands/test.py b/django/core/management/commands/test.py
    index 8ebf3da..b57a2f0 100644
    a b class Command(BaseCommand):  
    66    option_list = BaseCommand.option_list + (
    77        make_option('--noinput', action='store_false', dest='interactive', default=True,
    88            help='Tells Django to NOT prompt the user for input of any kind.'),
     9        make_option('--coverage', action='store_true', dest='coverage', default=False,
     10            help='Tells Django to run coverage analysis on your test modules.'),
    911    )
    1012    help = 'Runs the test suite for the specified applications, or the entire site if no apps are specified.'
    1113    args = '[appname ...]'
    class Command(BaseCommand):  
    1820
    1921        verbosity = int(options.get('verbosity', 1))
    2022        interactive = options.get('interactive', True)
     23        use_coverage = options.get('coverage', False)
    2124        test_runner = get_runner(settings)
    2225
     26
     27        if use_coverage:
     28            try:
     29                import coverage
     30                if verbosity > 1:
     31                    print "Running test runner with coverage."
     32                #coverage.use_cache(0)
     33                coverage.start()
     34            except:
     35                if verbosity > 0:
     36                    print "coverage.py module is not available."
     37                use_coverage = False
     38
    2339        failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive)
     40
    2441        if failures:
    2542            sys.exit(failures)
     43
     44        #We only want coverage when our tests are passing.
     45        if use_coverage:
     46            coverage.stop()
     47            coverage_models = []
     48            if test_labels:
     49                for label in test_labels:
     50                    if '.' in label:
     51                        #Test the app, don't know if we really want coverage here.
     52                        label = label.split('.')[0]
     53                    coverage_models.append(__import__(label, globals(), locals(), ['']))
     54            else:
     55                from django.db.models.loading import get_apps
     56                for app in get_apps():
     57                    if app.__name__.find('django.contrib') == -1:
     58                        coverage_models.append(app)
     59
     60
     61            print '---------------------------------------------------------------'
     62            print ' Unit Test Code Coverage Results'
     63            print '---------------------------------------------------------------'
     64
     65            coverage.report(coverage_models, show_missing=1)
     66
     67            print '---------------------------------------------------------------'
Back to Top