diff options
author | David Stanek <dstanek@dstanek.com> | 2010-05-21 23:40:04 -0400 |
---|---|---|
committer | David Stanek <dstanek@dstanek.com> | 2010-05-21 23:40:04 -0400 |
commit | 4bb3390f6675b4197cafd0e38e3e7f82d22c8516 (patch) | |
tree | 6f094ad09744002ae822c26ff7210f56a234a148 /coverage/testplugin.py | |
parent | b06c5c724b507a0059caf645c42991f83b86aec2 (diff) | |
download | python-coveragepy-git-4bb3390f6675b4197cafd0e38e3e7f82d22c8516.tar.gz |
cleaned up the testplugin module
Diffstat (limited to 'coverage/testplugin.py')
-rw-r--r-- | coverage/testplugin.py | 178 |
1 files changed, 75 insertions, 103 deletions
diff --git a/coverage/testplugin.py b/coverage/testplugin.py index c93b8599..75c5b331 100644 --- a/coverage/testplugin.py +++ b/coverage/testplugin.py @@ -1,42 +1,34 @@ -import os import re import sys import optparse -from types import ListType import coverage + class CoverageTestWrapper(object): - """ - A Coverage Test Wrapper. - + """A coverage test wrapper. + 1) Setup the with the parsed options 2) Call start() 3) Run your tests 4) Call finish() 5) Improve your code coverage ;) """ - - coverTests = False + coverPackages = None - def __init__(self, options, _covpkg=None): - self.options = options - + def __init__(self, options, _covpkg=coverage): # _covpkg is for dependency injection, so we can test this code. - if _covpkg: - self.covpkg = _covpkg - else: - import coverage - self.covpkg = coverage - + + self.options = options + self.covpkg = _covpkg + self.coverage = None self.coverTests = options.cover_tests self.coverPackages = options.cover_package - + def start(self): - # Set up coverage self.coverage = self.covpkg.coverage( data_suffix = bool(self.options.cover_parallel_mode), cover_pylib = self.options.cover_pylib, @@ -44,13 +36,11 @@ class CoverageTestWrapper(object): branch = self.options.cover_branch, ) - self.skipModules = sys.modules.keys()[:] - - # Run the script. + self.skipModules = sys.modules.keys()[:] #TODO: is this necessary?? + self.coverage.start() - + def finish(self, stream=None): - # end coverage and save the results self.coverage.stop() self.coverage.save() @@ -62,33 +52,31 @@ class CoverageTestWrapper(object): 'morfs': modules, 'ignore_errors': self.options.cover_ignore_errors, } - - # Handle any omits - # Allow pointing to a file as well - try: + + try: # try looking for an omit file omit_file = open(self.options.cover_omit) omit_prefixes = [line.strip() for line in omit_file.readlines()] report_args['omit_prefixes'] = omit_prefixes - except: + except: # assume cover_omit is a ',' separated list if provided omit = self.options.cover_omit.split(',') report_args['omit_prefixes'] = omit if 'report' in self.options.cover_actions: self.coverage.report( - show_missing=self.options.cover_show_missing, - file=stream, **report_args) + show_missing=self.options.cover_show_missing, + file=stream, **report_args) if 'annotate' in self.options.cover_actions: self.coverage.annotate( - directory=self.options.cover_directory, **report_args) + directory=self.options.cover_directory, **report_args) if 'html' in self.options.cover_actions: self.coverage.html_report( - directory=self.options.cover_directory, **report_args) + directory=self.options.cover_directory, **report_args) if 'xml' in self.options.cover_actions: outfile = self.options.cover_outfile if outfile == '-': outfile = None self.coverage.xml_report(outfile=outfile, **report_args) - + return def _want_module(self, name, module): @@ -100,77 +88,62 @@ class CoverageTestWrapper(object): options = [ - optparse.Option('', - '--cover-action', action='append', default=['report'], - dest='cover_actions', type="choice", choices=['annotate', 'html', 'report', 'xml'], - help="""\ + optparse.Option('--cover-action', action='append', default=['report'], + dest='cover_actions', type="choice", + choices=['annotate', 'html', 'report', 'xml'], + help="""\ annotate Annotate source files with execution information. html Create an HTML report. report Report coverage stats on modules. xml Create an XML report of coverage results. -""".strip()), - optparse.Option( - '--cover-package', action='append', default=[], - dest="cover_package", - metavar="COVER_PACKAGE", - help=("Restrict coverage output to selected package " - "- can be specified multiple times") - ), - optparse.Option("--cover-tests", action="store_true", - dest="cover_tests", - metavar="[NOSE_COVER_TESTS]", - default=False, - help="Include test modules in coverage report "), - optparse.Option( - '--cover-branch', action='store_true', - help="Measure branch execution. HIGHLY EXPERIMENTAL!" - ), - optparse.Option( - '--cover-directory', action='store', - metavar="DIR", - help="Write the output files to DIR." - ), - optparse.Option( - '--cover-ignore-errors', action='store_true', - help="Ignore errors while reading source files." - ), - optparse.Option( - '--cover-pylib', action='store_true', - help="Measure coverage even inside the Python installed library, " - "which isn't done by default." - ), - optparse.Option( - '--cover-show-missing', action='store_true', - help="Show line numbers of statements in each module that weren't " - "executed." - ), - optparse.Option( - '--cover-omit', action='store', - metavar="PRE1,PRE2,...", - default='', - help="Omit files when their filename path starts with one of these " - "prefixes." - ), - optparse.Option( - '--cover-outfile', action='store', - metavar="OUTFILE", - help="Write the XML report to this file. Defaults to 'coverage.xml'" - ), - optparse.Option( - '--cover-parallel-mode', action='store_true', - help="Include the machine name and process id in the .coverage " - "data file name." - ), - optparse.Option( - '--cover-timid', action='store_true', - help="Use a simpler but slower trace method. Try this if you get " - "seemingly impossible results!" - ), - optparse.Option( - '--cover-append', action='store_false', - help="Append coverage data to .coverage, otherwise it is started " - "clean with each run." - ) + """.strip()), + + optparse.Option('--cover-package', action='append', default=[], + dest="cover_package", metavar="COVER_PACKAGE", + help=("Restrict coverage output to selected package " + "- can be specified multiple times")), + + optparse.Option("--cover-tests", action="store_true", dest="cover_tests", + metavar="[NOSE_COVER_TESTS]", default=False, + help="Include test modules in coverage report "), + + optparse.Option('--cover-branch', action='store_true', + help="Measure branch execution. HIGHLY EXPERIMENTAL!"), + + optparse.Option('--cover-directory', action='store', metavar="DIR", + help="Write the output files to DIR."), + + optparse.Option('--cover-ignore-errors', action='store_true', + help="Ignore errors while reading source files."), + + optparse.Option('--cover-pylib', action='store_true', + help=("Measure coverage even inside the Python installed " + "library, which isn't done by default.")), + + optparse.Option('--cover-show-missing', action='store_true', + help=("Show line numbers of statements in each module " + "that weren't executed.")), + + optparse.Option('--cover-omit', action='store', + metavar="PRE1,PRE2,...", default='', + help=("Omit files when their filename path starts with " + "one of these prefixes.")), + + optparse.Option('--cover-outfile', action='store', metavar="OUTFILE", + help=("Write the XML report to this file. Defaults to " + "'coverage.xml'")), + + optparse.Option('--cover-parallel-mode', action='store_true', + help=("Include the machine name and process id in the " + ".coverage data file name.")), + + optparse.Option('--cover-timid', action='store_true', + help=("Use a simpler but slower trace method. Try this " + "if you get seemingly impossible results!")), + + optparse.Option('--cover-append', action='store_false', + help=("Append coverage data to .coverage, otherwise it " + "is started clean with each run.")) ] # py.test plugin hooks @@ -189,6 +162,7 @@ def pytest_configure(config): config.pluginmanager.register(DoCover(config), "do_coverage") class DoCover: + def __init__(self, config): self.config = config @@ -199,18 +173,16 @@ class DoCover: self.coverage.start() def pytest_terminal_summary(self, terminalreporter): - # Finished the tests start processing the coverage config = terminalreporter.config tw = terminalreporter._tw tw.sep('-', 'coverage') tw.line('Processing Coverage...') self.coverage.finish() - + # XXX please make the following unnessary # Monkey patch omit_filter to use regex patterns for file omits def omit_filter(omit_prefixes, code_units): - import re exclude_patterns = [re.compile(line.strip()) for line in omit_prefixes if line and not line.startswith('#')] filtered = [] |