Python coverage.py() Examples
The following are 30
code examples of coverage.py().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
coverage
, or try the search function
.
Example #1
Source File: test_html.py From coveragepy-bbmirror with Apache License 2.0 | 7 votes |
def test_tabbed(self): self.output_dir("out/tabbed") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage() cov.start() import tabbed # pragma: nested cov.stop() # pragma: nested cov.html_report(tabbed, directory="../out/tabbed") # Editors like to change things, make sure our source file still has tabs. contains("src/tabbed.py", "\tif x:\t\t\t\t\t# look nice") contains( "out/tabbed/tabbed_py.html", '> <span class="key">if</span> ' '<span class="nam">x</span><span class="op">:</span>' ' ' '<span class="com"># look nice</span>' ) doesnt_contain("out/tabbed/tabbed_py.html", "\t")
Example #2
Source File: test_html.py From coveragepy-bbmirror with Apache License 2.0 | 7 votes |
def test_html_delta_from_source_change(self): # HTML generation can create only the files that have changed. # In this case, helper1 changes because its source is different. self.create_initial_files() self.run_coverage() index1 = self.get_html_index_content() self.remove_html_files() # Now change a file and do it again self.make_file("helper1.py", """\ def func1(x): # A nice function if x % 2: print("odd") """) self.run_coverage() # Only the changed files should have been created. self.assert_exists("htmlcov/index.html") self.assert_exists("htmlcov/helper1_py.html") self.assert_doesnt_exist("htmlcov/main_file_py.html") self.assert_doesnt_exist("htmlcov/helper2_py.html") index2 = self.get_html_index_content() self.assertMultiLineEqual(index1, index2)
Example #3
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def assert_tryexecfile_output(self, out1, out2): """Assert that the output we got is a successful run of try_execfile.py. `out1` and `out2` must be the same, modulo a few slight known platform differences. """ # First, is this even credible try_execfile.py output? self.assertIn('"DATA": "xyzzy"', out1) if env.JYTHON: # pragma: only jython # Argv0 is different for Jython, remove that from the comparison. out1 = re_lines(out1, r'\s+"argv0":', match=False) out2 = re_lines(out2, r'\s+"argv0":', match=False) self.assertMultiLineEqual(out1, out2)
Example #4
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_append_data(self): self.make_b_or_c_py() out = self.run_command("coverage run b_or_c.py b") self.assertEqual(out, 'done\n') self.assert_exists(".coverage") self.assertEqual(self.number_of_data_files(), 1) out = self.run_command("coverage run --append b_or_c.py c") self.assertEqual(out, 'done\n') self.assert_exists(".coverage") self.assertEqual(self.number_of_data_files(), 1) # Read the coverage file and see that b_or_c.py has all 7 lines # executed. data = coverage.CoverageData() data.read_file(".coverage") self.assertEqual(data.line_counts()['b_or_c.py'], 7)
Example #5
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_excepthook_throw(self): if env.PYPY: self.skipTest("PyPy handles excepthook throws differently, punt for now.") self.make_file("excepthook_throw.py", """\ import sys def excepthook(*args): # Write this message to stderr so that we don't have to deal # with interleaved stdout/stderr comparisons in the assertions # in the test. sys.stderr.write('in excepthook\\n') raise RuntimeError('Error Inside') sys.excepthook = excepthook raise RuntimeError('Error Outside') """) cov_st, cov_out = self.run_command_status("coverage run excepthook_throw.py") py_st, py_out = self.run_command_status("python excepthook_throw.py") if not env.JYTHON: self.assertEqual(cov_st, py_st) self.assertEqual(cov_st, 1) self.assertIn("in excepthook", py_out) self.assertEqual(cov_out, py_out)
Example #6
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_coverage_run_dir_is_like_python_dir(self): if env.PYVERSION == (3, 5, 4, 'final', 0): # pragma: obscure self.skipTest("3.5.4 broke this: https://bugs.python.org/issue32551") with open(TRY_EXECFILE) as f: self.make_file("with_main/__main__.py", f.read()) out_cov = self.run_command("coverage run with_main") out_py = self.run_command("python with_main") # The coverage.py results are not identical to the Python results, and # I don't know why. For now, ignore those failures. If someone finds # a real problem with the discrepancies, we can work on it some more. ignored = r"__file__|__loader__|__package__" # PyPy includes the current directory in the path when running a # directory, while CPython and coverage.py do not. Exclude that from # the comparison also... if env.PYPY: ignored += "|"+re.escape(os.getcwd()) out_cov = re_lines(out_cov, ignored, match=False) out_py = re_lines(out_py, ignored, match=False) self.assert_tryexecfile_output(out_cov, out_py)
Example #7
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_excepthook_exit(self): if env.PYPY or env.JYTHON: self.skipTest("non-CPython handles excepthook exits differently, punt for now.") self.make_file("excepthook_exit.py", """\ import sys def excepthook(*args): print('in excepthook') sys.exit(0) sys.excepthook = excepthook raise RuntimeError('Error Outside') """) cov_st, cov_out = self.run_command_status("coverage run excepthook_exit.py") py_st, py_out = self.run_command_status("python excepthook_exit.py") self.assertEqual(cov_st, py_st) self.assertEqual(cov_st, 0) self.assertIn("in excepthook", py_out) self.assertEqual(cov_out, py_out)
Example #8
Source File: cmdline.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def __init__(self, action, options, defaults=None, usage=None, description=None): """Create an OptionParser for a coverage.py command. `action` is the slug to put into `options.action`. `options` is a list of Option's for the command. `defaults` is a dict of default value for options. `usage` is the usage string to display in help. `description` is the description of the command, for the help text. """ if usage: usage = "%prog " + usage super(CmdOptionParser, self).__init__( usage=usage, description=description, ) self.set_defaults(action=action, **(defaults or {})) self.add_options(options) self.cmd = action
Example #9
Source File: plugin.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def has_dynamic_source_filename(self): """Does this FileTracer have dynamic source file names? FileTracers can provide dynamically determined file names by implementing :meth:`dynamic_source_filename`. Invoking that function is expensive. To determine whether to invoke it, coverage.py uses the result of this function to know if it needs to bother invoking :meth:`dynamic_source_filename`. See :meth:`CoveragePlugin.file_tracer` for details about static and dynamic file names. Returns True if :meth:`dynamic_source_filename` should be called to get dynamic source file names. """ return False
Example #10
Source File: plugin.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def line_number_range(self, frame): """Get the range of source line numbers for a given a call frame. The call frame is examined, and the source line number in the original file is returned. The return value is a pair of numbers, the starting line number and the ending line number, both inclusive. For example, returning (5, 7) means that lines 5, 6, and 7 should be considered executed. This function might decide that the frame doesn't indicate any lines from the source file were executed. Return (-1, -1) in this case to tell coverage.py that no lines should be recorded for this frame. """ lineno = frame.f_lineno return lineno, lineno
Example #11
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_coverage_run_script_imports_doubledashsource(self): # This file imports try_execfile, which compiles it to .pyc, so the # first run will have __file__ == "try_execfile.py" and the second will # have __file__ == "try_execfile.pyc", which throws off the comparison. # Setting dont_write_bytecode True stops the compilation to .pyc and # keeps the test working. self.make_file("myscript", """\ import sys; sys.dont_write_bytecode = True import process_test.try_execfile """) # These -m commands assume the coverage tree is on the path. out_cov = self.run_command( "coverage run --source process_test myscript" ) out_py = self.run_command("python myscript") self.assert_tryexecfile_output(out_cov, out_py) st, out = self.run_command_status("coverage report") self.assertEqual(st, 0) self.assertEqual(self.line_count(out), 6, out)
Example #12
Source File: test_html.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def create_initial_files(self): """Create the source files we need to run these tests.""" self.make_file("main_file.py", """\ import helper1, helper2 helper1.func1(12) helper2.func2(12) """) self.make_file("helper1.py", """\ def func1(x): if x % 2: print("odd") """) self.make_file("helper2.py", """\ def func2(x): print("x is %d" % x) """)
Example #13
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_missing_source_file(self): # Check what happens if the source is missing when reporting happens. self.make_file("fleeting.py", """\ s = 'goodbye, cruel world!' """) self.run_command("coverage run fleeting.py") os.remove("fleeting.py") out = self.run_command("coverage html -d htmlcov") self.assertRegex(out, "No source for code: '.*fleeting.py'") self.assertNotIn("Traceback", out) # It happens that the code paths are different for *.py and other # files, so try again with no extension. self.make_file("fleeting", """\ s = 'goodbye, cruel world!' """) self.run_command("coverage run fleeting") os.remove("fleeting") status, out = self.run_command_status("coverage html -d htmlcov") self.assertRegex(out, "No source for code: '.*fleeting'") self.assertNotIn("Traceback", out) self.assertEqual(status, 1)
Example #14
Source File: test_html.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_html_delta_from_settings_change(self): # HTML generation can create only the files that have changed. # In this case, everything changes because the coverage.py settings # have changed. self.create_initial_files() self.run_coverage(covargs=dict(omit=[])) index1 = self.get_html_index_content() self.remove_html_files() self.run_coverage(covargs=dict(omit=['xyzzy*'])) # All the files have been reported again. self.assert_exists("htmlcov/index.html") self.assert_exists("htmlcov/helper1_py.html") self.assert_exists("htmlcov/main_file_py.html") self.assert_exists("htmlcov/helper2_py.html") index2 = self.get_html_index_content() self.assertMultiLineEqual(index1, index2)
Example #15
Source File: test_html.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_html_delta_from_coverage_version_change(self): # HTML generation can create only the files that have changed. # In this case, everything changes because the coverage.py version has # changed. self.create_initial_files() self.run_coverage() index1 = self.get_html_index_content() self.remove_html_files() # "Upgrade" coverage.py! coverage.__version__ = "XYZZY" self.run_coverage() # All the files have been reported again. self.assert_exists("htmlcov/index.html") self.assert_exists("htmlcov/helper1_py.html") self.assert_exists("htmlcov/main_file_py.html") self.assert_exists("htmlcov/helper2_py.html") index2 = self.get_html_index_content() fixed_index2 = index2.replace("XYZZY", self.real_coverage_version) self.assertMultiLineEqual(index1, fixed_index2)
Example #16
Source File: test_html.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_file_becomes_100(self): self.create_initial_files() self.run_coverage() # Now change a file and do it again self.make_file("main_file.py", """\ import helper1, helper2 # helper1 is now 100% helper1.func1(12) helper1.func1(23) """) self.run_coverage(htmlargs=dict(skip_covered=True)) # The 100% file, skipped, shouldn't be here. self.assert_doesnt_exist("htmlcov/helper1_py.html")
Example #17
Source File: test_html.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_dotpy_not_python_ignored(self): self.make_file("main.py", "import innocuous") self.make_file("innocuous.py", "a = 2") cov = coverage.Coverage() self.start_import_stop(cov, "main") self.make_file("innocuous.py", "<h1>This isn't python!</h1>") cov.html_report(ignore_errors=True) self.assertEqual( len(cov._warnings), 1, "Expected a warning to be thrown when an invalid python file is parsed") self.assertIn( "Could not parse Python file", cov._warnings[0], "Warning message should be in 'invalid file' warning" ) self.assertIn( "innocuous.py", cov._warnings[0], "Filename should be in 'invalid file' warning" ) self.assert_exists("htmlcov/index.html") # This would be better as a glob, if the HTML layout changes: self.assert_doesnt_exist("htmlcov/innocuous.html")
Example #18
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_lang_c(self): if env.JYTHON: # Jython as of 2.7.1rc3 won't compile a filename that isn't utf8. self.skipTest("Jython can't handle this test") # LANG=C forces getfilesystemencoding on Linux to 'ascii', which causes # failures with non-ascii file names. We don't want to make a real file # with strange characters, though, because that gets the test runners # tangled up. This will isolate the concerns to the coverage.py code. # https://bitbucket.org/ned/coveragepy/issues/533/exception-on-unencodable-file-name self.make_file("weird_file.py", r""" globs = {} code = "a = 1\nb = 2\n" exec(compile(code, "wut\xe9\xea\xeb\xec\x01\x02.py", 'exec'), globs) print(globs['a']) print(globs['b']) """) self.set_environ("LANG", "C") out = self.run_command("coverage run weird_file.py") self.assertEqual(out, "1\n2\n")
Example #19
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_code_exits(self): self.make_file("exit.py", """\ import sys def f1(): print("about to exit..") sys.exit(17) def f2(): f1() f2() """) # The important thing is for "coverage run" and "python" to have the # same output. No traceback. status, out = self.run_command_status("coverage run exit.py") status2, out2 = self.run_command_status("python exit.py") self.assertMultiLineEqual(out, out2) self.assertMultiLineEqual(out, "about to exit..\n") self.assertEqual(status, status2) self.assertEqual(status, 17)
Example #20
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_warn_preimported(self): self.make_file("hello.py", """\ import goodbye import coverage cov = coverage.Coverage(include=["good*"], check_preimported=True) cov.start() print(goodbye.f()) cov.stop() """) self.make_file("goodbye.py", """\ def f(): return "Goodbye!" """) goodbye_path = os.path.abspath("goodbye.py") out = self.run_command("python hello.py") self.assertIn("Goodbye!", out) msg = ( "Coverage.py warning: " "Already imported a file that will be measured: {0} " "(already-imported)").format(goodbye_path) self.assertIn(msg, out)
Example #21
Source File: test_html.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_report_skip_covered_branches(self): self.make_file("main_file.py", """ import not_covered def normal(): print("z") normal() """) self.make_file("not_covered.py", """ def not_covered(): print("n") """) self.run_coverage(covargs=dict(branch=True), htmlargs=dict(skip_covered=True)) self.assert_exists("htmlcov/index.html") self.assert_doesnt_exist("htmlcov/main_file_py.html") self.assert_exists("htmlcov/not_covered_py.html")
Example #22
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_warnings_during_reporting(self): # While fixing issue #224, the warnings were being printed far too # often. Make sure they're not any more. self.make_file("hello.py", """\ import sys, os, the_other print("Hello") """) self.make_file("the_other.py", """\ print("What?") """) self.make_file(".coveragerc", """\ [run] source = . xyzzy """) self.run_command("coverage run hello.py") out = self.run_command("coverage html") self.assertEqual(out.count("Module xyzzy was never imported."), 0)
Example #23
Source File: test_html.py From coveragepy-bbmirror with Apache License 2.0 | 6 votes |
def test_report_skip_covered_no_branches(self): self.make_file("main_file.py", """ import not_covered def normal(): print("z") normal() """) self.make_file("not_covered.py", """ def not_covered(): print("n") """) self.run_coverage(htmlargs=dict(skip_covered=True)) self.assert_exists("htmlcov/index.html") self.assert_doesnt_exist("htmlcov/main_file_py.html") self.assert_exists("htmlcov/not_covered_py.html")
Example #24
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_combine_parallel_data(self): self.make_b_or_c_py() out = self.run_command("coverage run -p b_or_c.py b") self.assertEqual(out, 'done\n') self.assert_doesnt_exist(".coverage") self.assertEqual(self.number_of_data_files(), 1) out = self.run_command("coverage run -p b_or_c.py c") self.assertEqual(out, 'done\n') self.assert_doesnt_exist(".coverage") # After two -p runs, there should be two .coverage.machine.123 files. self.assertEqual(self.number_of_data_files(), 2) # Combine the parallel coverage data files into .coverage . self.run_command("coverage combine") self.assert_exists(".coverage") # After combining, there should be only the .coverage file. self.assertEqual(self.number_of_data_files(), 1) # Read the coverage file and see that b_or_c.py has all 7 lines # executed. data = coverage.CoverageData() data.read_file(".coverage") self.assertEqual(data.line_counts()['b_or_c.py'], 7) # Running combine again should fail, because there are no parallel data # files to combine. status, out = self.run_command_status("coverage combine") self.assertEqual(status, 1) self.assertEqual(out, "No data to combine\n") # And the originally combined data is still there. data = coverage.CoverageData() data.read_file(".coverage") self.assertEqual(data.line_counts()['b_or_c.py'], 7)
Example #25
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_combine_with_rc(self): self.make_b_or_c_py() self.make_file(".coveragerc", """\ [run] parallel = true """) out = self.run_command("coverage run b_or_c.py b") self.assertEqual(out, 'done\n') self.assert_doesnt_exist(".coverage") out = self.run_command("coverage run b_or_c.py c") self.assertEqual(out, 'done\n') self.assert_doesnt_exist(".coverage") # After two runs, there should be two .coverage.machine.123 files. self.assertEqual(self.number_of_data_files(), 2) # Combine the parallel coverage data files into .coverage . self.run_command("coverage combine") self.assert_exists(".coverage") self.assert_exists(".coveragerc") # After combining, there should be only the .coverage file. self.assertEqual(self.number_of_data_files(), 1) # Read the coverage file and see that b_or_c.py has all 7 lines # executed. data = coverage.CoverageData() data.read_file(".coverage") self.assertEqual(data.line_counts()['b_or_c.py'], 7) # Reporting should still work even with the .rc file out = self.run_command("coverage report") self.assertMultiLineEqual(out, textwrap.dedent("""\ Name Stmts Miss Cover ------------------------------- b_or_c.py 7 0 100% """))
Example #26
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_combine_no_usable_files(self): # https://bitbucket.org/ned/coveragepy/issues/629/multiple-use-of-combine-leads-to-empty self.make_b_or_c_py() out = self.run_command("coverage run b_or_c.py b") self.assertEqual(out, 'done\n') self.assert_exists(".coverage") self.assertEqual(self.number_of_data_files(), 1) # Make bogus data files. self.make_file(".coverage.bad1", "This isn't a coverage data file.") self.make_file(".coverage.bad2", "This isn't a coverage data file.") # Combine the parallel coverage data files into .coverage, but nothing is readable. status, out = self.run_command_status("coverage combine") self.assertEqual(status, 1) for n in "12": self.assert_exists(".coverage.bad{0}".format(n)) warning_regex = ( r"Coverage.py warning: Couldn't read data from '.*\.coverage\.bad{0}': " r"CoverageException: Doesn't seem to be a coverage\.py data file".format(n) ) self.assertRegex(out, warning_regex) self.assertRegex(out, r"No usable data files") # After combining, we should have a main file and two parallel files. self.assertEqual(self.number_of_data_files(), 3) # Read the coverage file and see that b_or_c.py has 6 lines # executed (we only did b, not c). data = coverage.CoverageData() data.read_file(".coverage") self.assertEqual(data.line_counts()['b_or_c.py'], 6)
Example #27
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_code_exits_no_arg(self): self.make_file("exit_none.py", """\ import sys def f1(): print("about to exit quietly..") sys.exit() f1() """) status, out = self.run_command_status("coverage run exit_none.py") status2, out2 = self.run_command_status("python exit_none.py") self.assertMultiLineEqual(out, out2) self.assertMultiLineEqual(out, "about to exit quietly..\n") self.assertEqual(status, status2) self.assertEqual(status, 0)
Example #28
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_combine_parallel_data_with_a_corrupt_file(self): self.make_b_or_c_py() out = self.run_command("coverage run -p b_or_c.py b") self.assertEqual(out, 'done\n') self.assert_doesnt_exist(".coverage") self.assertEqual(self.number_of_data_files(), 1) out = self.run_command("coverage run -p b_or_c.py c") self.assertEqual(out, 'done\n') self.assert_doesnt_exist(".coverage") # After two -p runs, there should be two .coverage.machine.123 files. self.assertEqual(self.number_of_data_files(), 2) # Make a bogus data file. self.make_file(".coverage.bad", "This isn't a coverage data file.") # Combine the parallel coverage data files into .coverage . out = self.run_command("coverage combine") self.assert_exists(".coverage") self.assert_exists(".coverage.bad") warning_regex = ( r"Coverage.py warning: Couldn't read data from '.*\.coverage\.bad': " r"CoverageException: Doesn't seem to be a coverage\.py data file" ) self.assertRegex(out, warning_regex) # After combining, those two should be the only data files. self.assertEqual(self.number_of_data_files(), 2) # Read the coverage file and see that b_or_c.py has all 7 lines # executed. data = coverage.CoverageData() data.read_file(".coverage") self.assertEqual(data.line_counts()['b_or_c.py'], 7)
Example #29
Source File: test_html.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_omit_3(self): self.output_dir("out/omit_3") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage(include=["./*"]) cov.start() import main # pragma: nested # pylint: disable=unused-variable cov.stop() # pragma: nested cov.html_report(directory="../out/omit_3", omit=["m1.py", "m2.py"]) compare("gold_omit_3", "out/omit_3", size_within=10, file_pattern="*.html")
Example #30
Source File: test_process.py From coveragepy-bbmirror with Apache License 2.0 | 5 votes |
def test_running_missing_file(self): status, out = self.run_command_status("coverage run xyzzy.py") self.assertRegex(out, "No file to run: .*xyzzy.py") self.assertNotIn("raceback", out) self.assertNotIn("rror", out) self.assertEqual(status, 1)