def test_bom(self): self.make_file("bom.py", bytes=b"""\ \xef\xbb\xbf# A Python source file in utf-8, with BOM. math = "3\xc3\x974 = 12, \xc3\xb72 = 6\xc2\xb10" import sys if sys.version_info >= (3, 0): assert len(math) == 18 assert len(math.encode('utf-8')) == 21 else: assert len(math) == 21 assert len(math.decode('utf-8')) == 18 """.replace(b"\n", b"\r\n")) # It's important that the source file really have a BOM, which can # get lost, so check that it's really there, and that we have \r\n # line endings. with open("bom.py", "rb") as f: data = f.read() assert data[:3] == b"\xef\xbb\xbf" assert data.count(b"\r\n") == 11 cov = coverage.Coverage() bom = self.start_import_stop(cov, "bom") cov.html_report(bom, directory="out") compare_html(gold_path("html/bom"), "out") contains( "out/bom_py.html", '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', )
def test_tabbed(self): # The file contents would look like this with 8-space tabs: # x = 1 # if x: # a = "tabbed" # aligned comments # if x: # look nice # b = "no spaces" # when they # c = "done" # line up. self.make_file("tabbed.py", """\ x = 1 if x: \ta = "Tabbed"\t\t\t\t# Aligned comments \tif x:\t\t\t\t\t# look nice \t\tb = "No spaces"\t\t\t# when they \tc = "Done"\t\t\t\t# line up. """) cov = coverage.Coverage() tabbed = self.start_import_stop(cov, "tabbed") cov.html_report(tabbed, directory="out") # Editors like to change things, make sure our source file still has tabs. contains("tabbed.py", "\tif x:\t\t\t\t\t# look nice") contains( "out/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_py.html", "\t")
def test_styled(self): self.make_file("a.py", """\ if 1 < 2: # Needed a < to look at HTML entities. a = 3 else: a = 4 """) self.make_file("extra.css", "/* Doesn't matter what goes in here, it gets copied. */") cov = coverage.Coverage() cov.start() import a # pragma: nested # pylint: disable=import-error cov.stop() # pragma: nested cov.html_report(a, directory="out", extra_css="extra.css") compare_html("out", gold_path("html/gold_styled")) compare("out", gold_path("html/gold_styled"), size_within=10, file_pattern="*.css") contains( "out/a_py.html", '<link rel="stylesheet" href="extra.css" type="text/css">', ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">67%</span>' ) contains( "out/index.html", '<link rel="stylesheet" href="extra.css" type="text/css">', '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>' )
def test_a(self): self.make_file("a.py", """\ if 1 < 2: # Needed a < to look at HTML entities. a = 3 else: a = 4 """) cov = coverage.Coverage() a = self.start_import_stop(cov, "a") cov.html_report(a, directory='out/a') compare_html(gold_path("html/a"), "out/a") contains( "out/a/a_py.html", ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> ' '<span class="op">=</span> <span class="num">3</span>'), '<span class="pc_cov">67%</span>', ) contains( "out/a/index.html", '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>', '<td class="right" data-ratio="2 3">67%</td>', )
def test_styled(self): self.make_file("a.py", """\ if 1 < 2: # Needed a < to look at HTML entities. a = 3 else: a = 4 """) self.make_file("extra.css", "/* Doesn't matter what goes in here, it gets copied. */\n") cov = coverage.Coverage() a = self.start_import_stop(cov, "a") cov.html_report(a, directory="out", extra_css="extra.css") compare_html(gold_path("html/styled"), "out") compare(gold_path("html/styled"), "out", file_pattern="*.css") contains( "out/a_py.html", '<link rel="stylesheet" href="extra.css" type="text/css">', ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">67%</span>' ) contains( "out/index.html", '<link rel="stylesheet" href="extra.css" type="text/css">', '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>' )
def test_styled(self): self.make_file("a.py", """\ if 1 < 2: # Needed a < to look at HTML entities. a = 3 else: a = 4 """) self.make_file("extra.css", "/* Doesn't matter what goes in here, it gets copied. */\n") cov = coverage.Coverage() a = self.start_import_stop(cov, "a") cov.html_report(a, directory="out/styled", extra_css="extra.css") compare_html(gold_path("html/styled"), "out/styled") compare(gold_path("html/styled"), "out/styled", file_pattern="*.css") contains( "out/styled/a_py.html", '<link rel="stylesheet" href="extra.css" type="text/css">', ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">67%</span>' ) contains( "out/styled/index.html", '<link rel="stylesheet" href="extra.css" type="text/css">', '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>' )
def test_unicode(self): self.make_file("unicode.py", """\ # -*- coding: utf-8 -*- # A Python source file with exotic characters. upside_down = "ʎd˙ǝbɐɹǝʌoɔ" surrogate = "db40,dd00: x󠄀" """) # pylint: disable=import-error, redefined-builtin cov = coverage.Coverage() cov.start() import unicode # pragma: nested cov.stop() # pragma: nested cov.html_report(unicode, directory="out") compare_html("out", gold_path("html/gold_unicode")) contains( "out/unicode_py.html", '<span class="str">"ʎd˙ǝbɐɹǝʌoɔ"</span>', ) contains_any( "out/unicode_py.html", '<span class="str">"db40,dd00: x��"</span>', '<span class="str">"db40,dd00: x󠄀"</span>', )
def test_styled(self): self.output_dir("out/styled") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage() cov.start() import a # pragma: nested cov.stop() # pragma: nested cov.html_report(a, directory="../out/styled", extra_css="extra.css") compare_html("gold_styled", "out/styled") compare("gold_styled", "out/styled", size_within=10, file_pattern="*.css") contains("out/styled/a_py.html", '<link rel="stylesheet" href="extra.css" type="text/css">', ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">67%</span>') contains("out/styled/index.html", '<link rel="stylesheet" href="extra.css" type="text/css">', '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>')
def test_other(self): self.make_file("src/here.py", """\ import other if 1 < 2: h = 3 else: h = 4 """) self.make_file("othersrc/other.py", """\ # A file in another directory. We're checking that it ends up in the # HTML report. print("This is the other src!") """) with change_dir("src"): sys.path.insert(0, "") # pytest sometimes has this, sometimes not!? sys.path.insert(0, "../othersrc") cov = coverage.Coverage(include=["./*", "../othersrc/*"]) self.start_import_stop(cov, "here") cov.html_report(directory="../out/other") # Different platforms will name the "other" file differently. Rename it for p in glob.glob("out/other/*_other_py.html"): os.rename(p, "out/other/blah_blah_other_py.html") compare_html(gold_path("html/other"), "out/other") contains( "out/other/index.html", '<a href="here_py.html">here.py</a>', 'other_py.html">', 'other.py</a>', )
def test_b_branch(self): self.output_dir("out/b_branch") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage(branch=True) cov.start() import b # pragma: nested cov.stop() # pragma: nested cov.html_report(b, directory="../out/b_branch") compare("gold_b_branch", "out/b_branch", size_within=10, file_pattern="*.html") contains( "out/b_branch/b_py.html", ('<span class="key">if</span> <span class="nam">x</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">70%</span>', ('<span class="annotate short">8 ↛ 11</span>' '<span class="annotate long">line 8 didn\'t jump to line 11, because the condition on line 8 was never false</span>'), ('<span class="annotate short">17 ↛ exit</span>' '<span class="annotate long">line 17 didn\'t return from function \'two\', because the condition on line 17 was never false</span>'), ('<span class="annotate short">25 ↛ 26, ' '25 ↛ 28</span>' '<span class="annotate long">2 missed branches: 1) line 25 didn\'t jump to line 26, because the condition on line 25 was never true, 2) line 25 didn\'t jump to line 28, because the condition on line 25 was never false</span>'), ) contains( "out/b_branch/index.html", '<a href="b_py.html">b.py</a>', '<span class="pc_cov">70%</span>', '<td class="right" data-ratio="16 23">70%</td>', )
def test_a(self): self.output_dir("out/a") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage() cov.start() import a # pragma: nested cov.stop() # pragma: nested cov.html_report(a, directory='../out/a') compare_html("gold_a", "out/a") contains( "out/a/a_py.html", ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> ' '<span class="op">=</span> <span class="num">3</span>'), '<span class="pc_cov">67%</span>', ) contains( "out/a/index.html", '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>', '<td class="right" data-ratio="2 3">67%</td>', )
def test_a(self): self.make_file("a.py", """\ if 1 < 2: # Needed a < to look at HTML entities. a = 3 else: a = 4 """) cov = coverage.Coverage() a = self.start_import_stop(cov, "a") cov.html_report(a, directory='out') compare_html(gold_path("html/a"), "out") contains( "out/a_py.html", ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> ' '<span class="op">=</span> <span class="num">3</span>'), '<span class="pc_cov">67%</span>', ) contains( "out/index.html", '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>', '<td class="right" data-ratio="2 3">67%</td>', )
def test_b_branch(self): self.output_dir("out/b_branch") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage(branch=True) cov.start() import b # pragma: nested cov.stop() # pragma: nested cov.html_report(b, directory="../out/b_branch") compare("gold_b_branch", "out/b_branch", size_within=10, file_pattern="*.html") contains( "out/b_branch/b_py.html", ('<span class="key">if</span> <span class="nam">x</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">70%</span>', ('<span class="annotate" title="Line 8 was executed, but never jumped to line 11">' '8 ↛ 11 [?]</span>'), ('<span class="annotate" title="Line 17 was executed, but never jumped ' 'to the function exit">17 ↛ exit [?]</span>'), ('<span class="annotate" title="Line 25 was executed, but never jumped ' 'to line 26 or line 28">25 ↛ 26, ' '25 ↛ 28 [?]</span>'), ) contains( "out/b_branch/index.html", '<a href="b_py.html">b.py</a>', '<span class="pc_cov">70%</span>', '<td class="right" data-ratio="16 23">70%</td>', )
def test_other(self): self.make_file("src/here.py", """\ import other if 1 < 2: h = 3 else: h = 4 """) self.make_file("othersrc/other.py", """\ # A file in another directory. We're checking that it ends up in the # HTML report. print("This is the other src!") """) with change_dir("src"): sys.path.insert(0, "") # pytest sometimes has this, sometimes not!? sys.path.insert(0, "../othersrc") cov = coverage.Coverage(include=["./*", "../othersrc/*"]) self.start_import_stop(cov, "here") cov.html_report(directory="../out") # Different platforms will name the "other" file differently. Rename it for p in glob.glob("out/*_other_py.html"): os.rename(p, "out/blah_blah_other_py.html") compare_html(gold_path("html/other"), "out") contains( "out/index.html", '<a href="here_py.html">here.py</a>', 'other_py.html">', 'other.py</a>', )
def test_bom(self): self.make_file("bom.py", bytes=b"""\ \xef\xbb\xbf# A Python source file in utf-8, with BOM. math = "3\xc3\x974 = 12, \xc3\xb72 = 6\xc2\xb10" import sys if sys.version_info >= (3, 0): assert len(math) == 18 assert len(math.encode('utf-8')) == 21 else: assert len(math) == 21 assert len(math.decode('utf-8')) == 18 """.replace(b"\n", b"\r\n")) # It's important that the source file really have a BOM, which can # get lost, so check that it's really there, and that we have \r\n # line endings. with open("bom.py", "rb") as f: data = f.read() assert data[:3] == b"\xef\xbb\xbf" assert data.count(b"\r\n") == 11 cov = coverage.Coverage() bom = self.start_import_stop(cov, "bom") cov.html_report(bom, directory="out/bom") compare_html(gold_path("html/bom"), "out/bom") contains( "out/bom/bom_py.html", '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', )
def test_a(self): self.make_file("a.py", """\ if 1 < 2: # Needed a < to look at HTML entities. a = 3 else: a = 4 """) cov = coverage.Coverage() cov.start() import a # pragma: nested # pylint: disable=import-error cov.stop() # pragma: nested cov.html_report(a, directory='out') compare_html("out", gold_path("html/gold_a")) contains( "out/a_py.html", ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> ' '<span class="op">=</span> <span class="num">3</span>'), '<span class="pc_cov">67%</span>', ) contains( "out/index.html", '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>', '<td class="right" data-ratio="2 3">67%</td>', )
def test_styled(self): self.output_dir("out/styled") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage() cov.start() import a # pragma: nested cov.stop() # pragma: nested cov.html_report(a, directory="../out/styled", extra_css="extra.css") compare("gold_styled", "out/styled", size_within=10, file_pattern="*.html") compare("gold_styled", "out/styled", size_within=10, file_pattern="*.css") contains( "out/styled/a_py.html", '<link rel="stylesheet" href="extra.css" type="text/css">', ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">67%</span>' ) contains( "out/styled/index.html", '<link rel="stylesheet" href="extra.css" type="text/css">', '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>' )
def test_a(self): self.output_dir("out/a") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage() cov.start() import a # pragma: nested cov.stop() # pragma: nested cov.html_report(a, directory='../out/a') compare("gold_a", "out/a", size_within=10, file_pattern="*.html") contains( "out/a/a_py.html", ('<span class="key">if</span> <span class="num">1</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> ' '<span class="op">=</span> <span class="num">3</span>'), '<span class="pc_cov">67%</span>', ) contains( "out/a/index.html", '<a href="a_py.html">a.py</a>', '<span class="pc_cov">67%</span>', '<td class="right" data-ratio="2 3">67%</td>', )
def test_unicode(self): self.output_dir("out/unicode") with change_dir("src"): # pylint: disable=import-error, redefined-builtin cov = coverage.Coverage() cov.start() import unicode # pragma: nested cov.stop() # pragma: nested cov.html_report(unicode, directory="../out/unicode") compare("gold_unicode", "out/unicode", size_within=10, file_pattern="*.html") contains( "out/unicode/unicode_py.html", '<span class="str">"ʎd˙ǝbɐɹǝʌoɔ"</span>', ) contains_any( "out/unicode/unicode_py.html", '<span class="str">"db40,dd00: x��"</span>', '<span class="str">"db40,dd00: x󠄀"</span>', )
def test_other(self): self.output_dir("out/other") with change_dir("src"): # pylint: disable=import-error, unused-variable sys.path.insert(0, "../othersrc") cov = coverage.Coverage(include=["./*", "../othersrc/*"]) cov.start() import here # pragma: nested cov.stop() # pragma: nested cov.html_report(directory="../out/other") # Different platforms will name the "other" file differently. Rename it for p in glob.glob("out/other/*_other_py.html"): os.rename(p, "out/other/blah_blah_other_py.html") compare("gold_other", "out/other", size_within=10, file_pattern="*.html") contains( "out/other/index.html", '<a href="here_py.html">here.py</a>', 'other_py.html">', 'other.py</a>', )
def test_unicode(self): surrogate = u"\U000e0100" if env.PY2: surrogate = surrogate.encode('utf-8') self.make_file("unicode.py", """\ # -*- coding: utf-8 -*- # A Python source file with exotic characters. upside_down = "ʎd˙ǝbɐɹǝʌoɔ" surrogate = "db40,dd00: x@" """.replace("@", surrogate)) cov = coverage.Coverage() unimod = self.start_import_stop(cov, "unicode") cov.html_report(unimod, directory="out/unicode") compare_html(gold_path("html/unicode"), "out/unicode") contains( "out/unicode/unicode_py.html", '<span class="str">"ʎd˙ǝbɐɹǝʌoɔ"</span>', ) contains_any( "out/unicode/unicode_py.html", '<span class="str">"db40,dd00: x��"</span>', '<span class="str">"db40,dd00: x󠄀"</span>', )
def test_partial(self): self.make_file("partial.py", """\ # partial branches and excluded lines a = 2 while "no peephole".upper(): # t4 break while a: # pragma: no branch break if 0: never_happen() if 13: a = 14 if a == 16: raise ZeroDivisionError("17") """) self.make_file("partial.ini", """\ [run] branch = True [report] exclude_lines = raise ZeroDivisionError """) cov = coverage.Coverage(config_file="partial.ini") partial = self.start_import_stop(cov, "partial") cov.html_report(partial, directory="out/partial") compare_html(gold_path("html/partial"), "out/partial") contains( "out/partial/partial_py.html", '<p id="t4" class="par run show_par">', '<p id="t7" class="run">', # The "if 0" and "if 1" statements are optimized away. '<p id="t10" class="pln">', # The "raise ZeroDivisionError" is excluded by regex in the .ini. '<p id="t17" class="exc show_exc">', ) contains( "out/partial/index.html", '<a href="partial_py.html">partial.py</a>', '<span class="pc_cov">91%</span>' )
def test_isolatin1(self): self.output_dir("out/isolatin1") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage() cov.start() import isolatin1 # pragma: nested cov.stop() # pragma: nested cov.html_report(isolatin1, directory="../out/isolatin1") compare("gold_isolatin1", "out/isolatin1", size_within=10, file_pattern="*.html") contains( "out/isolatin1/isolatin1_py.html", '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', )
def test_isolatin1(self): self.output_dir("out/isolatin1") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage() cov.start() import isolatin1 # pragma: nested cov.stop() # pragma: nested cov.html_report(isolatin1, directory="../out/isolatin1") compare_html("gold_isolatin1", "out/isolatin1") contains( "out/isolatin1/isolatin1_py.html", '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', )
def test_bom(self): self.output_dir("out/bom") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage() cov.start() import bom # pragma: nested cov.stop() # pragma: nested cov.html_report(bom, directory="../out/bom") compare("gold_bom", "out/bom", size_within=10, file_pattern="*.html") contains( "out/bom/bom_py.html", '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', )
def test_bom(self): self.output_dir("out/bom") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage() cov.start() import bom # pragma: nested cov.stop() # pragma: nested cov.html_report(bom, directory="../out/bom") compare("gold_bom", "out/bom", size_within=10, file_pattern="*.html") contains( "out/bom/bom_py.html", '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', )
def test_isolatin1(self): self.make_file("isolatin1.py", bytes=b"""\ # -*- coding: iso8859-1 -*- # A Python source file in another encoding. math = "3\xd74 = 12, \xf72 = 6\xb10" assert len(math) == 18 """) cov = coverage.Coverage() isolatin1 = self.start_import_stop(cov, "isolatin1") cov.html_report(isolatin1, directory="out") compare_html(gold_path("html/isolatin1"), "out") contains( "out/isolatin1_py.html", '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', )
def test_isolatin1(self): self.make_file("isolatin1.py", bytes=b"""\ # -*- coding: iso8859-1 -*- # A Python source file in another encoding. math = "3\xd74 = 12, \xf72 = 6\xb10" assert len(math) == 18 """) cov = coverage.Coverage() isolatin1 = self.start_import_stop(cov, "isolatin1") cov.html_report(isolatin1, directory="out/isolatin1") compare_html(gold_path("html/isolatin1"), "out/isolatin1") contains( "out/isolatin1/isolatin1_py.html", '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', )
def test_b_branch(self): self.output_dir("out/b_branch") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage(branch=True) cov.start() import b # pragma: nested cov.stop() # pragma: nested cov.html_report(b, directory="../out/b_branch") compare("gold_b_branch", "out/b_branch", size_within=10, file_pattern="*.html") contains( "out/b_branch/b_py.html", ('<span class="key">if</span> <span class="nam">x</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">70%</span>', ('<span class="annotate short">8 ↛ 11</span>' '<span class="annotate long">line 8 didn\'t jump to line 11, ' 'because the condition on line 8 was never false</span>'), ('<span class="annotate short">17 ↛ exit</span>' '<span class="annotate long">line 17 didn\'t return from function \'two\', ' 'because the condition on line 17 was never false</span>'), ('<span class="annotate short">25 ↛ 26, ' '25 ↛ 28</span>' '<span class="annotate long">2 missed branches: ' '1) line 25 didn\'t jump to line 26, ' 'because the condition on line 25 was never true, ' '2) line 25 didn\'t jump to line 28, ' 'because the condition on line 25 was never false</span>'), ) contains( "out/b_branch/index.html", '<a href="b_py.html">b.py</a>', '<span class="pc_cov">70%</span>', '<td class="right" data-ratio="16 23">70%</td>', )
def test_other(self): self.make_file( "src/here.py", """\ import other if 1 < 2: h = 3 else: h = 4 """) self.make_file( "othersrc/other.py", """\ # A file in another directory. We're checking that it ends up in the # HTML report. print("This is the other src!") """) with change_dir("src"): sys.path.insert(0, "../othersrc") cov = coverage.Coverage(include=["./*", "../othersrc/*"]) self.start_import_stop(cov, "here") cov.html_report(directory="../out/other") # Different platforms will name the "other" file differently. Rename it actual_file = list(glob.glob("out/other/*_other_py.html")) assert len(actual_file) == 1 os.rename(actual_file[0], "out/other/blah_blah_other_py.html") compare_html( gold_path("html/other"), "out/other", extra_scrubs=[ (r'href="d_[0-9a-z]{16}_', 'href="_TEST_TMPDIR_othersrc_'), ], ) contains( 'out/other/index.html', '<a href="here_py.html">here.py</a>', 'other_py.html">', 'other.py</a>', )
def test_isolatin1(self): self.make_file("isolatin1.py", bytes=b"""\ # -*- coding: iso8859-1 -*- # A Python source file in another encoding. math = "3\xd74 = 12, \xf72 = 6\xb10" assert len(math) == 18 """) cov = coverage.Coverage() cov.start() import isolatin1 # pragma: nested # pylint: disable=import-error cov.stop() # pragma: nested cov.html_report(isolatin1, directory="out") compare_html("out", gold_path("html/gold_isolatin1")) contains( "out/isolatin1_py.html", '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', )
def test_partial(self): self.output_dir("out/partial") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage(branch=True) cov.start() import partial # pragma: nested cov.stop() # pragma: nested cov.html_report(partial, directory="../out/partial") compare("gold_partial", "out/partial", size_within=10, file_pattern="*.html") contains( "out/partial/partial_py.html", '<p id="t8" class="stm run hide_run">', '<p id="t11" class="stm run hide_run">', '<p id="t14" class="stm run hide_run">', # The "if 0" and "if 1" statements are optimized away. '<p id="t17" class="pln">', ) contains( "out/partial/index.html", '<a href="partial_py.html">partial.py</a>', ) contains( "out/partial/index.html", '<span class="pc_cov">100%</span>' )
def test_partial(self): self.output_dir("out/partial") with change_dir("src"): # pylint: disable=import-error cov = coverage.Coverage(config_file="partial.ini") cov.start() import partial # pragma: nested cov.stop() # pragma: nested cov.html_report(partial, directory="../out/partial") compare_html("gold_partial", "out/partial") contains( "out/partial/partial_py.html", '<p id="t8" class="stm run hide_run">', '<p id="t11" class="stm run hide_run">', '<p id="t14" class="stm run hide_run">', # The "if 0" and "if 1" statements are optimized away. '<p id="t17" class="pln">', # The "raise AssertionError" is excluded by regex in the .ini. '<p id="t24" class="exc">', ) contains( "out/partial/index.html", '<a href="partial_py.html">partial.py</a>', ) contains("out/partial/index.html", '<span class="pc_cov">100%</span>')
def test_other(self): self.output_dir("out/other") with change_dir("src"): # pylint: disable=import-error sys.path.insert(0, "../othersrc") cov = coverage.Coverage(include=["./*", "../othersrc/*"]) cov.start() import here # pragma: nested cov.stop() # pragma: nested cov.html_report(directory="../out/other") # Different platforms will name the "other" file differently. Rename it for p in glob.glob("out/other/*_other_py.html"): os.rename(p, "out/other/blah_blah_other_py.html") compare("gold_other", "out/other", size_within=10, file_pattern="*.html") contains( "out/other/index.html", '<a href="here_py.html">here.py</a>', 'other_py.html">', 'other.py</a>', )
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")
def test_bom(self): self.output_dir("out/bom") with change_dir("src"): # It's important that the source file really have a BOM, which can # get lost, so check that it's really there. with open("bom.py", "rb") as f: first_three = f.read(3) assert first_three == b"\xef\xbb\xbf" # pylint: disable=import-error cov = coverage.Coverage() cov.start() import bom # pragma: nested cov.stop() # pragma: nested cov.html_report(bom, directory="../out/bom") compare_html("gold_bom", "out/bom") contains( "out/bom/bom_py.html", '<span class="str">"3×4 = 12, ÷2 = 6±0"</span>', )
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")
def test_partial(self): self.make_file("partial.py", """\ # partial branches and excluded lines a = 6 while True: break while 1: break while a: # pragma: no branch break if 0: never_happen() if 1: a = 21 if a == 23: raise AssertionError("Can't") """) self.make_file("partial.ini", """\ [run] branch = True [report] exclude_lines = raise AssertionError """) cov = coverage.Coverage(config_file="partial.ini") cov.start() import partial # pragma: nested # pylint: disable=import-error cov.stop() # pragma: nested cov.html_report(partial, directory="out") compare_html("out", gold_path("html/gold_partial")) contains( "out/partial_py.html", '<p id="t4" class="stm run hide_run">', '<p id="t7" class="stm run hide_run">', '<p id="t10" class="stm run hide_run">', # The "if 0" and "if 1" statements are optimized away. '<p id="t13" class="pln">', # The "raise AssertionError" is excluded by regex in the .ini. '<p id="t20" class="exc">', ) contains( "out/index.html", '<a href="partial_py.html">partial.py</a>', ) contains( "out/index.html", '<span class="pc_cov">100%</span>' )
def test_unicode(self): self.make_file("unicode.py", """\ # -*- coding: utf-8 -*- # A Python source file with exotic characters. upside_down = "ʎd˙ǝbɐɹǝʌoɔ" surrogate = "db40,dd00: x󠄀" """) cov = coverage.Coverage() unimod = self.start_import_stop(cov, "unicode") cov.html_report(unimod, directory="out") compare_html(gold_path("html/unicode"), "out") contains( "out/unicode_py.html", '<span class="str">"ʎd˙ǝbɐɹǝʌoɔ"</span>', ) contains_any( "out/unicode_py.html", '<span class="str">"db40,dd00: x��"</span>', '<span class="str">"db40,dd00: x󠄀"</span>', )
def test_partial(self): self.make_file("partial.py", """\ # partial branches and excluded lines a = 6 while "no peephole".upper(): # t4 break while a: # pragma: no branch break if 0: never_happen() if 1: a = 21 if a == 23: raise AssertionError("Can't") """) self.make_file("partial.ini", """\ [run] branch = True [report] exclude_lines = raise AssertionError """) cov = coverage.Coverage(config_file="partial.ini") partial = self.start_import_stop(cov, "partial") cov.html_report(partial, directory="out") compare_html(gold_path("html/partial"), "out") contains( "out/partial_py.html", '<p id="t4" class="stm par run hide_run">', '<p id="t7" class="stm run hide_run">', # The "if 0" and "if 1" statements are optimized away. '<p id="t10" class="pln">', # The "raise AssertionError" is excluded by regex in the .ini. '<p id="t17" class="exc">', ) contains( "out/index.html", '<a href="partial_py.html">partial.py</a>', ) contains( "out/index.html", '<span class="pc_cov">91%</span>' )
def test_contains(self): contains(GOLD_GETTY_FILE, "Four", "fathers", "dedicated") msg = rf"Missing content in {GOLD_GETTY_FILE_RX}: 'xyzzy'" with pytest.raises(AssertionError, match=msg): contains(GOLD_GETTY_FILE, "Four", "fathers", "xyzzy", "dedicated")
def test_b_branch(self): self.make_file("b.py", """\ def one(x): # This will be a branch that misses the else. if x < 2: a = 3 else: a = 4 one(1) def two(x): # A missed else that branches to "exit" if x: a = 5 two(1) def three(): try: # This if has two branches, *neither* one taken. if name_error_this_variable_doesnt_exist: a = 1 else: a = 2 except: pass three() """) cov = coverage.Coverage(branch=True) b = self.start_import_stop(cov, "b") cov.html_report(b, directory="out") compare_html(gold_path("html/b_branch"), "out") contains( "out/b_py.html", ('<span class="key">if</span> <span class="nam">x</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">70%</span>', ('<span class="annotate short">3 ↛ 6</span>' '<span class="annotate long">line 3 didn\'t jump to line 6, ' 'because the condition on line 3 was never false</span>'), ('<span class="annotate short">12 ↛ exit</span>' '<span class="annotate long">line 12 didn\'t return from function \'two\', ' 'because the condition on line 12 was never false</span>'), ('<span class="annotate short">20 ↛ 21, ' '20 ↛ 23</span>' '<span class="annotate long">2 missed branches: ' '1) line 20 didn\'t jump to line 21, ' 'because the condition on line 20 was never true, ' '2) line 20 didn\'t jump to line 23, ' 'because the condition on line 20 was never false</span>'), ) contains( "out/index.html", '<a href="b_py.html">b.py</a>', '<span class="pc_cov">70%</span>', '<td class="right" data-ratio="16 23">70%</td>', )
def test_b_branch(self): self.make_file("b.py", """\ def one(x): # This will be a branch that misses the else. if x < 2: a = 3 else: a = 4 one(1) def two(x): # A missed else that branches to "exit" if x: a = 5 two(1) def three(): try: # This if has two branches, *neither* one taken. if name_error_this_variable_doesnt_exist: a = 1 else: a = 2 except: pass three() """) cov = coverage.Coverage(branch=True) b = self.start_import_stop(cov, "b") cov.html_report(b, directory="out/b_branch") compare_html(gold_path("html/b_branch"), "out/b_branch") contains( "out/b_branch/b_py.html", ('<span class="key">if</span> <span class="nam">x</span> ' '<span class="op"><</span> <span class="num">2</span>'), (' <span class="nam">a</span> <span class="op">=</span> ' '<span class="num">3</span>'), '<span class="pc_cov">70%</span>', ('<span class="annotate short">3 ↛ 6</span>' '<span class="annotate long">line 3 didn\'t jump to line 6, ' 'because the condition on line 3 was never false</span>'), ('<span class="annotate short">12 ↛ exit</span>' '<span class="annotate long">line 12 didn\'t return from function \'two\', ' 'because the condition on line 12 was never false</span>'), ('<span class="annotate short">20 ↛ 21, ' '20 ↛ 23</span>' '<span class="annotate long">2 missed branches: ' '1) line 20 didn\'t jump to line 21, ' 'because the condition on line 20 was never true, ' '2) line 20 didn\'t jump to line 23, ' 'because the condition on line 20 was never false</span>'), ) contains( "out/b_branch/index.html", '<a href="b_py.html">b.py</a>', '<span class="pc_cov">70%</span>', '<td class="right" data-ratio="16 23">70%</td>', )