コード例 #1
0
ファイル: testrunner.py プロジェクト: blaxter/Bitten
    def _run_with_coverage(self):
        import coverage
        coverage.use_cache(False)
        coverage.start()
        try:
            self._run_tests()
        finally:
            coverage.stop()

            modules = [m for _, m in sys.modules.items()
                       if m is not None and hasattr(m, '__file__')
                       and os.path.splitext(m.__file__)[-1] in ('.py', '.pyc')]

            # Generate summary file
            buf = StringIO()
            coverage.report(modules, file=buf)
            buf.seek(0)
            fileobj = open(self.coverage_summary, 'w')
            try:
                filter_coverage(buf, fileobj)
            finally:
                fileobj.close()

            if self.coverage_dir:
                if not os.path.exists(self.coverage_dir):
                    os.makedirs(self.coverage_dir)
                coverage.annotate(modules, directory=self.coverage_dir,
                                  ignore_errors=True)
コード例 #2
0
def run_tests(test_labels, verbosity=1, interactive=True,
        extra_tests=[]):
    """
    Test runner which displays a code coverage report at the end of the
    run.
    """
    django_test_runner = get_runner(settings)
    coverage.use_cache(0)
    coverage.start()
    
    results = django_test_runner(test_labels, verbosity, interactive, 
        extra_tests)
    coverage.stop()

    coverage_modules = []
    if test_labels:
        for label in test_labels:
            # Don't report coverage if you're only running a single
            # test case.
            if '.' not in label:
                app = get_app(label)
                coverage_modules.extend(get_all_coverage_modules(app))
    else:
        for app in get_apps():
            coverage_modules.extend(get_all_coverage_modules(app))

    if coverage_modules:
        coverage.report(coverage_modules, show_missing=1)

    return results
コード例 #3
0
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True,
                              extra_tests=[]):
    # This doesn't work with Django 1.4
    from django.test.simple import run_tests as django_test_runner
    import coverage

    coverage.use_cache(0)
    coverage.start()

    test_results = django_test_runner(test_labels, verbosity, interactive,
                                      extra_tests)

    coverage.stop()
    coverage_modules = [m.__file__ for k, m in sys.modules.iteritems()
                        if m and k.split('.')[0] in test_labels
                        and 'test' not in k]
    print
    print '='*80
    print 'Coverage results for %s' % ', '.join(test_labels)
    print '='*80
    coverage.report(coverage_modules, show_missing=1)
    coverage_html_dir = getattr(settings, 'COVERAGE_HTML_DIR', None)
    if coverage_html_dir is not None:
        coverage._the_coverage.html_report(coverage_modules,
                                           coverage_html_dir)

    return test_results
コード例 #4
0
ファイル: testrunner.py プロジェクト: djangozoom/fixcity
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True,
                              extra_tests=[]):
    """Custom test runner.  Follows the django.test.simple.run_tests()
    interface."""

    do_coverage = hasattr(settings, 'COVERAGE_MODULES') and settings.COVERAGE_MODULES
    # Start code coverage before anything else if necessary
    if do_coverage:
        coverage.use_cache(0) # Do not cache any of the coverage.py stuff
        coverage.start()
 
    test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests)
 

    if do_coverage:
        coverage.stop()
        # Print code metrics header
        print
        print '-' * 60
        print ' Unit Test Code Coverage Results'
        print '-' * 60
 
        coverage_modules = []
        for module in settings.COVERAGE_MODULES:
            coverage_modules.append(__import__(module, globals(), locals(),
                                               ['']))
 
        coverage.report(coverage_modules, show_missing=1)
 
        # Print code metrics footer
        print '-' * 60
 
    return test_results
コード例 #5
0
ファイル: runners.py プロジェクト: rob-b/Grab
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]):
    """Custom test runner.  Follows the django.test.simple.run_tests() interface."""
    coverage.use_cache(0) # Do not cache any of the coverage.py stuff
    coverage.start()
    test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests)
    coverage.stop()

    coverage_modules = []
    if test_labels:
        for label in test_labels:
            # Don't report coverage if you're only running a single
            # test case.
            if '.' not in label:
                app = get_app(label)
                coverage_modules.extend(get_coverage_modules(app))
    else:
        for app in get_apps():
            coverage_modules.extend(get_coverage_modules(app))


    if coverage_modules:
        # Print code metrics header
        print ''
        print '----------------------------------------------------------------------'
        print ' Unit Test Code Coverage Results'
        print '----------------------------------------------------------------------'
        coverage.report(coverage_modules, show_missing=1)
        # Print code metrics footer
        print '----------------------------------------------------------------------'
    return test_results
コード例 #6
0
def run_tests(*args, **kwargs):
  """Custom test runner.  Follows the django.test.simple.run_tests()
  interface."""
  coverage.use_cache(0) # Do not cache any of the coverage.py stuff
  coverage.start()
 
  test_results = django_test_runner(*args, **kwargs)
 
  # Stop code coverage after tests have completed
  coverage.stop()
 
  # Print code metrics header
  print ''
  print '----------------------------------------------------------------------'
  print ' Unit Test Code Coverage Results'
  print '----------------------------------------------------------------------'
 
  # Report code coverage metrics
  coverage_modules = []
  for module in settings.COVERAGE_MODULES:
      coverage_modules.append(__import__(module, globals(), locals(), ['']))
 
  coverage.report(coverage_modules, show_missing=1)
 
  # Print code metrics footer
  print '----------------------------------------------------------------------'
  return test_results
コード例 #7
0
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]):
    """
    Custom test runner.  Follows the django.test.simple.run_tests() interface.
    """
    # Start code coverage before anything else if necessary
    if hasattr(settings, 'COVERAGE_MODULES'):
        coverage.use_cache(0) # Do not cache any of the coverage.py stuff
        coverage.start()

    test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests)

    # Stop code coverage after tests have completed
    if hasattr(settings, 'COVERAGE_MODULES'):
        coverage.stop()

    # Print code metrics header
    print ''
    print '----------------------------------------------------------------------'
    print ' Unit Test Code Coverage Results'
    print '----------------------------------------------------------------------'

    # Report code coverage metrics
    if hasattr(settings, 'COVERAGE_MODULES'):
        coverage_modules = []
        for module in settings.COVERAGE_MODULES:
            coverage_modules.append(__import__(module, globals(), locals(), ['']))
        coverage.report(coverage_modules, show_missing=1)
        # Print code metrics footer
        print '----------------------------------------------------------------------'

    return test_results
コード例 #8
0
ファイル: testrunner.py プロジェクト: pombredanne/trachacks
    def _run_with_coverage(self):
        import coverage
        coverage.use_cache(False)
        coverage.start()
        try:
            self._run_tests()
        finally:
            coverage.stop()

            modules = [
                m for _, m in sys.modules.items()
                if m is not None and hasattr(m, '__file__')
                and os.path.splitext(m.__file__)[-1] in ('.py', '.pyc')
            ]

            # Generate summary file
            buf = StringIO()
            coverage.report(modules, file=buf)
            buf.seek(0)
            fileobj = open(self.coverage_summary, 'w')
            try:
                filter_coverage(buf, fileobj)
            finally:
                fileobj.close()

            if self.coverage_dir:
                if not os.path.exists(self.coverage_dir):
                    os.makedirs(self.coverage_dir)
                coverage.annotate(modules,
                                  directory=self.coverage_dir,
                                  ignore_errors=True)
コード例 #9
0
ファイル: __init__.py プロジェクト: karthik519/ahkera
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]):
    """Custom test runner.  Follows the django.test.simple.run_tests() interface."""
    # Start code coverage before anything else if necessary
    coverage_modules = []
    for module_name in settings.COVERAGE_MODULES:
        app_name = module_name.split(".")[0]
        if (not test_labels) or (app_name in test_labels):
            coverage_modules.append(__import__(module_name, globals(), locals(), ['']))
 
    if hasattr(settings, 'COVERAGE_MODULES'):
        coverage.use_cache(0) # Do not cache any of the coverage.py stuff
        coverage.start()
 
    test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests)
 
    # Stop code coverage after tests have completed
    if hasattr(settings, 'COVERAGE_MODULES'):
        coverage.stop()
 
        # Print code metrics header
        print ''
        print '----------------------------------------------------------------------'
        print ' Unit Test Code Coverage Results'
        print '----------------------------------------------------------------------'
 
        coverage.report(coverage_modules, show_missing=1)
 
        # Print code metrics footer
        print '----------------------------------------------------------------------'
    else:
        print "No coverage modules defined in settings.py"
 
    return test_results
コード例 #10
0
def run_tests(*args, **kwargs):
    """Custom test runner.  Follows the django.test.simple.run_tests()
  interface."""
    coverage.use_cache(0)  # Do not cache any of the coverage.py stuff
    coverage.start()

    test_results = django_test_runner(*args, **kwargs)

    # Stop code coverage after tests have completed
    coverage.stop()

    # Print code metrics header
    print ''
    print '----------------------------------------------------------------------'
    print ' Unit Test Code Coverage Results'
    print '----------------------------------------------------------------------'

    # Report code coverage metrics
    coverage_modules = []
    for module in settings.COVERAGE_MODULES:
        coverage_modules.append(__import__(module, globals(), locals(), ['']))

    coverage.report(coverage_modules, show_missing=1)

    # Print code metrics footer
    print '----------------------------------------------------------------------'
    return test_results
コード例 #11
0
ファイル: test.py プロジェクト: jsquyres/mtt
def run_tests(verbosity):
    "Run test suite"

    # list all the files in the top level directory
    file_list = os.listdir(os.path.join(os.path.abspath(
        os.path.dirname(os.path.realpath(__file__)))))

    # list all the files in the tests directory
    test_list = os.listdir(os.path.join(os.path.abspath(
        os.path.dirname(os.path.realpath(__file__))), 'tests'))

    code_modules = []
    # loop over all the file names
    for file_name in file_list:
        extension = os.path.splitext(file_name)[-1]
        # if they are python files or the test runner
        if extension == '.py' and file_name != 'test.py':
            # work out the module name
            code_module_name = os.path.splitext(file_name)[0:-1][0]
            # now import the module
            module = __import__(code_module_name, globals(), locals(), 
                code_module_name)
            # and add it to the list of available modules
            code_modules.append(module)

    test_modules = []
    # loop over all the file names
    for file_name in test_list:
        extension = os.path.splitext(file_name)[-1]
        # if they are python files
        if extension == '.py':
            # work out the module name
            test_module_name = "tests." + os.path.splitext(file_name)[0:-1][0]
            # now import the module
            module = __import__(test_module_name, globals(), locals(), 
                test_module_name)
            # and add it to the list of available modules
            test_modules.append(module)
        
    # populate a test suite from the individual tests from the list of modules
    suite = unittest.TestSuite(map(
        unittest.defaultTestLoader.loadTestsFromModule, test_modules))

    # set up the test runner
    runner = unittest.TextTestRunner(verbosity=int(verbosity))
    
    # set up coverage reporting
    coverage.use_cache(0)
    coverage.start()
    
    # run the tests
    runner.run(suite)
    
    # stop coverage reporting
    coverage.stop()
    
    # output coverage report
    coverage.report(code_modules, show_missing=1)
コード例 #12
0
ファイル: testrunner.py プロジェクト: B-Rich/languagelearning
    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        self.enable_coverage = hasattr(settings, u'COVERAGE_MODULES')

        if self.enable_coverage:
            coverage.use_cache(0)
            coverage.start()
     
        super(SelectiveTestSuiteRunnerWithCoverage, self)\
                            .run_tests(test_labels, extra_tests=None, **kwargs)
コード例 #13
0
def run_tests(test_labels, verbosity=1, interactive=True,
              extra_tests=[]):
    """
    Test runner which displays a code coverage report at the end of the
    run.
    """
    coverage.use_cache(0)
    for e in settings.COVERAGE_CODE_EXCLUDES:
        coverage.exclude(e)
    coverage.start()
    results = base_run_tests(test_labels, verbosity, interactive, extra_tests)
    coverage.stop()

    coverage_modules = []
    if test_labels:
        for label in test_labels:
            label = label.split('.')[0]
            app = get_app(label)
            coverage_modules.append(_get_app_package(app))
    else:
        for app in get_apps():
            coverage_modules.append(_get_app_package(app))

    coverage_modules.extend(settings.COVERAGE_ADDITIONAL_MODULES)

    packages, modules, excludes, errors = get_all_modules(
        coverage_modules, settings.COVERAGE_MODULE_EXCLUDES,
        settings.COVERAGE_PATH_EXCLUDES)

    outdir = settings.COVERAGE_REPORT_HTML_OUTPUT_DIR
    if outdir is None:
        coverage.report(modules.values(), show_missing=1)
        if excludes:
            print >>sys.stdout
            print >>sys.stdout, "The following packages or modules were excluded:",
            for e in excludes:
                print >>sys.stdout, e,
            print >>sys.stdout
        if errors:
            print >>sys.stdout
            print >>sys.stderr, "There were problems with the following packages or modules:",
            for e in errors:
                print >>sys.stderr, e,
            print >>sys.stdout
    else:
        outdir = os.path.abspath(outdir)
        if settings.COVERAGE_CUSTOM_REPORTS:
            html_report(outdir, modules, excludes, errors)
        else:
            coverage._the_coverage.html_report(modules.values(), outdir)
            coverage._the_coverage.xml_report(modules.values(), os.path.join(outdir,'coverage.xml') )
        print >>sys.stdout
        print >>sys.stdout, "HTML reports were output to '%s'" %outdir

    return results
コード例 #14
0
    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        coverage.use_cache(settings.COVERAGE_USE_CACHE)
        for e in settings.COVERAGE_CODE_EXCLUDES:
            coverage.exclude(e)
        coverage.start()
        results = super(CoverageRunner, self).run_tests(test_labels,
                                                        extra_tests, **kwargs)
        coverage.stop()

        coverage_modules = []
        if test_labels:
            for label in test_labels:
                label = label.split('.')[0]
                app = get_app(label)
                coverage_modules.append(self._get_app_package(app))
        else:
            for app in get_apps():
                coverage_modules.append(self._get_app_package(app))

        coverage_modules.extend(settings.COVERAGE_ADDITIONAL_MODULES)

        packages, modules, excludes, errors = get_all_modules(
            coverage_modules, settings.COVERAGE_MODULE_EXCLUDES,
            settings.COVERAGE_PATH_EXCLUDES)

        if settings.COVERAGE_USE_STDOUT:
            coverage.report(modules.values(), show_missing=1)
            if excludes:
                message = "The following packages or modules were excluded:"
                print >>sys.stdout
                print >>sys.stdout, message,
                for e in excludes:
                    print >>sys.stdout, e,
                print >>sys.stdout
            if errors:
                message = "There were problems with the following packages "
                message += "or modules:"
                print >>sys.stdout
                print >>sys.stderr, message,
                for e in errors:
                    print >>sys.stderr, e,
                print >>sys.stdout

        outdir = settings.COVERAGE_REPORT_HTML_OUTPUT_DIR
        if outdir:
            outdir = os.path.abspath(outdir)
            if settings.COVERAGE_CUSTOM_REPORTS:
                html_report(outdir, modules, excludes, errors)
            else:
                coverage._the_coverage.html_report(modules.values(), outdir)
            print >>sys.stdout
            print >>sys.stdout, "HTML reports were output to '%s'" %outdir

        return results
コード例 #15
0
ファイル: tests.py プロジェクト: pombredanne/django-project
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]):
    import coverage
    from django.test.simple import run_tests as django_test_runner
    from django.conf import settings

    coverage.use_cache(0)  # Do not cache any of the coverage.py stuff
    coverage.start()
    test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests)
    coverage.stop()
    coverage_modules = []
    for module in settings.COVERAGE_MODULES:
        coverage_modules.append(__import__(module, globals(), locals(), [""]))
    coverage.report(coverage_modules, show_missing=0)
    return test_results
コード例 #16
0
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
    """
    Test runner which displays a code coverage report at the end of the
    run.
    """
    coverage.use_cache(0)
    for e in getattr(settings, 'COVERAGE_CODE_EXCLUDES', []):
        coverage.exclude(e)
    coverage.start()
    results = base_run_tests(test_labels, verbosity, interactive, extra_tests)
    coverage.stop()

    coverage_modules = []
    if test_labels:
        for label in test_labels:
            label = label.split('.')[0]
            app = get_app(label)
            coverage_modules.append(_get_app_package(app))
    else:
        for app in get_apps():
            coverage_modules.append(_get_app_package(app))

    coverage_modules.extend(
        getattr(settings, 'COVERAGE_ADDITIONAL_MODULES', []))

    packages, modules, excludes, errors = get_all_modules(
        coverage_modules, getattr(settings, 'COVERAGE_MODULE_EXCLUDES', []),
        getattr(settings, 'COVERAGE_PATH_EXCLUDES', []))

    coverage.report(modules.values(), show_missing=1)
    if excludes:
        print >> sys.stdout
        print >> sys.stdout, "The following packages or modules were excluded:",
        for e in excludes:
            print >> sys.stdout, e,
        print >> sys.stdout
    if errors:
        print >> sys.stdout
        print >> sys.stderr, "There were problems with the following packages or modules:",
        for e in errors:
            print >> sys.stderr, e,
        print >> sys.stdout

    outdir = getattr(settings, 'COVERAGE_REPORT_HTML_OUTPUT_DIR', 'test_html')
    outdir = os.path.abspath(outdir)
    html_report(outdir, modules, excludes, errors)
    print >> sys.stdout
    print >> sys.stdout, "HTML reports were output to '%s'" % outdir

    return results
コード例 #17
0
def main():
    # Create the option parser.
    optparser = OptionParser(usage='%prog [options] NAME ...',
                             version="Edloper's Doctest Driver, "
                             "version %s" % __version__)

    action_group = OptionGroup(optparser, 'Actions (default=check)')
    action_group.add_options([CHECK_OPT, UPDATE_OPT, DEBUG_OPT])
    optparser.add_option_group(action_group)

    reporting_group = OptionGroup(optparser, 'Reporting')
    reporting_group.add_options([
        VERBOSE_OPT, QUIET_OPT, UDIFF_OPT, CDIFF_OPT, NDIFF_OPT, COVERAGE_OPT,
        CONTINUE_OPT
    ])
    optparser.add_option_group(reporting_group)

    compare_group = OptionGroup(optparser, 'Output Comparison')
    compare_group.add_options([ELLIPSIS_OPT, NORMWS_OPT])
    optparser.add_option_group(compare_group)

    # Extract optionflags and the list of file names.
    optionvals, names = optparser.parse_args()
    if len(names) == 0:
        optparser.error("No files specified")
    optionflags = (optionvals.udiff * REPORT_UDIFF
                   | optionvals.cdiff * REPORT_CDIFF
                   | optionvals.ellipsis * ELLIPSIS
                   | optionvals.normws * NORMALIZE_WHITESPACE)

    # Check coverage, if requested
    if optionvals.coverage:
        coverage.use_cache(True, cache_file=optionvals.coverage)
        coverage.start()

    # Perform the requested action.
    if optionvals.action == 'check':
        run(names, optionflags, optionvals.verbosity,
            optionvals.kbinterrupt_continue)
    elif optionvals.action == 'update':
        update(names, optionflags, optionvals.verbosity)
    elif optionvals.action == 'debug':
        debug(names, optionflags, optionvals.verbosity)
    else:
        optparser.error('INTERNAL ERROR: Bad action %s' % optionvals.action)

    # Check coverage, if requested
    if optionvals.coverage:
        coverage.stop()
コード例 #18
0
ファイル: doctest_driver.py プロジェクト: yochananmkp/clir
def main():
    # Create the option parser.
    optparser = OptionParser(usage='%prog [options] NAME ...',
                             version="Edloper's Doctest Driver, "
                                     "version %s" % __version__)

    action_group = OptionGroup(optparser, 'Actions (default=check)')
    action_group.add_options([CHECK_OPT, UPDATE_OPT, DEBUG_OPT])
    optparser.add_option_group(action_group)

    reporting_group = OptionGroup(optparser, 'Reporting')
    reporting_group.add_options([VERBOSE_OPT, QUIET_OPT,
                                 UDIFF_OPT, CDIFF_OPT, NDIFF_OPT,
                                 COVERAGE_OPT, CONTINUE_OPT])
    optparser.add_option_group(reporting_group)

    compare_group = OptionGroup(optparser, 'Output Comparison')
    compare_group.add_options([ELLIPSIS_OPT, NORMWS_OPT])
    optparser.add_option_group(compare_group)

    # Extract optionflags and the list of file names.
    optionvals, names = optparser.parse_args()
    if len(names) == 0:
        optparser.error("No files specified")
    optionflags = (optionvals.udiff * REPORT_UDIFF |
                   optionvals.cdiff * REPORT_CDIFF |
                   optionvals.ellipsis * ELLIPSIS |
                   optionvals.normws * NORMALIZE_WHITESPACE)

    # Check coverage, if requested
    if optionvals.coverage:
        coverage.use_cache(True, cache_file=optionvals.coverage)
        coverage.start()

    # Perform the requested action.
    if optionvals.action == 'check':
        run(names, optionflags, optionvals.verbosity,
            optionvals.kbinterrupt_continue)
    elif optionvals.action == 'update':
        update(names, optionflags, optionvals.verbosity)
    elif optionvals.action == 'debug':
        debug(names, optionflags, optionvals.verbosity)
    else:
        optparser.error('INTERNAL ERROR: Bad action %s' % optionvals.action)

    # Check coverage, if requested
    if optionvals.coverage:
        coverage.stop()
コード例 #19
0
ファイル: django_coverage.py プロジェクト: pakal/django-zodb
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]):
    coverage_enabled = coverage and hasattr(settings, 'COVERAGE_MODULES')

    if coverage_enabled:
        coverage.use_cache(0)
        coverage.start()

    test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests)

    if coverage_enabled:
        coverage.stop()
        coverage_modules = []
        for module in settings.COVERAGE_MODULES:
            coverage_modules.append(__import__(module, globals(), locals(), ['']))
        coverage.report(coverage_modules, show_missing=1)

    return test_results
コード例 #20
0
def test_runner_with_coverage(test_labels,
                              verbosity=1,
                              interactive=True,
                              extra_tests=[]):
    """Custom test runner.  Follows the django.test.simple.run_tests() interface."""
    import os, shutil, sys

    # Look for coverage.py in __file__/lib as well as sys.path
    sys.path = [os.path.join(os.path.dirname(__file__), "lib")] + sys.path

    import coverage
    from django.test.simple import run_tests as django_test_runner

    from django.conf import settings

    # Start code coverage before anything else if necessary
    #if hasattr(settings, 'COVERAGE_MODULES') and not test_labels:
    coverage.use_cache(0)  # Do not cache any of the coverage.py stuff
    coverage.start()

    test_results = django_test_runner(test_labels, verbosity, interactive,
                                      extra_tests)

    # Stop code coverage after tests have completed
    #if hasattr(settings, 'COVERAGE_MODULES') and not test_labels:
    coverage.stop()

    # Print code metrics header
    print ''
    print '----------------------------------------------------------------------'
    print ' Unit Test Code Coverage Results'
    print '----------------------------------------------------------------------'

    # Report code coverage metrics
    coverage_modules = []
    if hasattr(settings, 'COVERAGE_MODULES') and (not test_labels
                                                  or 'cms' in test_labels):
        for module in settings.COVERAGE_MODULES:
            coverage_modules.append(
                __import__(module, globals(), locals(), ['']))
    coverage.report(coverage_modules, show_missing=1)
    #Print code metrics footer
    print '----------------------------------------------------------------------'

    return test_results
コード例 #21
0
    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        run_with_coverage = hasattr(settings, 'COVERAGE_MODULES')

        # Set DEFAULT_TEST_LABELS to run just the tests we want
        # when we don't give 'test' any arguments
        if not test_labels:
            test_labels = getattr(settings, 'DEFAULT_TEST_LABELS', [])
        # If we've set DEFAULT_TEST_LABELS and we want to run them all,
        # just say 'test all'
        elif list(test_labels) == ['all']:
            test_labels = []

        if run_with_coverage:
            import coverage
            coverage.use_cache(0)
            coverage.start()

        result = super(CoverageRunner, self).run_tests(test_labels,
                                                       extra_tests, **kwargs)

        if run_with_coverage:
            coverage.stop()
            coverage_modules = []
            if test_labels:
                for label in test_labels:
                    # Don't report coverage if you're only running a single
                    # test case.
                    if '.' not in label:
                        app = get_app(label)
                        coverage_modules.extend(self.get_coverage_modules(app))
            else:
                for app in get_apps():
                    coverage_modules.extend(self.get_coverage_modules(app))

            if coverage_modules:
                print ''
                print '----------------------------------------------------------------------'
                print ' Unit Test Code Coverage Results'
                print '----------------------------------------------------------------------'
                coverage.report(coverage_modules, show_missing=1)
                print '----------------------------------------------------------------------'

        return result
コード例 #22
0
ファイル: test.py プロジェクト: MechanisM/django-hilbert
    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        run_with_coverage = hasattr(settings, 'COVERAGE_MODULES')

        # Set DEFAULT_TEST_LABELS to run just the tests we want
        # when we don't give 'test' any arguments
        if not test_labels:
            test_labels = getattr(settings, 'DEFAULT_TEST_LABELS', [])
        # If we've set DEFAULT_TEST_LABELS and we want to run them all,
        # just say 'test all'
        elif list(test_labels) == ['all']:
            test_labels = []

        if run_with_coverage:
            import coverage
            coverage.use_cache(0)
            coverage.start()

        result = super(CoverageRunner, self).run_tests(test_labels, extra_tests, **kwargs)

        if run_with_coverage:
            coverage.stop()
            coverage_modules = []
            if test_labels:
                for label in test_labels:
                    # Don't report coverage if you're only running a single
                    # test case.
                    if '.' not in label:
                        app = get_app(label)
                        coverage_modules.extend(self.get_coverage_modules(app))
            else:
                for app in get_apps():
                    coverage_modules.extend(self.get_coverage_modules(app))

            if coverage_modules:
                print ''
                print '----------------------------------------------------------------------'
                print ' Unit Test Code Coverage Results'
                print '----------------------------------------------------------------------'
                coverage.report(coverage_modules, show_missing=1)
                print '----------------------------------------------------------------------'

        return result
コード例 #23
0
def run_tests_with_coverage(test_labels,
                            verbosity=1,
                            interactive=True,
                            extra_tests=[]):
    """
    Test runner which displays a code coverage report at the end of the
    run.  It will not display coverage if:

    a) The COVERAGE_MODULES setting is not set.
    b) A specific app is being tested.
    """
    if hasattr(settings, 'COVERAGE_MODULES') and not test_labels:
        enable_coverage = True
    else:
        enable_coverage = False

    if enable_coverage:
        coverage.use_cache(0)
        coverage.start()

    results = run_tests(test_labels, verbosity, interactive, extra_tests)

    if enable_coverage:
        coverage.stop()

        print '-------------------------------------------------'
        print 'Coverage'
        print '-------------------------------------------------'

        # Report coverage
        coverage_modules = []
        for module in settings.COVERAGE_MODULES:
            coverage_modules.append(
                __import__(module, globals(), locals(), ['']))

        coverage.report(coverage_modules, show_missing=1)

        print '-------------------------------------------------'

    return results
コード例 #24
0
ファイル: manager.py プロジェクト: wadoon/pytagsfs
    def run(self, test_names = None, print_only = False, coverage = False):
        if 'pytagsfs' in sys.modules:
            raise AssertionError(
              'pytagsfs already imported; '
              'this interferes with coverage analysis'
            )

        if coverage:
            import coverage as _coverage

            if int(_coverage.__version__.split('.')[0]) < 3:
                print >>sys.stderr, (
                  'warning: coverage versions < 3 '
                  'are known to produce imperfect results'
                )

            _coverage.use_cache(False)
            _coverage.start()

        try:
            self.load()

            suite = TestSuite()
            runner = TextTestRunner(verbosity = 2)

            for test in self.tests:
                if self.should_run_test(test, test_names):
                    if print_only:
                        print test.id()
                    else:
                        suite.addTest(test)

            if not print_only:
                return runner.run(suite)

        finally:
            if coverage:
                _coverage.stop()
                import pytagsfs
                _coverage.report(self.find_modules(pytagsfs))
コード例 #25
0
ファイル: __init__.py プロジェクト: srj55/django-cms
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]):
    """Custom test runner.  Follows the django.test.simple.run_tests() interface."""
        
    import os, sys
     
    # Look for coverage.py in __file__/lib as well as sys.path
    sys.path = [os.path.join(os.path.dirname(__file__), "lib")] + sys.path

    import coverage
    from django.test.utils import get_runner
    from django.conf import settings
    
    django_test_runner = get_runner(settings)(verbosity=verbosity, interactive=interactive)
    
    # Start code coverage before anything else if necessary
    #if hasattr(settings, 'COVERAGE_MODULES') and not test_labels:
    coverage.use_cache(0) # Do not cache any of the coverage.py stuff
    coverage.start()
 
    test_results = django_test_runner(test_labels, extra_tests=extra_tests)
 
    return test_results
コード例 #26
0
ファイル: __init__.py プロジェクト: FrankBie/django-seo-link
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]):
    """Custom test runner.  Follows the django.test.simple.run_tests() interface."""
    import os, shutil, sys

    # Look for coverage.py in __file__/lib as well as sys.path
    sys.path = [os.path.join(os.path.dirname(__file__), "lib")] + sys.path
     
    import coverage
    from django.test.simple import run_tests as django_test_runner
     
    from django.conf import settings
    
    # Start code coverage before anything else if necessary
    #if hasattr(settings, 'COVERAGE_MODULES') and not test_labels:
    coverage.use_cache(0) # Do not cache any of the coverage.py stuff
    coverage.start()

    test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests)

    # Stop code coverage after tests have completed
    #if hasattr(settings, 'COVERAGE_MODULES') and not test_labels:
    coverage.stop()

    # Print code metrics header
    print ''
    print '----------------------------------------------------------------------'
    print ' Unit Test Code Coverage Results'
    print '----------------------------------------------------------------------'

    # Report code coverage metrics
    coverage_modules = []
    if hasattr(settings, 'COVERAGE_MODULES') and (not test_labels):
        for module in settings.COVERAGE_MODULES:
            coverage_modules.append(__import__(module, globals(), locals(), ['']))
    coverage.report(coverage_modules, show_missing=1)
            #Print code metrics footer
    print '----------------------------------------------------------------------'

    return test_results
コード例 #27
0
ファイル: test_utils.py プロジェクト: tow/feincms
    def run_tests(self, *args, **kwargs):
        run_with_coverage = hasattr(settings, 'COVERAGE_MODULES')

        if run_with_coverage:
            coverage.use_cache(0)
            coverage.start()

        result = super(CoverageRunner, self).run_tests(*args, **kwargs)

        if run_with_coverage:
            coverage.stop()
            print ''
            print '----------------------------------------------------------------------'
            print ' Unit Test Code Coverage Results'
            print '----------------------------------------------------------------------'
            coverage_modules = []
            for module in settings.COVERAGE_MODULES:
                coverage_modules.append(__import__(module, globals(),
                                                   locals(), ['']))
            coverage.report(coverage_modules, show_missing=1)
            print '----------------------------------------------------------------------'

        return result
コード例 #28
0
ファイル: test_runner.py プロジェクト: ATG618/plata
    def run_tests(self, *args, **kwargs):
        run_with_coverage = hasattr(settings, 'COVERAGE_MODULES')

        if run_with_coverage:
            coverage.use_cache(0)
            coverage.start()

        result = super(CoverageRunner, self).run_tests(*args, **kwargs)

        if run_with_coverage:
            coverage.stop()
            print ''
            print '----------------------------------------------------------------------'
            print ' Unit Test Code Coverage Results'
            print '----------------------------------------------------------------------'
            coverage_modules = []
            for module in settings.COVERAGE_MODULES:
                coverage_modules.append(__import__(module, globals(),
                                                   locals(), ['']))
            coverage.report(coverage_modules, show_missing=1)
            print '----------------------------------------------------------------------'

        return result
コード例 #29
0
def test_runner(test_labels, verbosity=1, interactive=True, extra_tests=[]):
    coveragemodules = []
    if hasattr(settings, 'COVERAGE_MODULES'):
        coveragemodules = settings.COVERAGE_MODULES
    if coveragemodules and not test_labels:
        coverage.use_cache(0)
        coverage.start()

    test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests)

    if coveragemodules and not test_labels:
        coverage.stop()
        print ''
        print '----------------------------------------------------------------------'
        print ' Unit Test Code Coverage Results'
        print '----------------------------------------------------------------------'
        modules = []
        for module_string in coveragemodules:
            modules.append(__import__(module_string, globals(), locals(), [""]))
        coverage.report(modules, show_missing=1)
        print '----------------------------------------------------------------------'

    return test_results
コード例 #30
0
ファイル: tests.py プロジェクト: B-Rich/cardstories
def run_tests_with_coverage(test_labels, verbosity=1, interactive=True,
                            extra_tests=[]):
    """
    Test runner which displays a code coverage report at the end of the
    run.  It will not display coverage if:

    a) The COVERAGE_MODULES setting is not set.
    b) A specific app is being tested.
    """
    if hasattr(settings, 'COVERAGE_MODULES') and not test_labels:
        enable_coverage = True
    else:
        enable_coverage = False

    if enable_coverage:
        coverage.use_cache(0)
        coverage.start()
 
    results = run_tests(test_labels, verbosity, interactive, extra_tests)
 
    if enable_coverage:
        coverage.stop()
 
        print '-------------------------------------------------'
        print 'Coverage'
        print '-------------------------------------------------'
 
        # Report coverage
        coverage_modules = []
        for module in settings.COVERAGE_MODULES:
            coverage_modules.append(__import__(module, globals(), locals(), ['']))
 
        coverage.report(coverage_modules, show_missing=1)
 
        print '-------------------------------------------------'
 
    return results
コード例 #31
0
def run_tests(test_labels, verbosity=1, interactive=True,
              extra_tests=[]):
    """
    Test runner which displays a code coverage report at the end of the
    run.
    """
    ##############################
    setup_funcs, teardown_funcs = get_test_enviroment_functions()
    # Prepare django for testing.
    setup_test_environment(setup_funcs)
    old_db_name = settings.DATABASE_NAME
    connection.creation.create_test_db(verbosity, autoclobber=not interactive)

    # Pretend it's a production environment.
    settings.DEBUG = False

    nose_argv = ['nosetests']
    if hasattr(settings, 'NOSE_ARGS'):
        nose_argv.extend(settings.NOSE_ARGS)

    # Everything after '--' is passed to nose.
    if '--' in sys.argv:
        hyphen_pos = sys.argv.index('--')
        nose_argv.extend(sys.argv[hyphen_pos + 1:])

    if verbosity >= 1:
        print ' '.join(nose_argv)

    ############################

    coverage.use_cache(0)
    for e in settings.COVERAGE_CODE_EXCLUDES:
        coverage.exclude(e)
    coverage.start()
    nose.run(argv=nose_argv)    
    #results = base_run_tests(test_labels, verbosity, interactive, extra_tests)
    coverage.stop()

    coverage_modules = []
    if test_labels:
        for label in test_labels:
            label = label.split('.')[0]
            app = get_app(label)
            coverage_modules.append(_get_app_package(app))
    else:
        for app in get_apps():
            coverage_modules.append(_get_app_package(app))

    coverage_modules.extend(settings.COVERAGE_ADDITIONAL_MODULES)

    packages, modules, excludes, errors = get_all_modules(
        coverage_modules, settings.COVERAGE_MODULE_EXCLUDES,
        settings.COVERAGE_PATH_EXCLUDES)

    outdir = settings.COVERAGE_REPORT_HTML_OUTPUT_DIR
    if outdir is None:
        coverage.report(modules.values(), show_missing=1)
        if excludes:
            print >>sys.stdout
            print >>sys.stdout, "The following packages or modules were excluded:",
            for e in excludes:
                print >>sys.stdout, e,
            print >>sys.stdout
        if errors:
            print >>sys.stdout
            print >>sys.stderr, "There were problems with the following packages or modules:",
            for e in errors:
                print >>sys.stderr, e,
            print >>sys.stdout
    else:
        outdir = os.path.abspath(outdir)
        if settings.COVERAGE_CUSTOM_REPORTS:
            html_report(outdir, modules, excludes, errors)
        else:
            coverage._the_coverage.html_report(modules.values(), outdir)
        print >>sys.stdout
        print >>sys.stdout, "HTML reports were output to '%s'" %outdir

    ###########################
    # Clean up django.
    connection.creation.destroy_test_db(old_db_name, verbosity)
    teardown_test_environment(teardown_funcs)
    ###########################

    return results
コード例 #32
0
ファイル: tests.py プロジェクト: pombredanne/signalbox
def test_runner_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[]):
  """Custom test runner.  Follows the django.test.simple.run_tests() interface."""
  print "cs test_runner_with_coverage " + str(getattr(settings, 'COVERAGE_MODULES')) + " , " + str(test_labels)
  
  coverage.use_cache(0) # Do not cache any of the coverage.py stuff
  coverage.start()
  
  # Start code coverage before anything else if necessary
#   if hasattr(settings, 'COVERAGE_MODULES') and not test_labels:
#     print "cs coverage start"
#     coverage.use_cache(0) # Do not cache any of the coverage.py stuff
#     coverage.start()

  test_results = django_test_runner(test_labels, verbosity, interactive, extra_tests)


  coverage.stop()

  if not os.path.exists(settings.COVERAGE_DIR):
    os.makedirs(settings.COVERAGE_DIR)

  coverage.html_report(directory='coverage')

  coverage.erase();

  # Stop code coverage after tests have completed
#   if hasattr(settings, 'COVERAGE_MODULES') and not test_labels:
#     coverage.stop()
# 
#     # Print code metrics header
#     print ''
#     print '----------------------------------------------------------------------'
#     print ' Unit Test Code Coverage Results'
#     print '----------------------------------------------------------------------'

#   coverage_modules = []
#   for module in settings.COVERAGE_MODULES:
#     coverage_modules.append(__import__(module, globals(), locals(), ['']))
#     
#   print "cs coverage_modules: " + str(coverage_modules)
    
#   for module_string in settings.COVERAGE_MODULES:
#     module = __import__(module_string, globals(), locals(), [""])
#     f,s,m,mf = coverage.analysis(module)    
#     fp = file(os.path.join(settings.COVERAGE_DIR, module_string + ".html"), "wb")
#     coverage_color.colorize_file(f, outstream=fp, not_covered=mf)
#     fp.close()
#   coverage.report(coverage_modules, show_missing=1)

#   coverage.erase();

  # Report code coverage metrics
#   if hasattr(settings, 'COVERAGE_MODULES') and not test_labels:
#     coverage_modules = []
#     for module in settings.COVERAGE_MODULES:
#       coverage_modules.append(__import__(module, globals(), locals(), ['']))
# 
#     coverage.report(coverage_modules, show_missing=1)
    
    # Print code metrics footer
#     print '----------------------------------------------------------------------'

  return test_results
コード例 #33
0
ファイル: testall.py プロジェクト: timdiels/0install
#!/usr/bin/env python

from __future__ import print_function

import unittest, os, sys

# Catch silly mistakes...
os.environ["HOME"] = "/home/idontexist"

try:
    import coverage

    coverage.use_cache(False)
    coverage.erase()
    coverage.start()
except ImportError:
    coverage = None

my_dir = os.path.dirname(sys.argv[0])
if not my_dir:
    my_dir = os.getcwd()

sys.argv.append("-v")

suite_names = [f[:-3] for f in os.listdir(my_dir) if f.startswith("test") and f.endswith(".py")]
suite_names.remove("testall")
suite_names.sort()

alltests = unittest.TestSuite()

for name in suite_names:
コード例 #34
0
ファイル: test_api.py プロジェクト: clytwynec/coveragepy
 def setUp(self):
     super(SingletonApiTest, self).setUp()
     # These tests use the singleton module interface.  Prevent it from
     # writing .coverage files at exit.
     coverage.use_cache(0)
コード例 #35
0
ファイル: testall.py プロジェクト: timdiels/0install
#!/usr/bin/env python

from __future__ import print_function

import unittest, os, sys

# Catch silly mistakes...
os.environ['HOME'] = '/home/idontexist'

try:
    import coverage
    coverage.use_cache(False)
    coverage.erase()
    coverage.start()
except ImportError:
    coverage = None

my_dir = os.path.dirname(sys.argv[0])
if not my_dir:
    my_dir = os.getcwd()

sys.argv.append('-v')

suite_names = [
    f[:-3] for f in os.listdir(my_dir)
    if f.startswith('test') and f.endswith('.py')
]
suite_names.remove('testall')
suite_names.sort()

alltests = unittest.TestSuite()
コード例 #36
0
    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        coverage.use_cache(settings.COVERAGE_USE_CACHE)
        for e in settings.COVERAGE_CODE_EXCLUDES:
            coverage.exclude(e)
        coverage.start()
        results = super(CoverageRunner, self).run_tests(test_labels,
                                                        extra_tests, **kwargs)
        coverage.stop()

        coverage_modules = []
        if test_labels:
            for label in test_labels:
                label = label.split('.')[0]
                app = get_app(label)
                coverage_modules.append(self._get_app_package(app))
        else:
            for app in get_apps():
                coverage_modules.append(self._get_app_package(app))

        coverage_modules.extend(settings.COVERAGE_ADDITIONAL_MODULES)

        packages, modules, excludes, errors = get_all_modules(
            coverage_modules, settings.COVERAGE_MODULE_EXCLUDES,
            settings.COVERAGE_PATH_EXCLUDES)

        if settings.COVERAGE_USE_STDOUT:
            coverage.report(list(modules.values()), show_missing=1)
            if excludes:
                message = "The following packages or modules were excluded:"
                print("")
                print(message)
                for e in excludes:
                    print(e)
                print("")
            if errors:
                message = "There were problems with the following packages "
                message += "or modules:"
                print("")
                print(message)
                for e in errors:
                    print(e)
                print("")

        outdir = settings.COVERAGE_REPORT_HTML_OUTPUT_DIR
        if outdir:
            outdir = os.path.abspath(outdir)
            if settings.COVERAGE_CUSTOM_REPORTS:
                html_report(outdir, modules, excludes, errors)
            else:
                coverage._the_coverage.html_report(list(modules.values()), outdir)
            print("")
            print("HTML reports were output to '%s'" %outdir)

        datafile = settings.COVERAGE_REPORT_DATA_FILE
        if datafile:
            covered_files = coverage._the_coverage.data.lines.keys()
            covered_arcs = coverage._the_coverage.data.arcs.keys()
            modfiles = []
            for module in modules.values():
                modfiles.append(module.__file__.replace('.pyc', '.py'))
            for filename in covered_files:
                if not filename in modfiles and filename in coverage._the_coverage.data.lines:
                    del coverage._the_coverage.data.lines[filename]
            for filename in covered_arcs:
                if not filename in modfiles and filename in coverage._the_coverage.data.arcs:
                    del coverage._the_coverage.data.arcs[filename]
            coverage._the_coverage.data.usefile(True)
            coverage._the_coverage.data.write_file(datafile)

        return results
コード例 #37
0
ファイル: nr.py プロジェクト: creotiv/django-nosetests
        # f****n Django models forget to make commit!
        transaction.commit_unless_managed() 
        try:
            cursor.execute(data)
            transaction.commit_unless_managed()
        except Exception,err:
            print 'DB Schema load error: ',err
        else:
            print 'DB Schema loaded.'
    else:
        print 'No Schema found.'
    
    print 'Start running tests.'
    #####

    coverage.use_cache(0)
    for e in default_settings.COVERAGE_CODE_EXCLUDES:
        coverage.exclude(e)
    coverage.start()

    #####

    nose.run(argv=nose_argv)

    #####

    coverage.stop()
    #####
    
    print 'Testing ended.'
コード例 #38
0
ファイル: testsuite.py プロジェクト: FND/templates
def initCoverage():
	coverage.erase()
	coverage.use_cache(0)
	coverage.start()
コード例 #39
0
ファイル: run.py プロジェクト: saif1413/L4OS
def start_coverage():
    """Setup coverage"""
    coverage.use_cache(False)
    coverage.erase()
    coverage.start()
コード例 #40
0
ファイル: test_api.py プロジェクト: phenoxim/coveragepy
 def setUp(self):
     super(SingletonApiTest, self).setUp()
     # These tests use the singleton module interface.  Prevent it from
     # writing .coverage files at exit.
     coverage.use_cache(0)
コード例 #41
0
def run_tests(verbosity):
    "Run test suite"

    # list all the files in the top level directory
    file_list = os.listdir(
        os.path.join(
            os.path.abspath(os.path.dirname(os.path.realpath(__file__)))))

    # list all the files in the tests directory
    test_list = os.listdir(
        os.path.join(
            os.path.abspath(os.path.dirname(os.path.realpath(__file__))),
            'tests'))

    code_modules = []
    # loop over all the file names
    for file_name in file_list:
        extension = os.path.splitext(file_name)[-1]
        # if they are python files or the test runner
        if extension == '.py' and file_name != 'test.py':
            # work out the module name
            code_module_name = os.path.splitext(file_name)[0:-1][0]
            # now import the module
            module = __import__(code_module_name, globals(), locals(),
                                code_module_name)
            # and add it to the list of available modules
            code_modules.append(module)

    test_modules = []
    # loop over all the file names
    for file_name in test_list:
        extension = os.path.splitext(file_name)[-1]
        # if they are python files
        if extension == '.py':
            # work out the module name
            test_module_name = "tests." + os.path.splitext(file_name)[0:-1][0]
            # now import the module
            module = __import__(test_module_name, globals(), locals(),
                                test_module_name)
            # and add it to the list of available modules
            test_modules.append(module)

    # populate a test suite from the individual tests from the list of modules
    suite = unittest.TestSuite(
        map(unittest.defaultTestLoader.loadTestsFromModule, test_modules))

    # set up the test runner
    runner = unittest.TextTestRunner(verbosity=int(verbosity))

    # set up coverage reporting
    coverage.use_cache(0)
    coverage.start()

    # run the tests
    runner.run(suite)

    # stop coverage reporting
    coverage.stop()

    # output coverage report
    coverage.report(code_modules, show_missing=1)