Esempio n. 1
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
    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
    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        cov = coverage(config_file=settings.COVERAGE_CONFIG_FILE)
        cov.use_cache(settings.COVERAGE_USE_CACHE)
        for e in settings.COVERAGE_CODE_EXCLUDES:
            cov.exclude(e)
        cov.start()
        results = super(CoverageRunner, self).run_tests(test_labels, extra_tests, **kwargs)
        cov.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)

        outfile = StringIO()
        coverage_value = cov.report(modules.values(), show_missing=1, file=outfile)
        if settings.COVERAGE_USE_STDOUT:
            print >>sys.stdout, outfile.getvalue()
            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 and settings.COVERAGE_OUTPUT_HTML_REPORT:
            outdir = os.path.abspath(outdir)
            if settings.COVERAGE_CUSTOM_REPORTS:
                html_report(outdir, modules, excludes, errors)
            else:
                cov.html_report(modules.values(), outdir)
            print >>sys.stdout
            print >>sys.stdout, "HTML reports were output to '%s'" %outdir

        coverage_fails = coverage_value < settings.COVERAGE_FAIL_UNDER
        if coverage_fails:
            raise Exception(
                "Test coverage failed: %0.2f%% is less than %0.2f%% !" % (
                    coverage_value, settings.COVERAGE_FAIL_UNDER
                )
            )

        # from https://github.com/django/django/blob/master/django/core/management/commands/test.py#L90-L91
        return results or coverage_fails
    def run_tests(self, test_labels, extra_tests=None, **kwargs):
        # 1. 设置coverage
        opts = {
            'auto_data': settings.COVERAGE_USE_CACHE,
        }
        if settings.COVERAGE_SOURCE:
            opts['source'] = settings.COVERAGE_SOURCE
        if settings.COVERAGE_CONFIG_FILE:
            opts['config_file'] = settings.COVERAGE_CONFIG_FILE
        coverage = Coverage(**opts)
        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()

        # 2. 添加coverage_modules
        coverage_modules = []
        if test_labels:
            for label in test_labels:
                label = label.split('.')[0].rstrip('/')
                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等
        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("")
                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("")

        # 输出Html格式的数据
        # modules: module_name --> module
        #
        outdir = settings.COVERAGE_REPORT_HTML_OUTPUT_DIR
        if outdir:
            outdir = os.path.abspath(outdir)
            # 默认使用55minutes的html表
            if settings.COVERAGE_CUSTOM_REPORTS:
                html_report(coverage, outdir, modules, excludes, errors)
            else:
                coverage.html_report(modules.values(), outdir)
            print("")
            print("HTML reports were output to '%s'" % outdir)

        return results