summaryrefslogtreecommitdiff
path: root/tests/test_summary.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2013-02-02 11:15:11 -0500
committerNed Batchelder <ned@nedbatchelder.com>2013-02-02 11:15:11 -0500
commitc19232a9d2be2bde30e446311f32e9dcdd5d4774 (patch)
treec2a9afb49cb74d9e041058c0275e1896dc0ecd0c /tests/test_summary.py
parentc359cea1408c410a4c770debeddf3aed0abd43fe (diff)
downloadpython-coveragepy-git-c19232a9d2be2bde30e446311f32e9dcdd5d4774.tar.gz
Move the test directory to tests to avoid conflicts with the stdlib test package.
--HG-- rename : test/__init__.py => tests/__init__.py rename : test/backtest.py => tests/backtest.py rename : test/backunittest.py => tests/backunittest.py rename : test/coveragetest.py => tests/coveragetest.py rename : test/covmodzip1.py => tests/covmodzip1.py rename : test/eggsrc/egg1/__init__.py => tests/eggsrc/egg1/__init__.py rename : test/eggsrc/egg1/egg1.py => tests/eggsrc/egg1/egg1.py rename : test/eggsrc/setup.py => tests/eggsrc/setup.py rename : test/farm/annotate/annotate_dir.py => tests/farm/annotate/annotate_dir.py rename : test/farm/annotate/gold/white.py,cover => tests/farm/annotate/gold/white.py,cover rename : test/farm/annotate/gold_anno_dir/a___init__.py,cover => tests/farm/annotate/gold_anno_dir/a___init__.py,cover rename : test/farm/annotate/gold_anno_dir/a_a.py,cover => tests/farm/annotate/gold_anno_dir/a_a.py,cover rename : test/farm/annotate/gold_anno_dir/b___init__.py,cover => tests/farm/annotate/gold_anno_dir/b___init__.py,cover rename : test/farm/annotate/gold_anno_dir/b_b.py,cover => tests/farm/annotate/gold_anno_dir/b_b.py,cover rename : test/farm/annotate/gold_anno_dir/multi.py,cover => tests/farm/annotate/gold_anno_dir/multi.py,cover rename : test/farm/annotate/gold_multi/a/__init__.py,cover => tests/farm/annotate/gold_multi/a/__init__.py,cover rename : test/farm/annotate/gold_multi/a/a.py,cover => tests/farm/annotate/gold_multi/a/a.py,cover rename : test/farm/annotate/gold_multi/b/__init__.py,cover => tests/farm/annotate/gold_multi/b/__init__.py,cover rename : test/farm/annotate/gold_multi/b/b.py,cover => tests/farm/annotate/gold_multi/b/b.py,cover rename : test/farm/annotate/gold_multi/multi.py,cover => tests/farm/annotate/gold_multi/multi.py,cover rename : test/farm/annotate/gold_v24/white.py,cover => tests/farm/annotate/gold_v24/white.py,cover rename : test/farm/annotate/run.py => tests/farm/annotate/run.py rename : test/farm/annotate/run_multi.py => tests/farm/annotate/run_multi.py rename : test/farm/annotate/src/a/__init__.py => tests/farm/annotate/src/a/__init__.py rename : test/farm/annotate/src/a/a.py => tests/farm/annotate/src/a/a.py rename : test/farm/annotate/src/b/__init__.py => tests/farm/annotate/src/b/__init__.py rename : test/farm/annotate/src/b/b.py => tests/farm/annotate/src/b/b.py rename : test/farm/annotate/src/multi.py => tests/farm/annotate/src/multi.py rename : test/farm/annotate/src/white.py => tests/farm/annotate/src/white.py rename : test/farm/html/gold_a/a.html => tests/farm/html/gold_a/a.html rename : test/farm/html/gold_a/index.html => tests/farm/html/gold_a/index.html rename : test/farm/html/gold_b_branch/b.html => tests/farm/html/gold_b_branch/b.html rename : test/farm/html/gold_b_branch/index.html => tests/farm/html/gold_b_branch/index.html rename : test/farm/html/gold_bom/bom.html => tests/farm/html/gold_bom/bom.html rename : test/farm/html/gold_bom/index.html => tests/farm/html/gold_bom/index.html rename : test/farm/html/gold_isolatin1/index.html => tests/farm/html/gold_isolatin1/index.html rename : test/farm/html/gold_isolatin1/isolatin1.html => tests/farm/html/gold_isolatin1/isolatin1.html rename : test/farm/html/gold_omit_1/index.html => tests/farm/html/gold_omit_1/index.html rename : test/farm/html/gold_omit_1/m1.html => tests/farm/html/gold_omit_1/m1.html rename : test/farm/html/gold_omit_1/m2.html => tests/farm/html/gold_omit_1/m2.html rename : test/farm/html/gold_omit_1/m3.html => tests/farm/html/gold_omit_1/m3.html rename : test/farm/html/gold_omit_1/main.html => tests/farm/html/gold_omit_1/main.html rename : test/farm/html/gold_omit_2/index.html => tests/farm/html/gold_omit_2/index.html rename : test/farm/html/gold_omit_2/m2.html => tests/farm/html/gold_omit_2/m2.html rename : test/farm/html/gold_omit_2/m3.html => tests/farm/html/gold_omit_2/m3.html rename : test/farm/html/gold_omit_2/main.html => tests/farm/html/gold_omit_2/main.html rename : test/farm/html/gold_omit_3/index.html => tests/farm/html/gold_omit_3/index.html rename : test/farm/html/gold_omit_3/m3.html => tests/farm/html/gold_omit_3/m3.html rename : test/farm/html/gold_omit_3/main.html => tests/farm/html/gold_omit_3/main.html rename : test/farm/html/gold_omit_4/index.html => tests/farm/html/gold_omit_4/index.html rename : test/farm/html/gold_omit_4/m1.html => tests/farm/html/gold_omit_4/m1.html rename : test/farm/html/gold_omit_4/m3.html => tests/farm/html/gold_omit_4/m3.html rename : test/farm/html/gold_omit_4/main.html => tests/farm/html/gold_omit_4/main.html rename : test/farm/html/gold_omit_5/index.html => tests/farm/html/gold_omit_5/index.html rename : test/farm/html/gold_omit_5/m1.html => tests/farm/html/gold_omit_5/m1.html rename : test/farm/html/gold_omit_5/main.html => tests/farm/html/gold_omit_5/main.html rename : test/farm/html/gold_other/blah_blah_other.html => tests/farm/html/gold_other/blah_blah_other.html rename : test/farm/html/gold_other/here.html => tests/farm/html/gold_other/here.html rename : test/farm/html/gold_other/index.html => tests/farm/html/gold_other/index.html rename : test/farm/html/gold_partial/index.html => tests/farm/html/gold_partial/index.html rename : test/farm/html/gold_partial/partial.html => tests/farm/html/gold_partial/partial.html rename : test/farm/html/gold_styled/a.html => tests/farm/html/gold_styled/a.html rename : test/farm/html/gold_styled/extra.css => tests/farm/html/gold_styled/extra.css rename : test/farm/html/gold_styled/index.html => tests/farm/html/gold_styled/index.html rename : test/farm/html/gold_styled/style.css => tests/farm/html/gold_styled/style.css rename : test/farm/html/gold_unicode/index.html => tests/farm/html/gold_unicode/index.html rename : test/farm/html/gold_unicode/unicode.html => tests/farm/html/gold_unicode/unicode.html rename : test/farm/html/gold_x_xml/coverage.xml => tests/farm/html/gold_x_xml/coverage.xml rename : test/farm/html/gold_y_xml_branch/coverage.xml => tests/farm/html/gold_y_xml_branch/coverage.xml rename : test/farm/html/othersrc/other.py => tests/farm/html/othersrc/other.py rename : test/farm/html/run_a.py => tests/farm/html/run_a.py rename : test/farm/html/run_a_xml_1.py => tests/farm/html/run_a_xml_1.py rename : test/farm/html/run_a_xml_2.py => tests/farm/html/run_a_xml_2.py rename : test/farm/html/run_b_branch.py => tests/farm/html/run_b_branch.py rename : test/farm/html/run_bom.py => tests/farm/html/run_bom.py rename : test/farm/html/run_isolatin1.py => tests/farm/html/run_isolatin1.py rename : test/farm/html/run_omit_1.py => tests/farm/html/run_omit_1.py rename : test/farm/html/run_omit_2.py => tests/farm/html/run_omit_2.py rename : test/farm/html/run_omit_3.py => tests/farm/html/run_omit_3.py rename : test/farm/html/run_omit_4.py => tests/farm/html/run_omit_4.py rename : test/farm/html/run_omit_5.py => tests/farm/html/run_omit_5.py rename : test/farm/html/run_other.py => tests/farm/html/run_other.py rename : test/farm/html/run_partial.py => tests/farm/html/run_partial.py rename : test/farm/html/run_styled.py => tests/farm/html/run_styled.py rename : test/farm/html/run_tabbed.py => tests/farm/html/run_tabbed.py rename : test/farm/html/run_unicode.py => tests/farm/html/run_unicode.py rename : test/farm/html/run_y_xml_branch.py => tests/farm/html/run_y_xml_branch.py rename : test/farm/html/src/a.py => tests/farm/html/src/a.py rename : test/farm/html/src/b.py => tests/farm/html/src/b.py rename : test/farm/html/src/bom.py => tests/farm/html/src/bom.py rename : test/farm/html/src/coverage.xml => tests/farm/html/src/coverage.xml rename : test/farm/html/src/extra.css => tests/farm/html/src/extra.css rename : test/farm/html/src/here.py => tests/farm/html/src/here.py rename : test/farm/html/src/isolatin1.py => tests/farm/html/src/isolatin1.py rename : test/farm/html/src/m1.py => tests/farm/html/src/m1.py rename : test/farm/html/src/m2.py => tests/farm/html/src/m2.py rename : test/farm/html/src/m3.py => tests/farm/html/src/m3.py rename : test/farm/html/src/main.py => tests/farm/html/src/main.py rename : test/farm/html/src/omit4.ini => tests/farm/html/src/omit4.ini rename : test/farm/html/src/omit5.ini => tests/farm/html/src/omit5.ini rename : test/farm/html/src/partial.py => tests/farm/html/src/partial.py rename : test/farm/html/src/run_a_xml_2.ini => tests/farm/html/src/run_a_xml_2.ini rename : test/farm/html/src/tabbed.py => tests/farm/html/src/tabbed.py rename : test/farm/html/src/unicode.py => tests/farm/html/src/unicode.py rename : test/farm/html/src/y.py => tests/farm/html/src/y.py rename : test/farm/run/run_chdir.py => tests/farm/run/run_chdir.py rename : test/farm/run/run_timid.py => tests/farm/run/run_timid.py rename : test/farm/run/run_xxx.py => tests/farm/run/run_xxx.py rename : test/farm/run/src/chdir.py => tests/farm/run/src/chdir.py rename : test/farm/run/src/showtrace.py => tests/farm/run/src/showtrace.py rename : test/farm/run/src/subdir/placeholder => tests/farm/run/src/subdir/placeholder rename : test/farm/run/src/xxx => tests/farm/run/src/xxx rename : test/js/index.html => tests/js/index.html rename : test/js/tests.js => tests/js/tests.js rename : test/modules/aa/__init__.py => tests/modules/aa/__init__.py rename : test/modules/aa/afile.odd.py => tests/modules/aa/afile.odd.py rename : test/modules/aa/afile.py => tests/modules/aa/afile.py rename : test/modules/aa/bb.odd/bfile.py => tests/modules/aa/bb.odd/bfile.py rename : test/modules/aa/bb/__init__.py => tests/modules/aa/bb/__init__.py rename : test/modules/aa/bb/bfile.odd.py => tests/modules/aa/bb/bfile.odd.py rename : test/modules/aa/bb/bfile.py => tests/modules/aa/bb/bfile.py rename : test/modules/aa/bb/cc/__init__.py => tests/modules/aa/bb/cc/__init__.py rename : test/modules/aa/bb/cc/cfile.py => tests/modules/aa/bb/cc/cfile.py rename : test/modules/aa/zfile.py => tests/modules/aa/zfile.py rename : test/modules/covmod1.py => tests/modules/covmod1.py rename : test/modules/pkg1/__init__.py => tests/modules/pkg1/__init__.py rename : test/modules/pkg1/__main__.py => tests/modules/pkg1/__main__.py rename : test/modules/pkg1/p1a.py => tests/modules/pkg1/p1a.py rename : test/modules/pkg1/p1b.py => tests/modules/pkg1/p1b.py rename : test/modules/pkg1/p1c.py => tests/modules/pkg1/p1c.py rename : test/modules/pkg1/runmod2.py => tests/modules/pkg1/runmod2.py rename : test/modules/pkg1/sub/__init__.py => tests/modules/pkg1/sub/__init__.py rename : test/modules/pkg1/sub/__main__.py => tests/modules/pkg1/sub/__main__.py rename : test/modules/pkg1/sub/ps1a.py => tests/modules/pkg1/sub/ps1a.py rename : test/modules/pkg1/sub/runmod3.py => tests/modules/pkg1/sub/runmod3.py rename : test/modules/pkg2/__init__.py => tests/modules/pkg2/__init__.py rename : test/modules/pkg2/p2a.py => tests/modules/pkg2/p2a.py rename : test/modules/pkg2/p2b.py => tests/modules/pkg2/p2b.py rename : test/modules/runmod1.py => tests/modules/runmod1.py rename : test/modules/usepkgs.py => tests/modules/usepkgs.py rename : test/moremodules/othermods/__init__.py => tests/moremodules/othermods/__init__.py rename : test/moremodules/othermods/othera.py => tests/moremodules/othermods/othera.py rename : test/moremodules/othermods/otherb.py => tests/moremodules/othermods/otherb.py rename : test/moremodules/othermods/sub/__init__.py => tests/moremodules/othermods/sub/__init__.py rename : test/moremodules/othermods/sub/osa.py => tests/moremodules/othermods/sub/osa.py rename : test/moremodules/othermods/sub/osb.py => tests/moremodules/othermods/sub/osb.py rename : test/osinfo.py => tests/osinfo.py rename : test/qunit/jquery.tmpl.min.js => tests/qunit/jquery.tmpl.min.js rename : test/qunit/qunit.css => tests/qunit/qunit.css rename : test/qunit/qunit.js => tests/qunit/qunit.js rename : test/stress_phystoken.tok => tests/stress_phystoken.tok rename : test/stress_phystoken_dos.tok => tests/stress_phystoken_dos.tok rename : test/test_api.py => tests/test_api.py rename : test/test_arcs.py => tests/test_arcs.py rename : test/test_cmdline.py => tests/test_cmdline.py rename : test/test_codeunit.py => tests/test_codeunit.py rename : test/test_config.py => tests/test_config.py rename : test/test_coverage.py => tests/test_coverage.py rename : test/test_data.py => tests/test_data.py rename : test/test_execfile.py => tests/test_execfile.py rename : test/test_farm.py => tests/test_farm.py rename : test/test_files.py => tests/test_files.py rename : test/test_html.py => tests/test_html.py rename : test/test_misc.py => tests/test_misc.py rename : test/test_oddball.py => tests/test_oddball.py rename : test/test_parser.py => tests/test_parser.py rename : test/test_phystokens.py => tests/test_phystokens.py rename : test/test_process.py => tests/test_process.py rename : test/test_results.py => tests/test_results.py rename : test/test_summary.py => tests/test_summary.py rename : test/test_templite.py => tests/test_templite.py rename : test/test_testing.py => tests/test_testing.py rename : test/test_xml.py => tests/test_xml.py rename : test/try_execfile.py => tests/try_execfile.py
Diffstat (limited to 'tests/test_summary.py')
-rw-r--r--tests/test_summary.py298
1 files changed, 298 insertions, 0 deletions
diff --git a/tests/test_summary.py b/tests/test_summary.py
new file mode 100644
index 00000000..5bb903a6
--- /dev/null
+++ b/tests/test_summary.py
@@ -0,0 +1,298 @@
+"""Test text-based summary reporting for coverage.py"""
+
+import os, re, sys
+
+import coverage
+from coverage.backward import StringIO
+
+from test.coveragetest import CoverageTest
+
+class SummaryTest(CoverageTest):
+ """Tests of the text summary reporting for coverage.py."""
+
+ def setUp(self):
+ super(SummaryTest, self).setUp()
+ self.make_file("mycode.py", """\
+ import covmod1
+ import covmodzip1
+ a = 1
+ print ('done')
+ """)
+ # Parent class saves and restores sys.path, we can just modify it.
+ sys.path.append(self.nice_file(os.path.dirname(__file__), 'modules'))
+
+ def report_from_command(self, cmd):
+ """Return the report from the `cmd`, with some convenience added."""
+ report = self.run_command(cmd).replace('\\', '/')
+ self.assertNotIn("error", report.lower())
+ return report
+
+ def line_count(self, report):
+ """How many lines are in `report`?"""
+ self.assertEqual(report.split('\n')[-1], "")
+ return len(report.split('\n')) - 1
+
+ def last_line_squeezed(self, report):
+ """Return the last line of `report` with the spaces squeezed down."""
+ last_line = report.split('\n')[-2]
+ return re.sub(r"\s+", " ", last_line)
+
+ def test_report(self):
+ out = self.run_command("coverage -x mycode.py")
+ self.assertEqual(out, 'done\n')
+ report = self.report_from_command("coverage -r")
+
+ # Name Stmts Miss Cover
+ # ---------------------------------------------------------------------
+ # c:/ned/coverage/trunk/test/modules/covmod1 2 0 100%
+ # c:/ned/coverage/trunk/test/zipmods.zip/covmodzip1 2 0 100%
+ # mycode 4 0 100%
+ # ---------------------------------------------------------------------
+ # TOTAL 8 0 100%
+
+ self.assertNotIn("/coverage/__init__/", report)
+ self.assertIn("/test/modules/covmod1 ", report)
+ self.assertIn("/test/zipmods.zip/covmodzip1 ", report)
+ self.assertIn("mycode ", report)
+ self.assertEqual(self.last_line_squeezed(report), "TOTAL 8 0 100%")
+
+ def test_report_just_one(self):
+ # Try reporting just one module
+ self.run_command("coverage -x mycode.py")
+ report = self.report_from_command("coverage -r mycode.py")
+
+ # Name Stmts Miss Cover
+ # ----------------------------
+ # mycode 4 0 100%
+
+ self.assertEqual(self.line_count(report), 3)
+ self.assertNotIn("/coverage/", report)
+ self.assertNotIn("/test/modules/covmod1 ", report)
+ self.assertNotIn("/test/zipmods.zip/covmodzip1 ", report)
+ self.assertIn("mycode ", report)
+ self.assertEqual(self.last_line_squeezed(report), "mycode 4 0 100%")
+
+ def test_report_omitting(self):
+ # Try reporting while omitting some modules
+ prefix = os.path.split(__file__)[0]
+ self.run_command("coverage -x mycode.py")
+ report = self.report_from_command("coverage -r -o '%s/*'" % prefix)
+
+ # Name Stmts Miss Cover
+ # ----------------------------
+ # mycode 4 0 100%
+
+ self.assertEqual(self.line_count(report), 3)
+ self.assertNotIn("/coverage/", report)
+ self.assertNotIn("/test/modules/covmod1 ", report)
+ self.assertNotIn("/test/zipmods.zip/covmodzip1 ", report)
+ self.assertIn("mycode ", report)
+ self.assertEqual(self.last_line_squeezed(report), "mycode 4 0 100%")
+
+ def test_report_including(self):
+ # Try reporting while including some modules
+ self.run_command("coverage run mycode.py")
+ report = self.report_from_command("coverage report --include=mycode*")
+
+ # Name Stmts Miss Cover
+ # ----------------------------
+ # mycode 4 0 100%
+
+ self.assertEqual(self.line_count(report), 3)
+ self.assertNotIn("/coverage/", report)
+ self.assertNotIn("/test/modules/covmod1 ", report)
+ self.assertNotIn("/test/zipmods.zip/covmodzip1 ", report)
+ self.assertIn("mycode ", report)
+ self.assertEqual(self.last_line_squeezed(report), "mycode 4 0 100%")
+
+ def test_report_branches(self):
+ self.make_file("mybranch.py", """\
+ def branch(x):
+ if x:
+ print("x")
+ return x
+ branch(1)
+ """)
+ out = self.run_command("coverage run --branch mybranch.py")
+ self.assertEqual(out, 'x\n')
+ report = self.report_from_command("coverage report")
+
+ # Name Stmts Miss Branch BrMiss Cover
+ # --------------------------------------------
+ # mybranch 5 0 2 1 85%
+
+ self.assertEqual(self.line_count(report), 3)
+ self.assertIn("mybranch ", report)
+ self.assertEqual(self.last_line_squeezed(report),
+ "mybranch 5 0 2 1 86%")
+
+ def test_dotpy_not_python(self):
+ # We run a .py file, and when reporting, we can't parse it as Python.
+ # We should get an error message in the report.
+
+ self.run_command("coverage run mycode.py")
+ self.make_file("mycode.py", "This isn't python at all!")
+ report = self.report_from_command("coverage -r mycode.py")
+
+ # pylint: disable=C0301
+ # Name Stmts Miss Cover
+ # ----------------------------
+ # mycode NotPython: Couldn't parse '/tmp/test_cover/63354509363/mycode.py' as Python source: 'invalid syntax' at line 1
+
+ last = self.last_line_squeezed(report)
+ # The actual file name varies run to run.
+ last = re.sub(r"parse '.*mycode.py", "parse 'mycode.py", last)
+ # The actual error message varies version to version
+ last = re.sub(r": '.*' at", ": 'error' at", last)
+ self.assertEqual(last,
+ "mycode NotPython: "
+ "Couldn't parse 'mycode.py' as Python source: "
+ "'error' at line 1"
+ )
+
+ def test_dotpy_not_python_ignored(self):
+ # We run a .py file, and when reporting, we can't parse it as Python,
+ # but we've said to ignore errors, so there's no error reported.
+ self.run_command("coverage run mycode.py")
+ self.make_file("mycode.py", "This isn't python at all!")
+ report = self.report_from_command("coverage -r -i mycode.py")
+
+ # Name Stmts Miss Cover
+ # ----------------------------
+
+ self.assertEqual(self.line_count(report), 2)
+
+ def test_dothtml_not_python(self):
+ # We run a .html file, and when reporting, we can't parse it as
+ # Python. Since it wasn't .py, no error is reported.
+
+ # Run an "html" file
+ self.make_file("mycode.html", "a = 1")
+ self.run_command("coverage run mycode.html")
+ # Before reporting, change it to be an HTML file.
+ self.make_file("mycode.html", "<h1>This isn't python at all!</h1>")
+ report = self.report_from_command("coverage -r mycode.html")
+
+ # Name Stmts Miss Cover
+ # ----------------------------
+
+ self.assertEqual(self.line_count(report), 2)
+
+ def get_report(self, cov):
+ """Get the report from `cov`, and canonicalize it."""
+ repout = StringIO()
+ cov.report(file=repout, show_missing=False)
+ report = repout.getvalue().replace('\\', '/')
+ report = re.sub(r" +", " ", report)
+ return report
+
+ def test_bug_156_file_not_run_should_be_zero(self):
+ # https://bitbucket.org/ned/coveragepy/issue/156
+ self.make_file("mybranch.py", """\
+ def branch(x):
+ if x:
+ print("x")
+ return x
+ branch(1)
+ """)
+ self.make_file("main.py", """\
+ print("y")
+ """)
+ cov = coverage.coverage(branch=True, source=["."])
+ cov.start()
+ import main # pragma: nested # pylint: disable=F0401,W0612
+ cov.stop() # pragma: nested
+ report = self.get_report(cov).splitlines()
+ self.assertIn("mybranch 5 5 2 2 0%", report)
+
+ def run_TheCode_and_report_it(self):
+ """A helper for the next few tests."""
+ cov = coverage.coverage()
+ cov.start()
+ import TheCode # pragma: nested # pylint: disable=F0401,W0612
+ cov.stop() # pragma: nested
+ return self.get_report(cov)
+
+ def test_bug_203_mixed_case_listed_twice_with_rc(self):
+ self.make_file("TheCode.py", "a = 1\n")
+ self.make_file(".coveragerc", "[run]\nsource = .\n")
+
+ report = self.run_TheCode_and_report_it()
+
+ self.assertIn("TheCode", report)
+ self.assertNotIn("thecode", report)
+
+ def test_bug_203_mixed_case_listed_twice(self):
+ self.make_file("TheCode.py", "a = 1\n")
+
+ report = self.run_TheCode_and_report_it()
+
+ self.assertIn("TheCode", report)
+ self.assertNotIn("thecode", report)
+
+
+class SummaryTest2(CoverageTest):
+ """Another bunch of summary tests."""
+ # This class exists because tests naturally clump into classes based on the
+ # needs of their setUp and tearDown, rather than the product features they
+ # are testing. There's probably a better way to organize these.
+
+ run_in_temp_dir = False
+
+ def setUp(self):
+ super(SummaryTest2, self).setUp()
+ # Parent class saves and restores sys.path, we can just modify it.
+ this_dir = os.path.dirname(__file__)
+ sys.path.append(self.nice_file(this_dir, 'modules'))
+ sys.path.append(self.nice_file(this_dir, 'moremodules'))
+
+ def test_empty_files(self):
+ # Shows that empty files like __init__.py are listed as having zero
+ # statements, not one statement.
+ cov = coverage.coverage()
+ cov.start()
+ import usepkgs # pragma: nested # pylint: disable=F0401,W0612
+ cov.stop() # pragma: nested
+
+ repout = StringIO()
+ cov.report(file=repout, show_missing=False)
+
+ report = repout.getvalue().replace('\\', '/')
+ report = re.sub(r"\s+", " ", report)
+ self.assertIn("test/modules/pkg1/__init__ 1 0 100%", report)
+ self.assertIn("test/modules/pkg2/__init__ 0 0 100%", report)
+
+
+class ReportingReturnValue(CoverageTest):
+ """Tests of reporting functions returning values."""
+
+ def run_coverage(self):
+ """Run coverage on doit.py and return the coverage object."""
+ self.make_file("doit.py", """\
+ a = 1
+ b = 2
+ c = 3
+ d = 4
+ if a > 10:
+ f = 6
+ g = 7
+ """)
+
+ cov = coverage.coverage()
+ self.start_import_stop(cov, "doit")
+ return cov
+
+ def test_report(self):
+ cov = self.run_coverage()
+ val = cov.report(include="*/doit.py")
+ self.assertAlmostEqual(val, 85.7, 1)
+
+ def test_html(self):
+ cov = self.run_coverage()
+ val = cov.html_report(include="*/doit.py")
+ self.assertAlmostEqual(val, 85.7, 1)
+
+ def test_xml(self):
+ cov = self.run_coverage()
+ val = cov.xml_report(include="*/doit.py")
+ self.assertAlmostEqual(val, 85.7, 1)