我们从Python开源项目中,提取了以下33个代码示例,用于说明如何使用coverage.collector()。
def start(self): """Start measuring code coverage. Coverage measurement actually occurs in functions called after :meth:`start` is invoked. Statements in the same scope as :meth:`start` won't be measured. Once you invoke :meth:`start`, you must also call :meth:`stop` eventually, or your process might not shut down cleanly. """ self._init() if self.run_suffix: # Calling start() means we're running code, so use the run_suffix # as the data_suffix when we eventually save the data. self.data_suffix = self.run_suffix if self._auto_data: self.load() self.collector.start() self._started = True self._measured = True
def load(self): """Load previously-collected coverage data from the data file.""" self.collector.reset() self.data.read()
def stop(self): """Stop measuring code coverage.""" self._started = False self.collector.stop()
def erase(self): """Erase previously-collected coverage data. This removes the in-memory data collected in this session as well as discarding the data file. """ self.collector.reset() self.data.erase()
def _harvest_data(self): """Get the collected data and reset the collector. Also warn about various problems collecting data. """ if not self._measured: return self.data.add_line_data(self.collector.get_line_data()) self.data.add_arc_data(self.collector.get_arc_data()) self.collector.reset() # If there are still entries in the source_pkgs list, then we never # encountered those packages. if self._warn_unimported_source: for pkg in self.source_pkgs: self._warn("Module %s was never imported." % pkg) # Find out if we got any data. summary = self.data.summary() if not summary and self._warn_no_data: self._warn("No data was collected.") # Find files that were never executed at all. for src in self.source: for py_file in find_python_files(src): py_file = self.file_locator.canonical_filename(py_file) if self.omit_match and self.omit_match.match(py_file): # Turns out this file was omitted, so don't pull it back # in as unexecuted. continue self.data.touch_file(py_file) self._measured = False # Backward compatibility with version 1.
def load(self): """Load previously-collected coverage data from the data file.""" self._init() self.collector.reset() self.data_files.read(self.data)
def stop(self): """Stop measuring code coverage.""" if self._started: self.collector.stop() self._started = False
def erase(self): """Erase previously-collected coverage data. This removes the in-memory data collected in this session as well as discarding the data file. """ self._init() self.collector.reset() self.data.erase() self.data_files.erase(parallel=self.config.parallel)
def start(self): """Start measuring code coverage. Coverage measurement actually occurs in functions called after `start` is invoked. Statements in the same scope as `start` won't be measured. Once you invoke `start`, you must also call `stop` eventually, or your process might not shut down cleanly. """ if self.run_suffix: # Calling start() means we're running code, so use the run_suffix # as the data_suffix when we eventually save the data. self.data_suffix = self.run_suffix if self.auto_data: self.load() # Create the matchers we need for _should_trace if self.source or self.source_pkgs: self.source_match = TreeMatcher(self.source) else: if self.cover_dir: self.cover_match = TreeMatcher([self.cover_dir]) if self.pylib_dirs: self.pylib_match = TreeMatcher(self.pylib_dirs) if self.include: self.include_match = FnmatchMatcher(self.include) if self.omit: self.omit_match = FnmatchMatcher(self.omit) # The user may want to debug things, show info if desired. if self.debug.should('config'): self.debug.write("Configuration values:") config_info = sorted(self.config.__dict__.items()) self.debug.write_formatted_info(config_info) if self.debug.should('sys'): self.debug.write("Debugging info:") self.debug.write_formatted_info(self.sysinfo()) self.collector.start() self._started = True self._measured = True
def covered_main(includes, require_native=None, required_percentage=100.0, disable_coverage=True): """Equivalent of unittest.main(), except that it gathers coverage data, and asserts if the test is not at 100% coverage. Args: includes (list(str) or str) - List of paths to include in coverage report. May also be a single path instead of a list. require_native (str) - If non-None, will require that at least |require_native| version of coverage is installed on the system with CTracer. disable_coverage (bool) - If True, just run unittest.main() without any coverage tracking. Bug: crbug.com/662277 """ if disable_coverage: unittest.main() return try: import coverage if require_native is not None: got_ver = coverage.__version__ if not getattr(coverage.collector, 'CTracer', None): native_error(( "Native python-coverage module required.\n" "Pure-python implementation (version: %s) found: %s" ) % (got_ver, coverage), require_native) if got_ver < distutils.version.LooseVersion(require_native): native_error("Wrong version (%s) found: %s" % (got_ver, coverage), require_native) except ImportError: if require_native is None: sys.path.insert(0, os.path.join(ROOT_PATH, 'third_party')) import coverage else: print ("ERROR: python-coverage (%s) is required to be installed on your " "PYTHONPATH to run this test." % require_native) sys.exit(1) COVERAGE = coverage.coverage(include=includes) COVERAGE.start() retcode = 0 try: unittest.main() except SystemExit as e: retcode = e.code or retcode COVERAGE.stop() if COVERAGE.report() < required_percentage: print 'FATAL: not at required %f%% coverage.' % required_percentage retcode = 2 return retcode
def get_data(self): """Get the collected data and reset the collector. Also warn about various problems collecting data. Returns a :class:`coverage.CoverageData`, the collected coverage data. .. versionadded:: 4.0 """ self._init() if not self._measured: return self.data self.collector.save_data(self.data) # If there are still entries in the source_pkgs list, then we never # encountered those packages. if self._warn_unimported_source: for pkg in self.source_pkgs: if pkg not in sys.modules: self._warn("Module %s was never imported." % pkg) elif not ( hasattr(sys.modules[pkg], '__file__') and os.path.exists(sys.modules[pkg].__file__) ): self._warn("Module %s has no Python source." % pkg) else: self._warn("Module %s was previously imported, but not measured." % pkg) # Find out if we got any data. if not self.data and self._warn_no_data: self._warn("No data was collected.") # Find files that were never executed at all. for src in self.source: for py_file in find_python_files(src): py_file = files.canonical_filename(py_file) if self.omit_match and self.omit_match.match(py_file): # Turns out this file was omitted, so don't pull it back # in as unexecuted. continue self.data.touch_file(py_file) if self.config.note: self.data.add_run_info(note=self.config.note) self._measured = False return self.data # Backward compatibility with version 1.