def run(self): """ Determine the code's test coverage and return that as a float http://nedbatchelder.com/code/coverage/ """ if can_coverage: files = generate_filelist() cov = None # attempt to load previously cached coverage information if it exists try: cov = coverage.coverage(branch=True, data_file='wmcore-coverage.dat') cov.load() except Exception: cov = coverage.coverage(branch=True, ) cov.start() # runUnitTests() Undefined, no idea where this was supposed to come from - EWV cov.stop() cov.save() # we have our coverage information, now let's do something with it # get a list of modules cov.report(morfs=files, file=sys.stdout) return 0 else: print('You need the coverage module installed before running the' + \ ' coverage command')
def _wrap_coverage(cls, runner, *args): if 'PEX_COVERAGE' not in os.environ and 'PEX_COVERAGE_FILENAME' not in os.environ: runner(*args) return try: import coverage except ImportError: die('Could not bootstrap coverage module, aborting.') if 'PEX_COVERAGE_FILENAME' in os.environ: cov = coverage.coverage(data_file=os.environ['PEX_COVERAGE_FILENAME']) else: cov = coverage.coverage(data_suffix=True) TRACER.log('Starting coverage.') cov.start() try: runner(*args) finally: TRACER.log('Stopping coverage') cov.stop() # TODO(wickman) Post-process coverage to elide $PEX_ROOT and make # the report more useful/less noisy. #89 if 'PEX_COVERAGE_FILENAME' in os.environ: cov.save() else: cov.report(show_missing=False, ignore_errors=True, file=sys.stdout)
def test_ignore_stdlib(self): self.make_file("mymain.py", """\ import colorsys a = 1 hls = colorsys.rgb_to_hls(1.0, 0.5, 0.0) """) # Measure without the stdlib. cov1 = coverage.coverage() self.assertEqual(cov1.config.cover_pylib, False) self.start_import_stop(cov1, "mymain") # some statements were marked executed in mymain.py _, statements, missing, _ = cov1.analysis("mymain.py") self.assertNotEqual(statements, missing) # but none were in colorsys.py _, statements, missing, _ = cov1.analysis("colorsys.py") self.assertEqual(statements, missing) # Measure with the stdlib. cov2 = coverage.coverage(cover_pylib=True) self.start_import_stop(cov2, "mymain") # some statements were marked executed in mymain.py _, statements, missing, _ = cov2.analysis("mymain.py") self.assertNotEqual(statements, missing) # and some were marked executed in colorsys.py _, statements, missing, _ = cov2.analysis("colorsys.py") self.assertNotEqual(statements, missing)
def _wrap_coverage(self, runner, *args): if not self._vars.PEX_COVERAGE and self._vars.PEX_COVERAGE_FILENAME is None: runner(*args) return try: import coverage except ImportError: die('Could not bootstrap coverage module, aborting.') pex_coverage_filename = self._vars.PEX_COVERAGE_FILENAME if pex_coverage_filename is not None: cov = coverage.coverage(data_file=pex_coverage_filename) else: cov = coverage.coverage(data_suffix=True) TRACER.log('Starting coverage.') cov.start() try: runner(*args) finally: TRACER.log('Stopping coverage') cov.stop() # TODO(wickman) Post-process coverage to elide $PEX_ROOT and make # the report more useful/less noisy. #89 if pex_coverage_filename: cov.save() else: cov.report(show_missing=False, ignore_errors=True, file=sys.stdout)
def run(self): """ Determine the code's test coverage and return that as a float http://nedbatchelder.com/code/coverage/ """ if can_coverage: files = generate_filelist() dataFile = None cov = None # attempt to load previously cached coverage information if it exists try: dataFile = open("wmcore-coverage.dat", "r") cov = coverage.coverage(branch=True, data_file="wmcore-coverage.dat") cov.load() except: cov = coverage.coverage(branch=True) cov.start() runUnitTests() cov.stop() cov.save() # we have our coverage information, now let's do something with it # get a list of modules cov.report(morfs=files, file=sys.stdout) return 0 else: print "You need the coverage module installed before running the" + " coverage command"
def test_parse_errors(self): # Im-parsable values raise CoverageException, with details. bad_configs_and_msgs = [ ("[run]\ntimid = maybe?\n", r"maybe[?]"), ("timid = 1\n", r"timid = 1"), ("[run\n", r"\[run"), ( "[report]\nexclude_lines = foo(\n", r"Invalid \[report\].exclude_lines value 'foo\(': " r"(unbalanced parenthesis|missing \))", ), ( "[report]\npartial_branches = foo[\n", r"Invalid \[report\].partial_branches value 'foo\[': " r"(unexpected end of regular expression|unterminated character set)", ), ( "[report]\npartial_branches_always = foo***\n", r"Invalid \[report\].partial_branches_always value " r"'foo\*\*\*': " r"multiple repeat", ), ] for bad_config, msg in bad_configs_and_msgs: print("Trying %r" % bad_config) self.make_file(".coveragerc", bad_config) with self.assertRaisesRegex(CoverageException, msg): coverage.coverage()
def test_parse_errors(self): # Im-parseable values raise CoverageException self.make_file(".coveragerc", """\ [run] timid = maybe? """) with self.assertRaises(CoverageException): coverage.coverage()
def test_unreadable_config(self): # If a config file is explicitly specified, then it is an error for it # to not be readable. bad_files = ["nosuchfile.txt", "."] for bad_file in bad_files: msg = "Couldn't read %r as a config file" % bad_file with self.assertRaisesRegex(CoverageException, msg): coverage.coverage(config_file=bad_file)
def status(self) : 'calculate & return current coverage' with mdb.connection().cursor() as db : db.execute('SELECT SUM(MIN(goal, hits)) AS hits, SUM(goal) AS goal, SUM(MIN(goal+max_hits, rhits)) AS rhits, SUM(goal+max_hits) AS rgoal FROM '+self.covg+' WHERE goal > 0;') hits, goal, rhits, rgoal = db.fetchone() covrge=coverage.coverage(hits=hits, goal=goal) robust=coverage.coverage(hits=rhits, goal=rgoal) def metric() : 'be clear instead of using lambda' return robust if self.robust else covrge return mdb.accessor(coverage=covrge, robust=robust, metric=metric)
def test_data_file_from_environment(self): # There's an environment variable for the data_file. self.make_file(".coveragerc", """\ [run] timid = True data_file = weirdo.file """) self.set_environ("COVERAGE_FILE", "fromenv.dat") cov = coverage.coverage() self.assertEqual(cov.config.data_file, "fromenv.dat") # But the constructor args override the env var. cov = coverage.coverage(data_file="fromarg.dat") self.assertEqual(cov.config.data_file, "fromarg.dat")
def test_filenames(self): self.make_file( "mymain.py", """\ import mymod a = 1 """, ) self.make_file( "mymod.py", """\ fooey = 17 """, ) # Import the python file, executing it. cov = coverage.coverage() cov.start() self.import_local_file("mymain") # pragma: recursive coverage cov.stop() # pragma: recursive coverage filename, _, _, _ = cov.analysis("mymain.py") self.assertEqual(os.path.basename(filename), "mymain.py") filename, _, _, _ = cov.analysis("mymod.py") self.assertEqual(os.path.basename(filename), "mymod.py") filename, _, _, _ = cov.analysis(sys.modules["mymain"]) self.assertEqual(os.path.basename(filename), "mymain.py") filename, _, _, _ = cov.analysis(sys.modules["mymod"]) self.assertEqual(os.path.basename(filename), "mymod.py") # Import the python file, executing it again, once it's been compiled # already. cov = coverage.coverage() cov.start() self.import_local_file("mymain") # pragma: recursive coverage cov.stop() # pragma: recursive coverage filename, _, _, _ = cov.analysis("mymain.py") self.assertEqual(os.path.basename(filename), "mymain.py") filename, _, _, _ = cov.analysis("mymod.py") self.assertEqual(os.path.basename(filename), "mymod.py") filename, _, _, _ = cov.analysis(sys.modules["mymain"]) self.assertEqual(os.path.basename(filename), "mymain.py") filename, _, _, _ = cov.analysis(sys.modules["mymod"]) self.assertEqual(os.path.basename(filename), "mymod.py")
def start(self): """Erase any previous coverage data and start coverage.""" self.cov = coverage.coverage(source=self.cov_source, branch=self.cov_branch, config_file=self.cov_config) self.combining_cov = coverage.coverage(source=self.cov_source, branch=self.cov_branch, data_file=os.path.abspath(self.cov.config.data_file), config_file=self.cov_config) if self.cov_append: self.cov.load() else: self.cov.erase() self.cov.start() self.set_env()
def init(): # Only continue if ancestor process has set everything needed in # the env. global active_cov cov_source = os.environ.get('COV_CORE_SOURCE') cov_config = os.environ.get('COV_CORE_CONFIG') cov_datafile = os.environ.get('COV_CORE_DATAFILE') if cov_datafile: # Import what we need to activate coverage. import coverage # Determine all source roots. if not cov_source: cov_source = None else: cov_source = cov_source.split(os.pathsep) if not cov_config: cov_config = True # Activate coverage for this process. cov = active_cov = coverage.coverage( source=cov_source, data_suffix=True, config_file=cov_config, auto_data=True, data_file=cov_datafile ) cov.load() cov.start() cov._warn_no_data = False cov._warn_unimported_source = False return cov
def make_report(source_dir, report_dir, use_cache=False): #code adapted from /bin/test bin_dir = os.path.abspath(os.path.dirname(__file__)) # bin/ sympy_top = os.path.split(bin_dir)[0] # ../ sympy_dir = os.path.join(sympy_top, 'sympy') # ../sympy/ if os.path.isdir(sympy_dir): sys.path.insert(0, sympy_top) os.chdir(sympy_top) cov = coverage.coverage() cov.exclude("raise NotImplementedError") cov.exclude("def canonize") # this should be "@decorated" cov.exclude("def __mathml__") if use_cache: cov.load() else: cov.erase() cov.start() import sympy sympy.test(source_dir, subprocess=False) #sympy.doctest() #coverage doesn't play well with doctests cov.stop() cov.save() covered_files = list(generate_covered_files(source_dir)) if report_dir in os.listdir(os.curdir): for f in os.listdir(report_dir): if f.split('.')[-1] in ['html', 'css', 'js']: os.remove(os.path.join(report_dir, f)) cov.html_report(morfs=covered_files, directory=report_dir)
def coverage_html(): import os.path if not os.path.isfile('.coverage-modules'): sys.stderr.write("No .coverage-modules file; nothing to do\n") return with open('.coverage-modules','r') as f: modules = [x for x in f.read().split('\n') if x.strip()] cov = coverage.coverage() cov.load() # import everything for m in modules: try: base = m.split('.')[0] roslib.load_manifest(base) __import__(m) except: sys.stderr.write("WARN: cannot import %s\n"%(base)) modlist = '\n'.join([" * %s"%m for m in modules]) sys.stdout.write("Generating for\n%s\n"%(modlist)) # load the module instances to pass to coverage so it can generate annotation html reports mods = [] # TODO: rewrite, buggy for m in modules: mods.extend([v for v in sys.modules.values() if v and v.__name__.startswith(m) and not v in mods]) # dump the output to covhtml directory cov.html_report(mods, directory="covhtml")
def self_test(): import coverage # This way the bots don't need coverage.py to be installed. args = {} cases = [ 'Pretend-iOS-Bot', 'Test-Android-GCC-AndroidOne-GPU-Mali400MP2-Arm7-Release', 'Test-Android-GCC-Nexus9-GPU-TegraK1-Arm64-Debug', 'Test-Android-GCC-GalaxyS3-GPU-Mali400-Arm7-Debug', 'Test-Android-GCC-GalaxyS4-GPU-SGX544-Arm7-Release', 'Test-Android-GCC-Nexus7-GPU-Tegra3-Arm7-Release', 'Test-Android-GCC-NexusPlayer-CPU-SSSE3-x86-Release', 'Test-Ubuntu-GCC-ShuttleA-GPU-GTX550Ti-x86_64-Release-Valgrind', 'Test-Ubuntu-GCC-GCE-CPU-AVX2-x86_64-Release-TSAN', 'Test-Ubuntu-GCC-GCE-CPU-AVX2-x86_64-Release-Valgrind', 'Test-Win7-MSVC-ShuttleA-GPU-HD2000-x86-Debug-ANGLE', 'Test-Mac10.8-Clang-MacMini4.1-CPU-SSE4-x86_64-Release', ] cov = coverage.coverage() cov.start() for case in cases: args[case] = get_args(case) cov.stop() this_file = os.path.basename(__file__) _, _, not_run, _ = cov.analysis(this_file) filtered = [line for line in not_run if line > cov_start and line < cov_end] if filtered: print 'Lines not covered by test cases: ', filtered sys.exit(1) golden = this_file.replace('.py', '.json') with open(os.path.join(os.path.dirname(__file__), golden), 'w') as f: json.dump(args, f, indent=2, sort_keys=True)
def _report_coverage(self, req, body): self._stop_coverage(req) xml = False html = False path = None body = body['report'] if 'file' in body.keys(): path = body['file'] if path != os.path.basename(path): msg = _("Invalid path") raise exc.HTTPBadRequest(explanation=msg) path = os.path.join(self.data_path, path) else: msg = _("No path given for report file") raise exc.HTTPBadRequest(explanation=msg) if 'xml' in body.keys(): xml = body['xml'] elif 'html' in body.keys(): if not self.combine: msg = _("You can't use html reports without combining") raise exc.HTTPBadRequest(explanation=msg) html = body['html'] if self.combine: data_out = os.path.join(self.data_path, '.nova-coverage') import coverage coverInst = coverage.coverage(data_file=data_out) coverInst.combine() if xml: coverInst.xml_report(outfile=path) elif html: if os.path.isdir(path): msg = _("Directory conflict: %s already exists") % path raise exc.HTTPBadRequest(explanation=msg) coverInst.html_report(directory=path) else: output = open(path, 'w') coverInst.report(file=output) output.close() for service in self.services: service['telnet'].close() else: if xml: apipath = path + '.api' self.coverInst.xml_report(outfile=apipath) for service in self.services: self._report_coverage_telnet(service['telnet'], path + '.%s' % service['service'], xml=True) else: output = open(path + '.api', 'w') self.coverInst.report(file=output) for service in self.services: self._report_coverage_telnet(service['telnet'], path + '.%s' % service['service']) output.close() return {'path': path}
def run_coverage(module): module_name = module.__name__ module_path = module_name.replace('.', os.path.sep) + '.' + module_name.rsplit('_', 1)[-1] cov = coverage() cov.start() assert module.func1(1, 2) == (1 * 2) + 2 + 1 assert module.func2(2) == 2 * 2 if '_include_' in module_name: assert module.main_func(2) == (2 * 3) + ((2 * 2) + 4 + 1) + (2 * 2) cov.stop() out = StringIO() cov.report(file=out) #cov.report([module], file=out) lines = out.getvalue().splitlines() assert any(module_path in line for line in lines), "'%s' not found in coverage report:\n\n%s" % ( module_path, out.getvalue()) mod_file, exec_lines, excl_lines, missing_lines, _ = cov.analysis2(source_file_for(module)) assert module_path in mod_file if '_include_' in module_name: executed = set(exec_lines) - set(missing_lines) assert all(line in executed for line in [7, 12]), '%s / %s' % (exec_lines, missing_lines) # rest of test if for include file mod_file, exec_lines, excl_lines, missing_lines, _ = cov.analysis2( os.path.join(os.path.dirname(module.__file__), "pkg", "coverage_test_pyx.pxi")) executed = set(exec_lines) - set(missing_lines) assert all(line in executed for line in [5, 6, 7, 11]), '%s / %s' % (exec_lines, missing_lines)
def run(self): try: import coverage use_cov = True except: use_cov = False cov = None if use_cov: # The latter is required to not give errors on f23, probably # a temporary bug. omit = ["/usr/*", "/*/tests/*", "/builddir/*"] cov = coverage.coverage(omit=omit) cov.erase() cov.start() import tests as testsmodule testsmodule.cov = cov testsmodule.utils.REGENERATE_OUTPUT = bool(self.regenerate_output) if hasattr(unittest, "installHandler"): try: unittest.installHandler() except: print "installHandler hack failed" tests = unittest.TestLoader().loadTestsFromNames(self._testfiles) if self.only: newtests = [] for suite1 in tests: for suite2 in suite1: for testcase in suite2: if self.only in str(testcase): newtests.append(testcase) if not newtests: print "--only didn't find any tests" sys.exit(1) tests = unittest.TestSuite(newtests) print "Running only:" for test in newtests: print "%s" % test print t = unittest.TextTestRunner(verbosity=1) try: result = t.run(tests) except KeyboardInterrupt: sys.exit(1) if use_cov: cov.stop() cov.save() err = int(bool(len(result.failures) > 0 or len(result.errors) > 0)) if not err and use_cov and self.coverage: cov.report(show_missing=False) sys.exit(err)
def run(self): set_test_environ() # Wipe existing modules, to make sure coverage data is properly # generated for them. for key in list(sys.modules.keys()): if key.startswith('pgi'): del(sys.modules[key]) try: from coverage import coverage except ImportError: print("Missing 'coverage' module. See " "https://pypi.python.org/pypi/coverage or try " "`apt-get install python-coverage`") return cov = coverage() cov.start() import tests tests.test(False, "cffi") dest = os.path.join(os.getcwd(), "coverage") cov.stop() cov.html_report( directory=dest, ignore_errors=True, include=["pgi/*"]) print("Coverage summary: file://%s/index.html" % dest)
def self_test(): import coverage # This way the bots don't need coverage.py to be installed. args = {} cases = [ 'Perf-Android-GalaxyS3-Mali400-Arm7-Release', 'Perf-Android-Nexus7-Tegra3-Arm7-Release', 'Test-Ubuntu12-ShuttleA-GTX550Ti-x86_64-Release-Valgrind', 'Test-Win7-ShuttleA-HD2000-x86-Debug-ANGLE', ] cov = coverage.coverage() cov.start() for case in cases: args[case] = get_args(case) cov.stop() this_file = os.path.basename(__file__) _, _, not_run, _ = cov.analysis(this_file) filtered = [line for line in not_run if line > cov_start and line < cov_end] if filtered: print 'Lines not covered by test cases: ', filtered sys.exit(1) golden = this_file.replace('.py', '.json') with open(os.path.join(os.path.dirname(__file__), golden), 'w') as f: json.dump(args, f, indent=2, sort_keys=True)
def run(self): try: from coverage import coverage except ImportError: raise SystemExit( "Missing 'coverage' module. See " "https://pypi.python.org/pypi/coverage or try " "`apt-get install python-coverage python3-coverage`") for key in list(sys.modules.keys()): if key.startswith('mutagen'): del(sys.modules[key]) cov = coverage() cov.start() cmd = self.reinitialize_command("test") cmd.quick = self.quick cmd.ensure_finalized() cmd.run() dest = os.path.join(os.getcwd(), "coverage") cov.stop() cov.html_report( directory=dest, ignore_errors=True, include=["mutagen/*", "tools/*"]) print("Coverage summary: file://%s/index.html" % dest)
def setUpClass(cls, extra_mocks=None, extra_config_knobs=None): super(TestCase, cls).setUpClass() global cov_handle if not cov_handle: cov_handle = coverage.coverage(source=['./'], omit=['.venv/*']) #cov_handle.start() cfgm_common.zkclient.LOG_DIR = './' gevent.wsgi.WSGIServer.handler_class = FakeWSGIHandler # end setUp cls.orig_mocked_values = setup_mocks(cls.mocks + (extra_mocks or [])) cls._server_info = create_api_server_instance( cls.__name__, cls._config_knobs + (extra_config_knobs or [])) try: cls._api_server_ip = cls._server_info['ip'] cls._api_server_port = cls._server_info['service_port'] cls._api_admin_port = cls._server_info['admin_port'] cls._api_svr_greenlet = cls._server_info['greenlet'] cls._api_svr_app = cls._server_info['app'] cls._vnc_lib = cls._server_info['api_conn'] cls._api_server_session = cls._server_info['api_session'] cls._api_server = cls._server_info['api_server'] except Exception as e: cls.tearDownClass() raise
def runtests(): parser = optparse.OptionParser() parser.add_option('--verbosity', dest='verbosity', default='1') parser.add_option('--coverage', dest='coverage', default='2') parser.add_option('--DATABASE_ENGINE', dest='DATABASE_ENGINE', default='sqlite3') parser.add_option('--DATABASE_NAME', dest='DATABASE_NAME', default='') parser.add_option('--DATABASE_USER', dest='DATABASE_USER', default='') parser.add_option('--DATABASE_PASSWORD', dest='DATABASE_PASSWORD', default='') parser.add_option('--DATABASE_HOST', dest='DATABASE_HOST', default='') parser.add_option('--DATABASE_PORT', dest='DATABASE_PORT', default='') options, args = parser.parse_args() dboptions = {} if options.DATABASE_ENGINE == 'mysql': dboptions = { "init_command": "SET storage_engine=INNODB," "character_set_connection=utf8," "collation_connection=utf8_unicode_ci"} if not options.DATABASE_NAME and options.DATABASE_ENGINE != 'sqlite3': options.DATABASE_NAME = 'treebeard' if not settings.configured: settings.configure( DATABASE_ENGINE=options.DATABASE_ENGINE, DATABASE_NAME=options.DATABASE_NAME, DATABASE_USER=options.DATABASE_USER, DATABASE_PASSWORD=options.DATABASE_PASSWORD, DATABASE_HOST=options.DATABASE_HOST, DATABASE_PORT=options.DATABASE_PORT, DATABASE_OPTIONS=dboptions, INSTALLED_APPS=[ 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.admin', 'treebeard', 'treebeard.tests']) covlevel = int(options.coverage) if covlevel: current_dir = os.path.dirname(os.path.abspath(__file__)) if covlevel == 2: branch = True else: branch = False cov = coverage.coverage(branch=branch, include=[current_dir + '/treebeard/*.py'], omit=[current_dir + '/treebeard/numconv.py', current_dir + '/treebeard/tests/*']) cov.load() cov.start() if not args: args = ['tests'] call_command('test', verbosity=options.verbosity, *args) if covlevel: cov.stop() cov.save()
def start_coverage(cls): try: import coverage cov = coverage.coverage(auto_data=True, data_suffix=True) cov.start() except ImportError: sys.stderr.write('Could not bootstrap coverage module!\n')
def start(self): """Determine what data file and suffix to contribute to and start coverage.""" # Determine whether we are collocated with master. self.is_collocated = (socket.gethostname() == self.config.slaveinput['cov_master_host'] and self.topdir == self.config.slaveinput['cov_master_topdir']) # If we are not collocated then rewrite master paths to slave paths. if not self.is_collocated: master_topdir = self.config.slaveinput['cov_master_topdir'] slave_topdir = self.topdir self.cov_source = [source.replace(master_topdir, slave_topdir) for source in self.cov_source] self.cov_config = self.cov_config.replace(master_topdir, slave_topdir) # Erase any previous data and start coverage. self.cov = coverage.coverage(source=self.cov_source, data_suffix=True, config_file=self.cov_config) if self.cov_append: self.cov.load() else: self.cov.erase() self.cov.start() self.set_env()
def main(): #Cleanup old html report: for root, dirs, files in os.walk('test/output_coverage_html/'): for f in files: if f == '.gitignore' or f == '.empty_dir': continue os.unlink(os.path.join(root, f)) for d in dirs: shutil.rmtree(os.path.join(root, d)) #Perform coverage analisys: if "coverage" in sys.modules: cov = coverage.coverage() cov.start() #Discover the tests and execute them: loader = unittest.TestLoader() tests = loader.discover('./test/') testRunner = unittest.runner.TextTestRunner(descriptions=True, verbosity=1) res = testRunner.run(tests) if "coverage" in sys.modules: cov.stop() cov.html_report() if res.wasSuccessful(): sys.exit(0) else: sys.exit(1)
def load_coverage_data_for(self, context, covered_path, expect_coverage=True): data_file = self.coverage_data_file() self.assertEqual(expect_coverage, os.path.isfile(data_file)) if expect_coverage: python_sources = context.products.get_data(GatherSources.PYTHON_SOURCES) covered_relpath = os.path.relpath(covered_path, self.build_root) owning_targets = [t for t in context.targets() if covered_relpath in t.sources_relative_to_buildroot()] self.assertEqual(1, len(owning_targets)) owning_target = owning_targets[0] src_chroot_path = python_sources.path() src_root_abspath = os.path.join(self.build_root, owning_target.target_base) covered_src_root_relpath = os.path.relpath(covered_path, src_root_abspath) chroot_path = os.path.join(src_chroot_path, covered_src_root_relpath) cp = configparser.SafeConfigParser() src_to_target_base = {src: tgt.target_base for tgt in context.targets() for src in tgt.sources_relative_to_source_root()} PytestRun._add_plugin_config(cp, src_chroot_path=src_chroot_path, src_to_target_base=src_to_target_base) with temporary_file(binary_mode=False) as fp: cp.write(fp) fp.close() coverage_data = coverage.coverage(config_file=fp.name, data_file=data_file) coverage_data.load() _, all_statements, not_run_statements, _ = coverage_data.analysis(chroot_path) return all_statements, not_run_statements
def testnodedown(self, node, error): """Collect data file name from slave.""" # If slave doesn't return any data then it is likely that this # plugin didn't get activated on the slave side. if not (hasattr(node, 'slaveoutput') and 'cov_slave_node_id' in node.slaveoutput): self.failed_slaves.append(node) return # If slave is not collocated then we must save the data file # that it returns to us. if 'cov_slave_lines' in node.slaveoutput: data_suffix = '%s.%s.%06d.%s' % ( socket.gethostname(), os.getpid(), random.randint(0, 999999), node.slaveoutput['cov_slave_node_id'] ) cov = coverage.coverage(source=self.cov_source, data_suffix=data_suffix, config_file=self.cov_config) cov.start() cov.data.lines = node.slaveoutput['cov_slave_lines'] cov.data.arcs = node.slaveoutput['cov_slave_arcs'] cov.stop() cov.save() path = node.slaveoutput['cov_slave_path'] self.cov.config.paths['source'].append(path) # Record the slave types that contribute to the data file. rinfo = node.gateway._rinfo() node_desc = self.get_node_desc(rinfo.platform, rinfo.version_info) self.node_descs.add(node_desc)
def setUp(self): super(TestCase, self).setUp() global cov_handle if not cov_handle: cov_handle = coverage.coverage(source=["./"], omit=[".venv/*"]) cov_handle.start() cfgm_common.zkclient.LOG_DIR = "./" gevent.wsgi.WSGIServer.handler_class = FakeWSGIHandler setup_common_flexmock() self._api_server_ip = socket.gethostbyname(socket.gethostname()) self._api_server_port = get_free_port() http_server_port = get_free_port() self._api_admin_port = get_free_port() self._api_svr_greenlet = gevent.spawn( launch_api_server, self._api_server_ip, self._api_server_port, http_server_port, self._api_admin_port, self._config_knobs, ) block_till_port_listened(self._api_server_ip, self._api_server_port) extra_env = {"HTTP_HOST": "%s%s" % (self._api_server_ip, self._api_server_port)} self._api_svr_app = TestApp(bottle.app(), extra_environ=extra_env) self._vnc_lib = VncApi("u", "p", api_server_host=self._api_server_ip, api_server_port=self._api_server_port) FakeNovaClient.vnc_lib = self._vnc_lib self._api_server_session = requests.Session() adapter = requests.adapters.HTTPAdapter() self._api_server_session.mount("http://", adapter) self._api_server_session.mount("https://", adapter) self._api_server = vnc_cfg_api_server.server self._api_server._sandesh.set_logging_params(level="SYS_WARN")
def mergeConfig(args, testing=False): # pragma: no cover """ I take in a namespace created by the ArgumentParser in cmdline.main() and merge in options from configuration files. The config items only replace argument items that are set to default value. Returns: I return a new argparse.Namespace, adding members: shouldExit = default False exitCode = default 0 include patterns = include-patterns setting converted to list. omit_patterns = omit-patterns settings converted to list and extended, taking clear-omit into account. cov = coverage object default None """ config = getConfig(getattr(args, "config", default_args.config)) new_args = copy.deepcopy(default_args) # Default by default! for name, default_value in dict(default_args._get_kwargs()).items(): # Config options overwrite default options config_getter = None if name in [ "termcolor", "notermcolor", "allow_stdout", "quiet_stdout", "help", "logging", "version", "disable_unidecode", "failfast", "run_coverage", "options", "completions", "completion_file", "clear_omit", "no_skip_report", "no_tracebacks", "disable_windows", "quiet_coverage", "junit_report", ]: config_getter = config.getboolean elif name in ["processes", "debug", "verbose", "minimum_coverage"]: config_getter = config.getint elif name in [ "file_pattern", "finalizer", "initializer", "cov_config_file", "include_patterns", "omit_patterns", "warnings", "test_pattern", ]: config_getter = config.get elif name in ["targets", "help", "config"]: pass # Some options only make sense coming on the command-line. elif name in ["store_opt", "parser"]: pass # These are convenience objects, not actual settings else: raise NotImplementedError(name) if config_getter: try: config_value = config_getter("green", name.replace("_", "-")) setattr(new_args, name, config_value) except (configparser.NoSectionError, configparser.NoOptionError): pass # Command-line values overwrite defaults and config values when # specified args_value = getattr(args, name, "unspecified") if args_value != "unspecified": setattr(new_args, name, args_value) new_args.shouldExit = False new_args.exitCode = 0 new_args.cov = None # Help? if new_args.help: # pragma: no cover new_args.parser.print_help() new_args.shouldExit = True return new_args # Did we just print the version? if new_args.version: from green.version import pretty_version sys.stdout.write(pretty_version() + "\n") new_args.shouldExit = True return new_args # Handle logging options if new_args.debug: logging.basicConfig( level=logging.DEBUG, format="%(asctime)s %(levelname)9s %(message)s", datefmt="%Y-%m-%d %H:%M:%S", ) elif not new_args.logging: logging.basicConfig(filename=os.devnull) # Disable termcolor? if new_args.notermcolor: new_args.termcolor = False # Coverage. We must enable it here because we cannot cover module-level # code after it is imported, and this is the earliest place we can turn on # coverage. omit_patterns = [ "*/argparse*", "*/colorama*", "*/django/*", "*/distutils*", # Gets pulled in on Travis-CI CPython "*/extras*", # pulled in by testtools "*/linecache2*", # pulled in by testtools "*/mimeparse*", # pulled in by testtools "*/mock*", "*/pbr*", # pulled in by testtools "*/pkg_resources*", # pulled in by django "*/pypy*", "*/pytz*", # pulled in by django "*/six*", # pulled in by testtools "*/termstyle*", "*/test*", "*/traceback2*", # pulled in by testtools "*/unittest2*", # pulled in by testtools "*Python.framework*", # OS X system python "*site-packages*", # System python for other OS's tempfile.gettempdir() + "*", ] if new_args.clear_omit: omit_patterns = [] if new_args.omit_patterns: omit_patterns.extend(new_args.omit_patterns.split(",")) new_args.omit_patterns = omit_patterns if new_args.include_patterns: new_args.include_patterns = new_args.include_patterns.split(",") else: new_args.include_patterns = [] if new_args.quiet_coverage or (type(new_args.cov_config_file) == str): new_args.run_coverage = True if new_args.minimum_coverage != None: new_args.run_coverage = True if new_args.run_coverage: if not testing: cov = coverage.coverage( data_file=".coverage", omit=omit_patterns, include=new_args.include_patterns, config_file=new_args.cov_config_file, ) cov.start() new_args.cov = cov return new_args
import os import click import shortuuid from flask.ext.script import Manager, Shell from flask.ext.migrate import Migrate, MigrateCommand from flask.ext.script.commands import ShowUrls from sqlalchemy.exc import IntegrityError from suda import create_app, db from suda.models import Client COV = None if os.environ.get('FLASK_COVERAGE'): import coverage COV = coverage.coverage(branch=True, include='suda/*') COV.start() app = create_app(os.getenv('FLASK_CONFIG') or 'development') manager = Manager(app) migrate = Migrate(app, db) def make_shell_context(): return dict(app=app, db=db) @manager.command def test(coverage=False): """Run the unit test."""
#!flask/bin/python # -*- coding: utf8 -*- from coverage import coverage cov = coverage(branch=True, omit=['env/*', 'tests.py']) import os, pytz import unittest from app import new_world_weather as new from app import app, db from app.models import User, Location from config import basedir from datetime import datetime, timedelta, date, time from mock import patch # import moonphase # mock api results def _helper(**kwargs): return { 'txt_query': kwargs.get('txt_query'), 'user_coord': kwargs.get('user_coord'), 'date': kwargs.get('date') } class MainTestCase(unittest.TestCase): def setUp(self): app.config['TESTING'] = True app.config['CSRF_ENABLED'] = False app.config[
# app = create(group=FlaskGroup) @click.group(cls=FlaskGroup, create_app=create) def manager(): """Management script for app""" manager.help_args = ('-h', '-?', '--help') manager.add_command('db', MigrateCommand) manager.add_command(database.manager, "database") manager.add_command(run_command, "runserver") COV = cover.coverage(branch=True, include='app/*', omit=[ 'app/tests/*', 'app/config/*', ]) @manager.command() def version(): """Displays app version.""" print(__version__) @manager.command() def check_config(): """Show the settings as Redash sees them (useful for debugging).""" for name, item in config.items():
from flask_script import Manager, Server from flask_migrate import Migrate, MigrateCommand from flask_cors import CORS, cross_origin from project.server import create_app, db from flask_jwt_simple import ( JWTManager, jwt_required, create_jwt, get_jwt_identity, get_jwt ) # code coverage COV = coverage.coverage( branch=True, include='project/*', omit=[ 'project/tests/*', 'project/server/config.py', 'project/server/*/__init__.py' ] ) COV.start() app1 = Flask(__name__) perms = Permissions(app1, db, current_user) app = create_app() jwt = JWTManager(app) CORS(app, max_age=600) migrate = Migrate(app, db) manager = Manager(app) manager.add_command("runserver",Server(
#!/usr/bin/env python import os COV = None if os.environ.get('FLASK_COVERAGE'): import coverage COV = coverage.coverage(branch=True, include='app/*') COV.start() if os.path.exists('.env'): print('Importing environment from .env...') for line in open('.env'): var = line.strip().split('=') if len(var) == 2: os.environ[var[0]] = var[1] from app.models import User, Follow, Role, Permission, Post, Comment, NexApiCase from app import db, celery, app from flask_script import Manager, Shell, Command, Option, Server from flask_migrate import Migrate, MigrateCommand from app.workers import server_monitor_worker # register_app_blueprint(app) # celery = Celery(app.name) from gunicorn.app.base import Application class GunicornServer(Command): description = 'Run the app within Gunicorn' def __init__(self, host='0.0.0.0', port=8000, workers=4):
test_verbosity = 1 if options.verbose == 1: logging.root.setLevel(logging.INFO) logger.info("Set log level: INFO") elif options.verbose > 1: logging.root.setLevel(logging.DEBUG) logger.info("Set log level: DEBUG") test_verbosity = 2 if options.coverage: logger.info("Running test-coverage") import coverage try: cov = coverage.Coverage(omit=["*test*", "*third_party*", "*/setup.py"]) except AttributeError: cov = coverage.coverage(omit=["*test*", "*third_party*", "*/setup.py"]) cov.start() # Prevent importing from source directory if (os.path.dirname(os.path.abspath(__file__)) == os.path.abspath( sys.path[0])): removed_from_sys_path = sys.path.pop(0) logger.info("Patched sys.path, removed: '%s'" % removed_from_sys_path) # import module if not options.insource: try: module = importer(PROJECT_NAME) except: logger.warning("%s missing, using built (i.e. not installed) version", PROJECT_NAME)
def get_coverage(self): workman = coverage.coverage() workman.load() workman._harvest_data() reporter = CoverallReporter(workman, workman.config) return reporter.report()
params['database'] = database params['userName'] = user params[ '%userName%::Mssql@%hostName%:%port%'] = '%userName%::Mssql@%hostName%:%port%' params['password'] = password return conn class MigrationTestCase(unittest.TestCase): connection = create_connection(**test_params) if __name__ == '__main__': if has_coverage: cov = coverage.coverage() cov.start() os.chdir(CURRENT_DIR) sys.path[:0] = [CURRENT_DIR, os.path.join(SRC_DIR, 'library/python')] suite = unittest.TestSuite() names = [os.path.splitext(fname)[0] for fname in glob.glob("test_*.py")] suite.addTest(unittest.defaultTestLoader.loadTestsFromNames(names)) result = unittest.TextTestRunner(stream=open( os.path.join(SRC_DIR, 'testing/python/test_results_migration.txt'), 'w'), verbosity=2).run(suite) if has_coverage:
def run_iptestall(options): """Run the entire IPython test suite by calling nose and trial. This function constructs :class:`IPTester` instances for all IPython modules and package and then runs each of them. This causes the modules and packages of IPython to be tested each in their own subprocess using nose. Parameters ---------- All parameters are passed as attributes of the options object. testgroups : list of str Run only these sections of the test suite. If empty, run all the available sections. fast : int or None Run the test suite in parallel, using n simultaneous processes. If None is passed, one process is used per CPU core. Default 1 (i.e. sequential) inc_slow : bool Include slow tests, like IPython.parallel. By default, these tests aren't run. xunit : bool Produce Xunit XML output. This is written to multiple foo.xunit.xml files. coverage : bool or str Measure code coverage from tests. True will store the raw coverage data, or pass 'html' or 'xml' to get reports. extra_args : list Extra arguments to pass to the test subprocesses, e.g. '-v' """ if options.fast != 1: # If running in parallel, capture output so it doesn't get interleaved TestController.buffer_output = True to_run, not_run = prepare_controllers(options) def justify(ltext, rtext, width=70, fill='-'): ltext += ' ' rtext = (' ' + rtext).rjust(width - len(ltext), fill) return ltext + rtext # Run all test runners, tracking execution time failed = [] t_start = time.time() print() if options.fast == 1: # This actually means sequential, i.e. with 1 job for controller in to_run: print('IPython test group:', controller.section) sys.stdout.flush() # Show in correct order when output is piped controller, res = do_run(controller) if res: failed.append(controller) if res == -signal.SIGINT: print("Interrupted") break print() else: # Run tests concurrently try: pool = multiprocessing.pool.ThreadPool(options.fast) for (controller, res) in pool.imap_unordered(do_run, to_run): res_string = 'OK' if res == 0 else 'FAILED' print( justify('IPython test group: ' + controller.section, res_string)) if res: print(bytes_to_str(controller.stdout)) failed.append(controller) if res == -signal.SIGINT: print("Interrupted") break except KeyboardInterrupt: return for controller in not_run: print(justify('IPython test group: ' + controller.section, 'NOT RUN')) t_end = time.time() t_tests = t_end - t_start nrunners = len(to_run) nfail = len(failed) # summarize results print('_' * 70) print('Test suite completed for system with the following information:') print(report()) took = "Took %.3fs." % t_tests print('Status: ', end='') if not failed: print('OK (%d test groups).' % nrunners, took) else: # If anything went wrong, point out what command to rerun manually to # see the actual errors and individual summary failed_sections = [c.section for c in failed] print( 'ERROR - {} out of {} test groups failed ({}).'.format( nfail, nrunners, ', '.join(failed_sections)), took) print() print('You may wish to rerun these, with:') print(' iptest', *failed_sections) print() if options.coverage: from coverage import coverage cov = coverage(data_file='.coverage') cov.combine() cov.save() # Coverage HTML report if options.coverage == 'html': html_dir = 'ipy_htmlcov' shutil.rmtree(html_dir, ignore_errors=True) print("Writing HTML coverage report to %s/ ... " % html_dir, end="") sys.stdout.flush() # Custom HTML reporter to clean up module names. from coverage.html import HtmlReporter class CustomHtmlReporter(HtmlReporter): def find_code_units(self, morfs): super(CustomHtmlReporter, self).find_code_units(morfs) for cu in self.code_units: nameparts = cu.name.split(os.sep) if 'IPython' not in nameparts: continue ix = nameparts.index('IPython') cu.name = '.'.join(nameparts[ix:]) # Reimplement the html_report method with our custom reporter cov._harvest_data() cov.config.from_args( omit='*{0}tests{0}*'.format(os.sep), html_dir=html_dir, html_title='IPython test coverage', ) reporter = CustomHtmlReporter(cov, cov.config) reporter.report(None) print('done.') # Coverage XML report elif options.coverage == 'xml': cov.xml_report(outfile='ipy_coverage.xml') if failed: # Ensure that our exit code indicates failure sys.exit(1)
def run( path=None, app='welcome', test_key='secret', test_options={'verbosity': 1}, test_report=None, coverage_report=None, coverage_exclude=None, coverage_include=None, DO_COVER=True, DO_NOSE=True, ): """ Run all tests in ``path`` for ``app``. It will automatically exclude this tester file, gluon, '/usr', 'app/languages', 'app/tests', 'app/docs', 'app/private'. .. note:: If the current working directory is the path to web2py you do not need to specify path, you may leave it None. If you are in the web2py directory is determined by the ability to ``import gluon``. Keyword Arguments: path -- The path to the web2py directory. app -- Name of application to test. test_key -- Secret key to inject into the WSGI environment test_options -- Dictionary of options to pass along to the test runner This is gets passed to either Nosetests or TextTestRunner depending on what is available. IE: {'verbosity': 3} test_report -- Path to a file, all output will be redirected here. This redirects sys.stdout and sys.stderr to this file. coverage_report -- If ``test_report`` is none, this will print the coverage report to this file, if coverage is installed. coverage_exclude -- List of omit_prefixes. If a filepath starts with this value it will be omitted from the report coverage_include -- List of files to include in the report. DO_COVER -- If False, will disable coverage even if it is installed DO_NOSE -- If False, will use unittest.TestTextRunner, even if nosetests is is installed. """ if test_report: print "Redirecting output to: ", test_report _STDOUT = sys.stdout _STDERR = sys.stderr _TR = open(test_report, 'w') sys.stdout = _TR sys.stderr = _TR print "The following modules are available" print "WebTest: ", _WEBTEST print "NoseTest: ", _NOSE print "Coverage: ", _COVER # Set our globals global _PATH global _APP_PATH global _TEST_APP_KEY if _GLUON and not path: path = os.getcwd() _PATH = path _APP_PATH = os.path.join(_PATH, 'applications', app) _TEST_APP_KEY = test_key # We need to make sure gluon is accessible, since it expects to be in the # root folder. if not _GLUON: sys.path.append(_PATH) os.chdir(_PATH) # Get a suite of tests for app. suite = get_suite(app=app) if _COVER and DO_COVER: cov = coverage() cov.start() if _NOSE and DO_NOSE: nose.core.run(suite=suite, config=nose.config.Config(**test_options)) else: unittest.TextTestRunner(**test_options).run(suite) if _COVER and DO_COVER: cov.stop() # Try to open a file, otherwise # use stringio. if isinstance(coverage_report, str) and not test_report: rpt = open(coverage_report, 'w') STRINGIO = False else: rpt = cStringIO.StringIO() STRINGIO = True # We want to omit these from the report. omit = ['gluon', '/usr', os.path.abspath(__file__)] # Ignore these folders. folders_to_omit = ['languages', 'tests', 'docs', 'private'] for f in folders_to_omit: omit.append(os.path.join('applications', app, f)) # Add any custom omissions if isinstance(coverage_exclude, (tuple, list)): omit.extend(coverage_exclude) # Now we need to get every python file that could # possibly be reported on. This way we only report # python files that exist in our app. # I don't think we are interested in coverage of external # modules, they should have their own tests anyways. report_on = [] for root, dirnames, filenames in os.walk(_APP_PATH): for filename in fnmatch.filter(filenames, '*.py'): report_on.append(os.path.join(root, filename)) if isinstance(coverage_include, (tuple, list)): report_on.extend(coverage_include) # Do the report. cov.report(morfs=report_on, file=rpt, omit_prefixes=omit) # If we arn't saving the report to a file # go ahead and print it out. if STRINGIO: print rpt.getvalue()
#!/usr/bin/env python import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "forecast.settings") from django.core.management import execute_from_command_line is_testing = 'test' in sys.argv if is_testing: import coverage cov = coverage.coverage( source=['opportunities'], omit=['opportunities/migrations/**', '*/tests.py']) cov.erase() cov.start() execute_from_command_line(sys.argv) if is_testing: cov.stop() cov.save() cov.report()
#!/usr/bin/env python import os import unittest from coverage import coverage cov = coverage(branch=True, omit=['/Users/ontoral/.virtualenvs/Fe/*', 'tests.py']) cov.start() from config import basedir from CC2013 import app, db from CC2013.models import * class TestCase(unittest.TestCase): def setUp(self): app.config['TESTING'] = True app.config['CSRF_ENABLED'] = False db_filename = os.path.join(basedir, 'test.db') app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + db_filename self.app = app.test_client() db.create_all() def tearDown(self): db.session.remove() db.drop_all() # Models # Unit
#!flask/bin/python import os import unittest from config import basedir from app import app, db from app.models import User, Post from coverage import coverage from activate_this import base from datetime import datetime cov = coverage(branch=True, omit=['tests.py']) cov.start() class TestCase(unittest.TestCase): def setUp(self): app.config['TESTING'] = True app.config['WFT_CSRF_ENABLED'] = False testdb = os.path.join(basedir, 'test.db') app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + testdb db.create_all() def tearDown(self): db.session.remove() db.drop_all() def test_avatar(self):
#!flask/bin/python import os import unittest from coverage import coverage from datetime import datetime, timedelta from app.translate import microsoft_translate from config import basedir from app import app, db from app.models import User, Post cov = coverage(branch=True, omit=['flask/*', 'tests.py']) cov.start() class TestCase(unittest.TestCase): def setUp(self): app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join( basedir, 'test.db') self.app = app.test_client() db.create_all() def tearDown(self): db.session.remove() db.drop_all() def test_avatar(self): u = User(nickname='john', email='*****@*****.**') avatar = u.avatar(128).split('?')[0] self.assertEqual(
from flask.cli import FlaskGroup import sys import coverage import unittest from project import create_app, db from project.api.models import User COV = coverage.coverage(branch=True, include="project/*", omit=[ "project/tests/*", "project/config.py", ]) COV.start() app = create_app() cli = FlaskGroup(create_app=create_app) @cli.command() def cov(): """Runs the unit tests with coverage.""" tests = unittest.TestLoader().discover("project/tests") result = unittest.TextTestRunner(verbosity=2).run(tests) if result.wasSuccessful(): COV.stop() COV.save() print("Coverage Summary:") COV.report() COV.html_report()
import unittest import os import sys from copa_transparente.tests import JSONTestRunner try: import coverage except: coverage = None if __name__ == "__main__": if coverage: cov = coverage.coverage(branch=True, source=['copa_transparente']) cov.start() loader = unittest.TestLoader() JSONTestRunner().run(loader.discover('copa_transparente/tests/')) if coverage: cov.stop() cov.save() cov.html_report(directory='htmlcov') cov.report(show_missing=False)
from pygments import highlight from pygments.formatters import Terminal256Formatter, TerminalFormatter from pygments.lexers import get_lexer_by_name if False: for i in range(16): print('%03d' % (i * 16), end='') for j in range(16): print(colors.color(' ', bg=(i * 16 + j)), end='') print() # import os # os.exit(0) from coverage import coverage cov = coverage() cov.load() total_statements = 0 total_missing = 0 total_files = 0 total_covered = 0 coverage_data = [] biggest_prefix = None for fname in cov.data.measured_files(): if fname.startswith('/tmp'): continue # XXX REMOVE if len(sys.argv) > 1: match = False for arg in sys.argv[1:]: if arg in fname: match = True
biostar.sh test" """ import sys, os, django if django.VERSION < (1, 3): print '*** Django version 1.3 or higher required.' print '*** Your version is %s' % '.'.join(map(str, django.VERSION)) sys.exit() from django.utils import unittest from django.test.simple import DjangoTestSuiteRunner from coverage import coverage cov = coverage(include=['main/*'], omit=['main/server/tests/*']) COVERAGE = 0 if COVERAGE: cov.start() # add our own testing suites from main.server import html from main.server.tests import test_models, test_site def path(*args): "Generates absolute paths" return os.path.abspath(os.path.join(*args))
#!/usr/bin/env python import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_settings") from django.core.management import execute_from_command_line is_testing = 'test' in sys.argv if is_testing: import coverage cov = coverage.coverage(source=['easy_timezones'], omit=['*tests.py']) cov.erase() cov.start() execute_from_command_line(sys.argv) if is_testing: cov.stop() cov.save() cov.report()
# Boston, MA 02111-1307 USA from __future__ import print_function import os import sys import unittest import coverage testfolder = os.path.abspath(os.path.dirname(__file__)) package_root = os.path.abspath(os.path.join(testfolder, r"..\..")) sys.path.append(package_root) # needs to be called before importing the modules cov = coverage.coverage(branch=True, omit=os.path.join(package_root, 'pywinauto', '*tests', '*.py')) cov.start() import pywinauto pywinauto.actionlogger.enable() # increase timings for AppVeyor pywinauto.timings.Timings.app_start_timeout = 20 pywinauto.timings.Timings.window_find_timeout = 50 modules_to_test = [pywinauto] def run_tests():
import coverage COV = coverage.coverage( branch=True, include='project/*', omit=[ 'project/tests/*', 'project/config.py' ] ) COV.start() from flask.cli import FlaskGroup from project import app, db import unittest from populate import seedReceipts, seedTags cli = FlaskGroup(app) # Create command @cli.command() def recreatedb(): db.drop_all() db.create_all() db.session.commit() # Populate Functions @cli.command() def seed(): seedTags(db)
import os from subprocess import call import coverage import sys import click from flask import current_app from flask.cli import with_appcontext from flask_migrate import upgrade from werkzeug.exceptions import MethodNotAllowed, NotFound COV = None if os.environ.get("FLASK_COVERAGE"): COV = coverage.coverage(branch=True, include="./*") COV.start() @click.command() @click.option( "--coverage/--no-coverage", default=False, help="Run tests under code coverage.", ) def test(coverage): """Run the unit tests.""" if coverage and not os.environ.get("FLASK_COVERAGE"): import subprocess os.environ["FLASK_COVERAGE"] = "1" sys.exit(subprocess.call(sys.argv)) import unittest
def _set_up_runner(self): h = self.host args = self.args self.stats = Stats(args.status_format, h.time, args.jobs) self.printer = Printer(self.print_, args.overwrite, args.terminal_width) if self.args.top_level_dirs and self.args.top_level_dir: self.print_( 'Cannot specify both --top-level-dir and --top-level-dirs', stream=h.stderr) return 1 self.top_level_dirs = args.top_level_dirs if not self.top_level_dirs and args.top_level_dir: self.top_level_dirs = [args.top_level_dir] if not self.top_level_dirs: for test in [t for t in args.tests if h.exists(t)]: if h.isdir(test): top_dir = test else: top_dir = h.dirname(test) while h.exists(top_dir, '__init__.py'): top_dir = h.dirname(top_dir) top_dir = h.realpath(top_dir) if not top_dir in self.top_level_dirs: self.top_level_dirs.append(top_dir) if not self.top_level_dirs: top_dir = h.getcwd() while h.exists(top_dir, '__init__.py'): top_dir = h.dirname(top_dir) top_dir = h.realpath(top_dir) self.top_level_dirs.append(top_dir) if not self.top_level_dir and self.top_level_dirs: self.top_level_dir = self.top_level_dirs[0] for path in self.top_level_dirs: h.add_to_path(path) for path in args.path: h.add_to_path(path) if args.coverage: # pragma: no cover try: import coverage except ImportError: self.print_('Error: coverage is not installed.') return 1 source = self.args.coverage_source if not source: source = self.top_level_dirs + self.args.path self.coverage_source = source self.cov = coverage.coverage(source=self.coverage_source, data_suffix=True) self.cov.erase() if args.expectations_files: ret = self.parse_expectations() if ret: return ret elif args.tags: self.print_('Error: tags require expectations files.') return 1 return 0
#!/usr/bin/env python import os import sys import coverage if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tardis.test_settings") from django.core.management import execute_from_command_line # https://github.com/django-nose/django-nose/issues/180#issuecomment-93371418 # Without this, coverage reporting starts after many modules have # already been imported, so module-level code is excluded from # coverage reports: cov = coverage.coverage(source=['tardis'], omit=['*/tests/*']) cov.set_option('report:show_missing', True) if len(sys.argv) > 1 and sys.argv[1] == "behave": cov.set_option('run:plugins', ['django_coverage_plugin']) cov.set_option('report:include', ['*.html', '*.js']) cov.erase() cov.start() if len(sys.argv) < 2: execute_from_command_line(['./test.py', 'test']) else: execute_from_command_line(sys.argv) cov.stop() cov.save() cov.report()
def initialise(self): self.start_time = time.time() self.test_results = [] self.ntests = 0 self.count = 1 self.skipped = 0 self.failures = 0 results_filename = os.getenv('COCOTB_RESULTS_FILE', "results.xml") suite_name = os.getenv('RESULT_TESTSUITE', "all") package_name = os.getenv('RESULT_TESTPACKAGE', "all") self.xunit = XUnitReporter(filename=results_filename) self.xunit.add_testsuite(name=suite_name, tests=repr(self.ntests), package=package_name) if (self._seed is not None): self.xunit.add_property(name="random_seed", value=("%d"%self._seed)) if coverage is not None: self.log.info("Enabling coverage collection of Python code") self._cov = coverage.coverage(branch=True, omit=["*cocotb*"]) self._cov.start() handle = simulator.get_root_handle(self._root_name) self._dut = cocotb.handle.SimHandle(handle) if handle else None if self._dut is None: raise AttributeError("Can not find Root Handle (%s)" % self._root_name) # Auto discovery for module_name in self._modules: try: self.log.debug("Python Path: " + ",".join(sys.path)) self.log.debug("PWD: " + os.getcwd()) module = _my_import(module_name) except Exception as E: self.log.critical("Failed to import module %s: %s", module_name, E) self.log.info("MODULE variable was \"%s\"", ".".join(self._modules)) self.log.info("Traceback: ") self.log.info(traceback.format_exc()) raise if self._functions: # Specific functions specified, don't auto discover for test in self._functions.rsplit(','): try: _test = getattr(module, test) except AttributeError: self.log.error("Requested test %s wasn't found in module %s", test, module_name) err = AttributeError("Test %s doesn't exist in %s" % (test, module_name)) _py_compat.raise_from(err, None) # discard nested traceback if not hasattr(_test, "im_test"): self.log.error("Requested %s from module %s isn't a cocotb.test decorated coroutine", test, module_name) raise ImportError("Failed to find requested test %s" % test) self._queue.append(_test(self._dut)) self.ntests += 1 break for thing in vars(module).values(): if hasattr(thing, "im_test"): try: test = thing(self._dut) skip = test.skip except Exception: skip = True self.log.warning("Failed to initialize test %s" % thing.name, exc_info=True) if skip: self.log.info("Skipping test %s" % thing.name) self.xunit.add_testcase(name=thing.name, classname=module_name, time="0.0", sim_time_ns="0.0", ratio_time="0.0") self.xunit.add_skipped() self.skipped += 1 self._store_test_result(module_name, thing.name, None, 0.0, 0.0, 0.0) else: self._queue.append(test) self.ntests += 1 self._queue.sort(key=lambda test: test.sort_name()) for valid_tests in self._queue: self.log.info("Found test %s.%s" % (valid_tests.module, valid_tests.funcname)) for module_name in self._hooks: self.log.info("Loading hook from module '"+module_name+"'") module = _my_import(module_name) for thing in vars(module).values(): if hasattr(thing, "im_hook"): try: test = thing(self._dut) except Exception: self.log.warning("Failed to initialize hook %s" % thing.name, exc_info=True) else: cocotb.scheduler.add(test)
import os if os.path.exists('/collect-coverage'): import uuid import coverage covpath = '/coverage/coverage.%s' % uuid.uuid1() cov = coverage.coverage(data_file=covpath, auto_data=True) cov._warn_no_data = False cov._warn_unimported_source = False cov.start()
#!/usr/bin/env python import os import sys import dotenv if __name__ == "__main__": dotenv.read_dotenv() os.environ.setdefault("DJANGO_SETTINGS_MODULE", "eforce.settings") from django.core.management import execute_from_command_line is_testing = 'test' in sys.argv if is_testing: import coverage cov = coverage.coverage(source=[ 'eforce_api', ], omit=['*/tests/*', '*/migrations/*']) cov.set_option('report:show_missing', True) cov.erase() cov.start() execute_from_command_line(sys.argv)
coverage = None # setup code coverage monitoring proc_cmdline = open("/proc/cmdline", "r").read() proc_cmdline = proc_cmdline.split() if ("inst.debug=1" in proc_cmdline) or ("inst.debug" in proc_cmdline): import coverage pyanaconda_dir = "pyanaconda" for sitepkg in site.getsitepackages(): possible_dir = os.path.join(sitepkg, "pyanaconda") if os.path.isdir(possible_dir): pyanaconda_dir = possible_dir break cov = coverage.coverage(data_file="/mnt/sysimage/root/anaconda.coverage", branch=True, source=["/usr/sbin/anaconda", pyanaconda_dir]) cov.start() import atexit, sys, time, signal import pid def exitHandler(rebootData, storage): # Clear the list of watched PIDs. from pyanaconda.core.process_watchers import WatchProcesses WatchProcesses.unwatch_all_processes() # stop and save coverage here b/c later the file system may be unavailable if coverage is not None: cov.stop()
def start(cls): """Start the test coverage as (and if) configured in .coveragerc.""" if cls.cov is None: cls.cov = coverage.coverage() if getattr(cls.cov.config, "config_files", None) or getattr(cls.cov.config, "config_files_read", None): # suporting 4.x and 5.x cls.cov.start()