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)
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)
def report(self, stream): log.debug("Coverage report") import coverage coverage.stop() modules = [ module for name, module in sys.modules.items() if self.wantModuleCoverage(name, module) ] log.debug("Coverage report will cover modules: %s", modules) if self.coverDir and self.coverAnnotate: coverage.annotate(modules, self.coverDir) fd = open("%s/cover.report" % self.coverDir, "w") coverage.report(modules, file=fd) fd.close()
def report(self, stream): """ Output code coverage report. """ log.debug("Coverage report") import coverage coverage.stop() modules = [ module for name, module in sys.modules.items() if self.wantModuleCoverage(name, module) ] log.debug("Coverage report will cover modules: %s", modules) coverage.report(modules, file=stream) if self.coverHtmlDir: if not os.path.exists(self.coverHtmlDir): os.makedirs(self.coverHtmlDir) log.debug("Generating HTML coverage report") files = {} for m in modules: if hasattr(m, '__name__') and hasattr(m, '__file__'): files[m.__name__] = m.__file__ coverage.annotate(files.values()) global_stats = {'covered': 0, 'missed': 0, 'skipped': 0} file_list = [] for m, f in files.iteritems(): if f.endswith('pyc'): f = f[:-1] coverfile = f + ',cover' outfile, stats = self.htmlAnnotate(m, f, coverfile, self.coverHtmlDir) for field in ('covered', 'missed', 'skipped'): global_stats[field] += stats[field] file_list.append((stats['percent'], m, outfile, stats)) os.unlink(coverfile) file_list.sort() global_stats['percent'] = self.computePercent( global_stats['covered'], global_stats['missed']) # Now write out an index file for the coverage HTML index = open(os.path.join(self.coverHtmlDir, 'index.html'), 'w') index.write('<html><head><title>Coverage Index</title></head>' '<body><p>') index.write(COVERAGE_STATS_TEMPLATE % global_stats) index.write('<table><tr><td>File</td><td>Covered</td><td>Missed' '</td><td>Skipped</td><td>Percent</td></tr>') for junk, name, outfile, stats in file_list: stats['a'] = '<a href="%s">%s</a>' % (outfile, name) index.write('<tr><td>%(a)s</td><td>%(covered)s</td><td>' '%(missed)s</td><td>%(skipped)s</td><td>' '%(percent)s %%</td></tr>' % stats) index.write('</table></p></html') index.close()
def report(self, stream): """ Output code coverage report. """ log.debug("Coverage report") import coverage coverage.stop() modules = [ module for name, module in sys.modules.items() if self.wantModuleCoverage(name, module, stream) ] log.debug("Coverage report will cover modules: %s", modules) coverage.report(modules, file=stream) if self.coverHtmlDir: if not os.path.exists(self.coverHtmlDir): os.makedirs(self.coverHtmlDir) log.debug("Generating HTML coverage report") files = {} for m in modules: if hasattr(m, '__name__') and hasattr(m, '__file__'): files[m.__name__] = m.__file__ coverage.annotate(files.values()) global_stats = {'covered': 0, 'missed': 0, 'skipped': 0} file_list = [] for m, f in files.iteritems(): if f.endswith('pyc'): f = f[:-1] coverfile = f+',cover' outfile, stats = self.htmlAnnotate(m, f, coverfile, self.coverHtmlDir) for field in ('covered', 'missed', 'skipped'): global_stats[field] += stats[field] file_list.append((stats['percent'], m, outfile, stats)) os.unlink(coverfile) file_list.sort() global_stats['percent'] = self.computePercent( global_stats['covered'], global_stats['missed']) # Now write out an index file for the coverage HTML index = open(os.path.join(self.coverHtmlDir, 'index.html'), 'w') index.write('<html><head><title>Coverage Index</title></head>' '<body><p>') index.write(COVERAGE_STATS_TEMPLATE % global_stats) index.write('<table><tr><td>File</td><td>Covered</td><td>Missed' '</td><td>Skipped</td><td>Percent</td></tr>') for junk, name, outfile, stats in file_list: stats['a'] = '<a href="%s">%s</a>' % (outfile, name) index.write('<tr><td>%(a)s</td><td>%(covered)s</td><td>' '%(missed)s</td><td>%(skipped)s</td><td>' '%(percent)s %%</td></tr>' % stats) index.write('</table></p></html') index.close()
def mark(code, get): content = code.split("\n") includes = coverage.system.system_includes("g++") include_args = ["-I{0}".format(path) for path in includes] for r in reversed(coverage.annotate(code, include_args)): marker = get(r) line = content[marker.end.line] line = line[:marker.end.column] + "]" + line[marker.end.column:] content[marker.end.line] = line line = content[marker.start.line] line = line[:marker.start.column] + "[" + line[marker.start.column:] content[marker.start.line] = line return "\n".join(content)
coverage.stop() # Remove the 'test' part of the unit test file names print "module_names", module_names removes = ['test_cadell_damage'] for ditch in removes: module_names.remove(ditch) module_names = [x[5:] for x in module_names] # This is a hack, since all the files have to be in eqrm_code code_dir = os.getcwd() sys.path.append(code_dir) modules = map(__import__, module_names) sys.path.remove(code_dir) coverage.report(modules) unit_dir = tempfile.mkdtemp() coverage.annotate(modules, directory=unit_dir) os.chdir('..') print "Getting differences in coverage? ", print "The run with less coverage might use compiled code. Do a clean_all.py." print "UNIT TEST COVERAGE FINISHED. DOING IMPLEMENTATION TESTS" #import sys; sys.exit() # Run the implementation tests coverage.erase() coverage.start() # This import causes problems. Import this and it imports analysis from eqrm_code.check_scenarios import run_scenarios