def stop(stream, packages, target, collect=False): """Stop coverage""" # Call stop coverage.stop() # Collect if asked if collect: coverage.the_coverage.collect() # Get the file descriptor for the report fd = StringIO.StringIO() # Get the report coverage.report(packages, file=fd) # Flush fd.flush() # Write on stream stream.write(fd.getvalue()) # Write in target ntools.save(fd.getvalue(), os.path.join(target, "coverage.dat"), binary=False) # Get the sources sources = parse(fd.getvalue()) # Close the file descriptor fd.close() # Annotate source files annotate(packages, target, ignore_errors=True) # Create report report(target, sources)
def doctest(self): import doctest, coverage # (f,t) = doctest.testmod(eval('isconf.Kernel')) # doctest.master.summarize() # sys.exit(f) modules = [] olddir = os.getcwd() os.chdir('lib/python') os.path.walk('.',getmods,modules) os.chdir(olddir) print modules # modules=[rpc822] fail=0 total=0 coverage.erase() coverage.start() for mod in modules: (f,t) = doctest.testmod(mod,report=0) fail += f total += t doctest.master.summarize() coverage.stop() for mod in modules: coverage.analysis(mod) coverage.report(modules) sys.exit(fail)
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 main(): from optparse import OptionParser usage = ('Usage: %prog [option] [modules to be tested]\n' 'Modules names have to be given in the form utils.mail (without ' 'zine.)\nIf no module names are given, all tests are run') parser = OptionParser(usage=usage) parser.add_option('-c', '--coverage', action='store_true', dest='coverage', help='show coverage information (slow!)') parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False, help='show which tests are run') options, args = parser.parse_args(sys.argv[1:]) modnames = ['zine.' + modname for modname in args] if options.coverage: if coverage is not None: use_coverage = True else: sys.stderr.write("coverage information requires Ned Batchelder's " "coverage.py to be installed!\n") sys.exit(1) else: use_coverage = False if use_coverage: coverage.erase() coverage.start() s, covermods = suite(modnames, True) else: s = suite(modnames) TextTestRunner(verbosity=options.verbose + 1).run(s) if use_coverage: coverage.stop() print '\n\n' + '=' * 25 + ' coverage information ' + '=' * 25 coverage.report(covermods)
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
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
def finalize_environment(self): root = soup.get_root() + "/conduit" modules = [] for i in range(3): modules.extend(glob(root + "/*" * i + ".py")) coverage.report(modules, ignore_errors=1, show_missing=0) coverage.erase()
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
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
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 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
def report_coverage(packages): test_re = re.compile('.+test[^%s.]*\..+' % os.sep) coverage.stop() files = set() for name in sys.modules: mod = sys.modules.get(name) if mod is None: continue elif not is_in_packages(name, packages): continue elif is_in_packages(name, ['django']): continue filename = mod.__file__.replace('.pyc', '.py') if test_re.match(filename): continue st = os.stat(filename) if st.st_size > 1: files.add(filename) if files: coverage.report(list(files)) coverage.erase()
def main_coverage(TESTS): modulenames = MODULE_NAMES coverage.erase() coverage.start() coverage.exclude('#pragma[: ]+[nN][oO] [cC][oO][vV][eE][rR]') modules = [] for modulename in modulenames: print modulenames mod = my_import(modulename) modules.append(mod) if 'unittest' in TESTS: print "***** Unittest *****" test_args = {'verbosity': 1} suite = unittest.TestLoader().loadTestsFromNames(TEST_NAMES) unittest.TextTestRunner(**test_args).run(suite) if 'doctest' in TESTS: t0 = time.time() print "\n***** Doctest *****" for mod in modules: doctest.testmod(mod, verbose=VERBOSE) td = time.time() - t0 print " Tests took %.3f seconds" % (td, ) print "\n***** Coverage Python *****" coverage.stop() coverage.report(modules, ignore_errors=1, show_missing=1) coverage.erase()
def run_tests(self, test_labels, verbosity=1, interactive=True, extra_tests=[]): coveragemodules = getattr(settings, 'COVERAGE_MODULES', []) if coveragemodules: coverage.start() self.setup_test_environment() suite = self.build_suite(test_labels, extra_tests) old_config = self.setup_databases() result = self.run_suite(suite) if coveragemodules: coverage.stop() coveragedir = getattr(settings, 'COVERAGE_DIR', './build/coverage') if not os.path.exists(coveragedir): os.makedirs(coveragedir) modules = [] for module_string in coveragemodules: module = __import__(module_string, globals(), locals(), [""]) modules.append(module) f,s,m,mf = coverage.analysis(module) fp = file(os.path.join(coveragedir, module_string + ".html"), "wb") colorize.colorize_file(f, outstream=fp, not_covered=mf) fp.close() coverage.report(modules, show_missing=0) coverage.erase() self.teardown_databases(old_config) self.teardown_test_environment() return len(result.failures) + len(result.errors)
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
def report_coverage(packages): test_re = re.compile('.+test[^%s.]*\..+' % os.sep) coverage.stop() files = set() for name in sys.modules: mod = sys.modules.get(name) if mod == None: continue elif not is_in_packages(name, packages): continue elif is_in_packages(name, ['django']): continue filename = mod.__file__.replace('.pyc', '.py') if test_re.match(filename): continue st = os.stat(filename) if st.st_size > 1: files.add(filename) if files: coverage.report(list(files)) coverage.erase()
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
def __init__(self, module_list): self.modules = {} self.coverage_cache = {} buffer = StringIO.StringIO("") print("building coverage report, this can be slow... (try setting --include or --exclude)") coverage.report(module_list, file=buffer) buffer.seek(0) print("done") for line in buffer.readlines(): #print line.rstrip() tokens = line.split() if len(tokens) > 3 and tokens[0] != 'Name': try: cm = CoveredModule(tokens[0], int(tokens[1]), int(tokens[2])) self.modules[cm.name] = cm if cm.name.endswith('__init__'): name = cm.name[:-9] self.modules[name] = CoveredModule(name, 0, None) except: pass #print "Error parsing", line self.root_module().size = 0
def test_report(self): self.do_report_work("mycode2") coverage.report(["mycode2.py"]) self.assertEqual(self.stdout(), textwrap.dedent("""\ Name Stmts Miss Cover Missing ------------------------------------------ mycode2.py 7 3 57% 4-6 """))
def reportCoverage(modules): """ report test coverage for given modules @param modules (list): module names """ modules = [__import__(module) for module in modules] coverage.report(modules, ignore_errors=0, show_missing=1)
def test_report(self): self.do_report_work("mycode2") coverage.report(["mycode2.py"]) self.assertEqual(self.stdout(), textwrap.dedent("""\ Name Stmts Miss Cover Missing --------------------------------------- mycode2 7 3 57% 4-6 """))
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)
def end_coverage(): coverage.the_coverage.start() coverage.the_coverage.collect() coverage.the_coverage.stop() modules = [p[:-3] for p in os.listdir('../polysh') if p.endswith('.py')] coverage.report(['../polysh/%s.py' % (m) for m in modules]) remove_coverage_files() # Prevent the atexit.register(the_coverage.save) from recreating the files coverage.the_coverage.usecache = coverage.the_coverage.cache = None
def report_coverage(): coverage.stop() module_list = [ mod for name, mod in sys.modules.copy().iteritems() if getattr(mod, '__file__', None) and name.startswith('jinja2.') and name not in IGNORED_MODULES ] module_list.sort() coverage.report(module_list)
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) coverage.report(modules, file=stream)
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
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): """ Run the unit tests for all the test labels in the provided list. Labels must be of the form: - app.TestClass.test_method Run a single specific test method - app.TestClass Run all the test methods in a given class - app Search for doctests and unittests in the named application. When looking for tests, the test runner will look in the models and tests modules for the application. A list of 'extra' tests may also be provided; these tests will be added to the test suite. If the settings file has an entry for COVERAGE_MODULES or test_labels is true it will prints the coverage report for modules/apps Returns number of tests that failed. """ do_coverage = hasattr(settings, 'COVERAGE_MODULES') or bool(test_labels) if do_coverage: coverage.erase() coverage.start() from django.test import simple retval = simple.run_tests(test_labels, verbosity, interactive, extra_tests) if do_coverage: coverage.stop() # Print code metrics header print '' print '----------------------------------------------------------------------' print ' Unit Test Code Coverage Results' print '----------------------------------------------------------------------' # try to import all modules for the coverage report. modules = [] if getattr(settings, 'COVERAGE_MODULES', None): modules = [__import__(module, {}, {}, ['']) for module in settings.COVERAGE_MODULES] elif test_labels: modules = [] for label in test_labels: label = label.split('.')[0] #remove test class or test method from label pkg = _get_app_package(label) modules.extend(_package_modules(*pkg)) coverage.report(modules, show_missing=1) return retval
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 checkCoverage(self, text, lines, missing="", excludes=[], report=""): # We write the code into a file so that we can import it. # coverage.py wants to deal with things as modules with file names. # We append self.n because otherwise two calls in one test will use the # same filename and whether the test works or not depends on the # timestamps in the .pyc file, so it becomes random whether the second # call will use the compiled version of the first call's code or not! modname = 'coverage_test_' + self.noise + str(self.n) self.n += 1 self.makeFile(modname, text) # Start up coverage.py self.t.clear() # coverage.erase() # for exc in excludes: # coverage.exclude(exc) # coverage.start() self.t.start() # Import the python file, executing it. mod = self.importModule(modname) # Stop coverage.py self.t.stop() # coverage.stop() # Clean up our side effects del sys.modules[modname] # Get the analysis results, and check that they are right. # _, clines, _, cmissing = coverage.analysis(mod) files = self.t.gather_files() clines = list(files.get(mod.__file__)) clines.sort() import sets clines = sets.Set(clines) lines = sets.Set(lines) subset = clines.issubset(lines) assert subset, "%s, %s\n" % (clines, lines) # self.assertEqual(clines, lines) # self.assertEqual(cmissing, missing) if report: self.assertEqual(1, 0) frep = StringIO() coverage.report(mod, file=frep) rep = " ".join(frep.getvalue().split("\n")[2].split()[1:]) self.assertEqual(report, rep)
def test_report_file(self): # The file= argument of coverage.report makes the report go there. self.do_report_work("mycode3") fout = StringIO() coverage.report(["mycode3.py"], file=fout) self.assertEqual(self.stdout(), "") self.assertEqual(fout.getvalue(), textwrap.dedent("""\ Name Stmts Miss Cover Missing ------------------------------------------ mycode3.py 7 3 57% 4-6 """))
def test_report_file(self): # The file= argument of coverage.report makes the report go there. self.do_report_work("mycode3") fout = StringIO() coverage.report(["mycode3.py"], file=fout) self.assertEqual(self.stdout(), "") self.assertEqual(fout.getvalue(), textwrap.dedent("""\ Name Stmts Miss Cover Missing --------------------------------------- mycode3 7 3 57% 4-6 """))
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 runTests(testModules=None, profileOut=None, coverageOutDir=None): if coverageOutDir is not None: if not os.path.exists(coverageOutDir): os.makedirs(coverageOutDir) coverage.erase() coverage.start() alltests = unittest.TestLoader().loadTestsFromNames(testModules) if profileOut is not None: results = Profiler().profile(alltests) results.sort() results.reverse() out = open(profileOut, 'w') for result in results: out.write("%s \t%3.6f\n" % (result[1], result[0])) print "unittest profiling information written in " + profileOut out.close() else: # if we don't add this else the tests are run twice unittest.TextTestRunner().run(alltests) if coverageOutDir is not None: coverage.stop() modules = glob('../*.py') #modules.remove('../__init__.py') for module in modules: f, s, m, mf = coverage.analysis(module) out = file(join(coverageOutDir, os.path.basename(f) + '.html'), 'wb') colorize.colorize_file(f, outstream=out, not_covered=mf) out.close() print coverageReportTxt = file(join(coverageOutDir, "coverage.txt"), 'w') coverage.report(modules, show_missing=False, omit_prefixes=['__'], file=coverageReportTxt) coverageReportTxt.close() coverage.report(modules, show_missing=False, omit_prefixes=['__']) coverage.erase() coverageReportTxt.close() print print "Coverage information updated in " + coverageOutDir print import webbrowser webbrowser.open('file://' + join(getcwd(), coverageOutDir))
def runTests(self): testrunner = TextTestRunnerWithTimings( verbosity=self._options.verbosity, timeTests=self._options.time, nrTestsToReport=self._options.time_reports) if self._options.coverage: coverage.erase() coverage.start() result = testrunner.run(self) if self._options.coverage: coverage.stop() print coverage.report(self.getPyFilesFromDir(os.path.join(projectRoot, 'taskcoachlib'))) return result
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
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
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 doit(): srcdir = os.environ['RTTSrcDir'] pyfiles = glob.glob(os.path.join(srcdir, '*.py')) coverage.erase() coverage.start() run.TestFramework() coverage.stop() coverage.analysis(run) coverage.report(pyfiles) coverage.erase()
def check(self): # Find coverage instance. coverage = self.state.get('__coverage__') if coverage is None: self.ctl.failed("coverage has not been started") return # Stop coverage. if coverage._started: coverage.stop() if getattr(coverage, 'auto_data', None) or \ getattr(coverage, '_auto_data', None) or \ getattr(coverage, '_auto_load', None) or \ getattr(coverage, '_auto_save', None): coverage.save() coverage.combine() # Generate the report. report_stream = StringIO.StringIO() check = coverage.report(file=report_stream) report = report_stream.getvalue() # Display the report and complain if coverage is insufficient. self.ui.literal(report) if check < self.input.coverage_check: self.ctl.failed("insufficient coverage: %s (expected: %s)" % (check, self.input.coverage_check))
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
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
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
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
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))
def run_tests(self, test_labels, verbosity=1, interactive=True, extra_tests=[]): coveragemodules = getattr(settings, 'COVERAGE_MODULES', []) if coveragemodules: coverage.start() self.setup_test_environment() suite = self.build_suite(test_labels, extra_tests) old_config = self.setup_databases() result = self.run_suite(suite) if coveragemodules: coverage.stop() coveragedir = getattr(settings, 'COVERAGE_DIR', './build/coverage') if not os.path.exists(coveragedir): os.makedirs(coveragedir) modules = [] for module_string in coveragemodules: module = __import__(module_string, globals(), locals(), [""]) modules.append(module) f, s, m, mf = coverage.analysis(module) fp = file(os.path.join(coveragedir, module_string + ".html"), "wb") colorize.colorize_file(f, outstream=fp, not_covered=mf) fp.close() coverage.report(modules, show_missing=0) coverage.erase() self.teardown_databases(old_config) self.teardown_test_environment() return len(result.failures) + len(result.errors)
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
def stop(stream, packages, target, collect=False): '''Stop coverage''' # Call stop coverage.stop() # Collect if asked if collect: coverage.the_coverage.collect() # Get the file descriptor for the report fd = StringIO.StringIO() # Get the report coverage.report(packages, file=fd) # Flush fd.flush() # Write on stream stream.write(fd.getvalue()) # Write in target ntools.save(fd.getvalue(), os.path.join(target, 'coverage.dat'), binary=False) # Get the sources sources = parse(fd.getvalue()) # Close the file descriptor fd.close() # Annotate source files annotate(packages, target, ignore_errors=True) # Create report report(target, sources)
if __name__ == "__main__": if os.environ.get('USECOVERAGE') == '1': try: import coverage coverage.erase() coverage.start() COVERAGE = 1 import _OGMultiPoint from _OGMultiPoint import * except: print "Error setting up coverage checking" COVERAGE = 0 else: COVERAGE = 0 if os.environ.get('USEPYCHECK') == '1': try: import pychecker.checker except: print "Pychecker not installed on this machine" unittest.TextTestRunner().run(testSuite()) if COVERAGE: coverage.stop() x = coverage.analysis(_OGMultiPoint) print "\n" coverage.report(_OGMultiPoint)
def run_tests(test_labels, verbosity=1, interactive=True, failfast=False, extra_tests=[]): """ Run the unit tests for all the test labels in the provided list. Labels must be of the form: - app.TestClass.test_method Run a single specific test method - app.TestClass Run all the test methods in a given class - app Search for doctests and unittests in the named application. When looking for tests, the test runner will look in the models and tests modules for the application. A list of 'extra' tests may also be provided; these tests will be added to the test suite. If the settings file has an entry for COVERAGE_MODULES or test_labels is true it will prints the coverage report for modules/apps Returns number of tests that failed. """ do_coverage = (hasattr(settings, 'COVERAGE_MODULES') or hasattr(settings, 'COVERAGE_APPS') or bool(test_labels)) if do_coverage: coverage.erase() coverage.start() DjangoTestSuiteRunner = None try: from django.test.simple import DjangoTestSuiteRunner except ImportError: from django.test import simple if DjangoTestSuiteRunner: testrunner = DjangoTestSuiteRunner(verbosity=verbosity, interactive=interactive, failfast=failfast) retval = testrunner.run_tests(test_labels, extra_tests) else: retval = simple.run_tests(test_labels, verbosity, interactive, extra_tests) if do_coverage: coverage.stop() # Print code metrics header print '' print '----------------------------------------------------------------------' print ' Unit Test Code Coverage Results' print '----------------------------------------------------------------------' # try to import all modules for the coverage report. modules = [] if test_labels or hasattr(settings, 'COVERAGE_APPS'): # apps entered at the command line prompt override those specified in settings labels = test_labels or settings.COVERAGE_APPS for label in labels: label = label.split('.')[0] #remove test class or test method from label pkg = _get_app_package(label) modules.extend(_package_modules(*pkg)) elif hasattr(settings, 'COVERAGE_MODULES'): modules = [__import__(module, {}, {}, ['']) for module in settings.COVERAGE_MODULES] if hasattr(settings, 'COVERAGE_EXCLUDE_MODULES'): for exclude_module_name in settings.COVERAGE_EXCLUDE_MODULES: # Test designed to avoid accidentally removing a module whose # name is prefixed by an excluded module name, but still remove # submodules modules = [module for module in modules if not module.__name__ == exclude_module_name and not module.__name__.startswith(exclude_module_name + '.')] coverage.report(modules, show_missing=1) return retval
def run(self): print('Starting Gaphor...') if self.model: print('Starting with model file', self.model) for cmd_name in self.get_sub_commands(): self.run_command(cmd_name) #if self.build_lib not in sys.path: #sys.path.insert(0, self.build_lib) #os.environ['GAPHOR_DATADIR'] = os.path.abspath('data') if self.coverage: import coverage coverage.start() if self.command: print('Executing command: %s...' % self.command) exec(self.command) elif self.doctest: print('Running doctest cases in module: %s...' % self.doctest) import imp # use zope's one since it handles coverage right from zope.testing import doctest # Figure out the file: f = os.path.join(*self.doctest.split('.')) + '.py' fp = open(f) # Prepend module's package path to sys.path pkg = os.path.join(self.build_lib, *self.doctest.split('.')[:-1]) #if pkg: # sys.path.insert(0, pkg) # print 'Added', pkg, 'to sys.path' # Load the module as local module (without package) test_module = imp.load_source(self.doctest.split('.')[-1], f, fp) failure, tests = doctest.testmod(test_module, name=self.doctest, optionflags=doctest.ELLIPSIS + doctest.NORMALIZE_WHITESPACE) if self.coverage: print() print('Coverage report:') coverage.report(f) sys.exit(failure != 0) elif self.unittest: # Running a unit test is done by opening the unit test file # as a module and running the tests within that module. print('Running test cases in unittest file: %s...' % self.unittest) import imp, unittest fp = open(self.unittest) test_module = imp.load_source('gaphor_test', self.unittest, fp) test_suite = unittest.TestLoader().loadTestsFromModule(test_module) #test_suite = unittest.TestLoader().loadTestsFromName(self.unittest) test_runner = unittest.TextTestRunner(verbosity=self.verbosity) result = test_runner.run(test_suite) if self.coverage: print() print('Coverage report:') coverage.report(self.unittest) sys.exit(not result.wasSuccessful()) elif self.file: print('Executing file: %s...' % self.file) dir, f = os.path.split(self.file) print('Extending PYTHONPATH with %s' % dir) #sys.path.append(dir) execfile(self.file, {}) else: print('Launching Gaphor...') del sys.argv[1:] starter = load_entry_point( 'gaphor==%s' % (self.distribution.get_version(), ), 'console_scripts', 'gaphor') if self.profile: print('Enabling profiling...') try: import cProfile import pstats prof = cProfile.Profile() prof.runcall(starter) prof.dump_stats('gaphor.prof') p = pstats.Stats('gaphor.prof') p.strip_dirs().sort_stats('time').print_stats(20) except ImportError as ex: import hotshot, hotshot.stats prof = hotshot.Profile('gaphor.prof') prof.runcall(starter) prof.close() stats = hotshot.stats.load('gaphor.prof') stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(20) else: starter()
import atom.mock_http_core import atom.auth import atom.client import gdata.gauth import gdata.client import gdata.data import gdata.blogger.data import gdata.blogger.client from gdata.test_config import settings # Ensure that coverage tests execute the live requests to the servers, but # allow use of cached server responses to speed up repeated runs. settings.RUN_LIVE_TESTS = True settings.CLEAR_CACHE = False def suite(): return unittest.TestSuite((atom_tests.core_test.suite(), )) if __name__ == '__main__': coverage.erase() coverage.start() unittest.TextTestRunner().run(all_tests.suite()) coverage.stop() coverage.report([ atom.core, atom.http_core, atom.auth, atom.data, atom.mock_http_core, atom.client, gdata.gauth, gdata.client, gdata.data, gdata.blogger.data, gdata.blogger.client ])
os.chdir('eqrm_code') module_names = eqrm_code.test_all.main() 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