summaryrefslogtreecommitdiff
path: root/tests/test_api.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_api.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_api.py')
-rw-r--r--tests/test_api.py571
1 files changed, 571 insertions, 0 deletions
diff --git a/tests/test_api.py b/tests/test_api.py
new file mode 100644
index 00000000..559684b8
--- /dev/null
+++ b/tests/test_api.py
@@ -0,0 +1,571 @@
+"""Tests for Coverage's api."""
+
+import fnmatch, os, re, sys, textwrap
+
+import coverage
+from coverage.backward import StringIO
+
+from test.coveragetest import CoverageTest
+
+
+class SingletonApiTest(CoverageTest):
+ """Tests of the old-fashioned singleton API."""
+
+ def setUp(self):
+ super(SingletonApiTest, self).setUp()
+ # These tests use the singleton module interface. Prevent it from
+ # writing .coverage files at exit.
+ coverage.use_cache(0)
+
+ def do_report_work(self, modname):
+ """Create a module named `modname`, then measure it."""
+ coverage.erase()
+
+ self.make_file(modname+".py", """\
+ a = 1
+ b = 2
+ if b == 3:
+ c = 4
+ d = 5
+ e = 6
+ f = 7
+ """)
+
+ # Import the python file, executing it.
+ self.start_import_stop(coverage, modname)
+
+ def test_simple(self):
+ coverage.erase()
+
+ self.make_file("mycode.py", """\
+ a = 1
+ b = 2
+ if b == 3:
+ c = 4
+ d = 5
+ """)
+
+ # Import the python file, executing it.
+ self.start_import_stop(coverage, "mycode")
+
+ _, statements, missing, missingtext = coverage.analysis("mycode.py")
+ self.assertEqual(statements, [1,2,3,4,5])
+ self.assertEqual(missing, [4])
+ self.assertEqual(missingtext, "4")
+
+ def test_report(self):
+ self.do_report_work("mycode2")
+ coverage.report(["mycode2.py"])
+ self.assertEqual(self.stdout(), textwrap.dedent("""\
+ Name Stmts Miss Cover Missing
+ ---------------------------------------
+ mycode2 7 3 57% 4-6
+ """))
+
+ def test_report_file(self):
+ # The file= argument of coverage.report makes the report go there.
+ self.do_report_work("mycode3")
+ fout = StringIO()
+ coverage.report(["mycode3.py"], file=fout)
+ self.assertEqual(self.stdout(), "")
+ self.assertEqual(fout.getvalue(), textwrap.dedent("""\
+ Name Stmts Miss Cover Missing
+ ---------------------------------------
+ mycode3 7 3 57% 4-6
+ """))
+
+ def test_report_default(self):
+ # Calling report() with no morfs will report on whatever was executed.
+ self.do_report_work("mycode4")
+ coverage.report()
+ rpt = re.sub(r"\s+", " ", self.stdout())
+ self.assertIn("mycode4 7 3 57% 4-6", rpt)
+
+
+class ApiTest(CoverageTest):
+ """Api-oriented tests for Coverage."""
+
+ def clean_files(self, files, pats):
+ """Remove names matching `pats` from `files`, a list of filenames."""
+ good = []
+ for f in files:
+ for pat in pats:
+ if fnmatch.fnmatch(f, pat):
+ break
+ else:
+ good.append(f)
+ return good
+
+ def assertFiles(self, files):
+ """Assert that the files here are `files`, ignoring the usual junk."""
+ here = os.listdir(".")
+ here = self.clean_files(here, ["*.pyc", "__pycache__"])
+ self.assertSameElements(here, files)
+
+ def test_unexecuted_file(self):
+ cov = coverage.coverage()
+
+ self.make_file("mycode.py", """\
+ a = 1
+ b = 2
+ if b == 3:
+ c = 4
+ d = 5
+ """)
+
+ self.make_file("not_run.py", """\
+ fooey = 17
+ """)
+
+ # Import the python file, executing it.
+ self.start_import_stop(cov, "mycode")
+
+ _, statements, missing, _ = cov.analysis("not_run.py")
+ self.assertEqual(statements, [1])
+ self.assertEqual(missing, [1])
+
+ 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()
+ self.start_import_stop(cov, "mymain")
+
+ 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()
+ self.start_import_stop(cov, "mymain")
+
+ 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 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 test_include_can_measure_stdlib(self):
+ self.make_file("mymain.py", """\
+ import colorsys, random
+ a = 1
+ r, g, b = [random.random() for _ in range(3)]
+ hls = colorsys.rgb_to_hls(r, g, b)
+ """)
+
+ # Measure without the stdlib, but include colorsys.
+ cov1 = coverage.coverage(cover_pylib=False, include=["*/colorsys.py"])
+ self.start_import_stop(cov1, "mymain")
+
+ # some statements were marked executed in colorsys.py
+ _, statements, missing, _ = cov1.analysis("colorsys.py")
+ self.assertNotEqual(statements, missing)
+ # but none were in random.py
+ _, statements, missing, _ = cov1.analysis("random.py")
+ self.assertEqual(statements, missing)
+
+ def test_exclude_list(self):
+ cov = coverage.coverage()
+ cov.clear_exclude()
+ self.assertEqual(cov.get_exclude_list(), [])
+ cov.exclude("foo")
+ self.assertEqual(cov.get_exclude_list(), ["foo"])
+ cov.exclude("bar")
+ self.assertEqual(cov.get_exclude_list(), ["foo", "bar"])
+ self.assertEqual(cov._exclude_regex('exclude'), "(foo)|(bar)")
+ cov.clear_exclude()
+ self.assertEqual(cov.get_exclude_list(), [])
+
+ def test_exclude_partial_list(self):
+ cov = coverage.coverage()
+ cov.clear_exclude(which='partial')
+ self.assertEqual(cov.get_exclude_list(which='partial'), [])
+ cov.exclude("foo", which='partial')
+ self.assertEqual(cov.get_exclude_list(which='partial'), ["foo"])
+ cov.exclude("bar", which='partial')
+ self.assertEqual(cov.get_exclude_list(which='partial'), ["foo", "bar"])
+ self.assertEqual(cov._exclude_regex(which='partial'), "(foo)|(bar)")
+ cov.clear_exclude(which='partial')
+ self.assertEqual(cov.get_exclude_list(which='partial'), [])
+
+ def test_exclude_and_partial_are_separate_lists(self):
+ cov = coverage.coverage()
+ cov.clear_exclude(which='partial')
+ cov.clear_exclude(which='exclude')
+ cov.exclude("foo", which='partial')
+ self.assertEqual(cov.get_exclude_list(which='partial'), ['foo'])
+ self.assertEqual(cov.get_exclude_list(which='exclude'), [])
+ cov.exclude("bar", which='exclude')
+ self.assertEqual(cov.get_exclude_list(which='partial'), ['foo'])
+ self.assertEqual(cov.get_exclude_list(which='exclude'), ['bar'])
+ cov.exclude("p2", which='partial')
+ cov.exclude("e2", which='exclude')
+ self.assertEqual(cov.get_exclude_list(which='partial'), ['foo', 'p2'])
+ self.assertEqual(cov.get_exclude_list(which='exclude'), ['bar', 'e2'])
+ cov.clear_exclude(which='partial')
+ self.assertEqual(cov.get_exclude_list(which='partial'), [])
+ self.assertEqual(cov.get_exclude_list(which='exclude'), ['bar', 'e2'])
+ cov.clear_exclude(which='exclude')
+ self.assertEqual(cov.get_exclude_list(which='partial'), [])
+ self.assertEqual(cov.get_exclude_list(which='exclude'), [])
+
+ def test_datafile_default(self):
+ # Default data file behavior: it's .coverage
+ self.make_file("datatest1.py", """\
+ fooey = 17
+ """)
+
+ self.assertFiles(["datatest1.py"])
+ cov = coverage.coverage()
+ self.start_import_stop(cov, "datatest1")
+ cov.save()
+ self.assertFiles(["datatest1.py", ".coverage"])
+
+ def test_datafile_specified(self):
+ # You can specify the data file name.
+ self.make_file("datatest2.py", """\
+ fooey = 17
+ """)
+
+ self.assertFiles(["datatest2.py"])
+ cov = coverage.coverage(data_file="cov.data")
+ self.start_import_stop(cov, "datatest2")
+ cov.save()
+ self.assertFiles(["datatest2.py", "cov.data"])
+
+ def test_datafile_and_suffix_specified(self):
+ # You can specify the data file name and suffix.
+ self.make_file("datatest3.py", """\
+ fooey = 17
+ """)
+
+ self.assertFiles(["datatest3.py"])
+ cov = coverage.coverage(data_file="cov.data", data_suffix="14")
+ self.start_import_stop(cov, "datatest3")
+ cov.save()
+ self.assertFiles(["datatest3.py", "cov.data.14"])
+
+ def test_datafile_from_rcfile(self):
+ # You can specify the data file name in the .coveragerc file
+ self.make_file("datatest4.py", """\
+ fooey = 17
+ """)
+ self.make_file(".coveragerc", """\
+ [run]
+ data_file = mydata.dat
+ """)
+
+ self.assertFiles(["datatest4.py", ".coveragerc"])
+ cov = coverage.coverage()
+ self.start_import_stop(cov, "datatest4")
+ cov.save()
+ self.assertFiles(["datatest4.py", ".coveragerc", "mydata.dat"])
+
+ def test_empty_reporting(self):
+ # Used to be you'd get an exception reporting on nothing...
+ cov = coverage.coverage()
+ cov.erase()
+ cov.report()
+
+ def test_start_stop_start_stop(self):
+ self.make_file("code1.py", """\
+ code1 = 1
+ """)
+ self.make_file("code2.py", """\
+ code2 = 1
+ code2 = 2
+ """)
+ cov = coverage.coverage()
+ self.start_import_stop(cov, "code1")
+ cov.save()
+ self.start_import_stop(cov, "code2")
+ _, statements, missing, _ = cov.analysis("code1.py")
+ self.assertEqual(statements, [1])
+ self.assertEqual(missing, [])
+ _, statements, missing, _ = cov.analysis("code2.py")
+ self.assertEqual(statements, [1, 2])
+ self.assertEqual(missing, [])
+
+ if 0: # expected failure
+ # for https://bitbucket.org/ned/coveragepy/issue/79
+ def test_start_save_stop(self):
+ self.make_file("code1.py", """\
+ code1 = 1
+ """)
+ self.make_file("code2.py", """\
+ code2 = 1
+ code2 = 2
+ """)
+ cov = coverage.coverage()
+ cov.start()
+ self.import_local_file("code1")
+ cov.save()
+ self.import_local_file("code2")
+ cov.stop()
+
+ _, statements, missing, _ = cov.analysis("code1.py")
+ self.assertEqual(statements, [1])
+ self.assertEqual(missing, [])
+ _, statements, missing, _ = cov.analysis("code2.py")
+ self.assertEqual(statements, [1, 2])
+ self.assertEqual(missing, [])
+
+
+
+class UsingModulesMixin(object):
+ """A mixin for importing modules from test/modules and test/moremodules."""
+
+ run_in_temp_dir = False
+
+ def setUp(self):
+ super(UsingModulesMixin, self).setUp()
+ # Parent class saves and restores sys.path, we can just modify it.
+ self.old_dir = os.getcwd()
+ os.chdir(self.nice_file(os.path.dirname(__file__), 'modules'))
+ sys.path.append(".")
+ sys.path.append("../moremodules")
+
+ def tearDown(self):
+ os.chdir(self.old_dir)
+ super(UsingModulesMixin, self).tearDown()
+
+
+class OmitIncludeTestsMixin(UsingModulesMixin):
+ """Test methods for coverage methods taking include and omit."""
+
+ def filenames_in(self, summary, filenames):
+ """Assert the `filenames` are in the keys of `summary`."""
+ for filename in filenames.split():
+ self.assertIn(filename, summary)
+
+ def filenames_not_in(self, summary, filenames):
+ """Assert the `filenames` are not in the keys of `summary`."""
+ for filename in filenames.split():
+ self.assertNotIn(filename, summary)
+
+ def test_nothing_specified(self):
+ result = self.coverage_usepkgs()
+ self.filenames_in(result, "p1a p1b p2a p2b othera otherb osa osb")
+ self.filenames_not_in(result, "p1c")
+ # Because there was no source= specified, we don't search for
+ # unexecuted files.
+
+ def test_include(self):
+ result = self.coverage_usepkgs(include=["*/p1a.py"])
+ self.filenames_in(result, "p1a")
+ self.filenames_not_in(result, "p1b p1c p2a p2b othera otherb osa osb")
+
+ def test_include_2(self):
+ result = self.coverage_usepkgs(include=["*a.py"])
+ self.filenames_in(result, "p1a p2a othera osa")
+ self.filenames_not_in(result, "p1b p1c p2b otherb osb")
+
+ def test_include_as_string(self):
+ result = self.coverage_usepkgs(include="*a.py")
+ self.filenames_in(result, "p1a p2a othera osa")
+ self.filenames_not_in(result, "p1b p1c p2b otherb osb")
+
+ def test_omit(self):
+ result = self.coverage_usepkgs(omit=["*/p1a.py"])
+ self.filenames_in(result, "p1b p2a p2b")
+ self.filenames_not_in(result, "p1a p1c")
+
+ def test_omit_2(self):
+ result = self.coverage_usepkgs(omit=["*a.py"])
+ self.filenames_in(result, "p1b p2b otherb osb")
+ self.filenames_not_in(result, "p1a p1c p2a othera osa")
+
+ def test_omit_as_string(self):
+ result = self.coverage_usepkgs(omit="*a.py")
+ self.filenames_in(result, "p1b p2b otherb osb")
+ self.filenames_not_in(result, "p1a p1c p2a othera osa")
+
+ def test_omit_and_include(self):
+ result = self.coverage_usepkgs( include=["*/p1*"], omit=["*/p1a.py"])
+ self.filenames_in(result, "p1b")
+ self.filenames_not_in(result, "p1a p1c p2a p2b")
+
+
+class SourceOmitIncludeTest(OmitIncludeTestsMixin, CoverageTest):
+ """Test using `source`, `omit` and `include` when measuring code."""
+
+ def coverage_usepkgs(self, **kwargs):
+ """Run coverage on usepkgs and return the line summary.
+
+ Arguments are passed to the `coverage.coverage` constructor.
+
+ """
+ cov = coverage.coverage(**kwargs)
+ cov.start()
+ import usepkgs # pragma: nested # pylint: disable=F0401,W0612
+ cov.stop() # pragma: nested
+ cov._harvest_data() # private! sshhh...
+ summary = cov.data.summary()
+ for k, v in list(summary.items()):
+ assert k.endswith(".py")
+ summary[k[:-3]] = v
+ return summary
+
+ def test_source_package(self):
+ lines = self.coverage_usepkgs(source=["pkg1"])
+ self.filenames_in(lines, "p1a p1b")
+ self.filenames_not_in(lines, "p2a p2b othera otherb osa osb")
+ # Because source= was specified, we do search for unexecuted files.
+ self.assertEqual(lines['p1c'], 0)
+
+ def test_source_package_dotted(self):
+ lines = self.coverage_usepkgs(source=["pkg1.p1b"])
+ self.filenames_in(lines, "p1b")
+ self.filenames_not_in(lines, "p1a p1c p2a p2b othera otherb osa osb")
+
+
+class ReportIncludeOmitTest(OmitIncludeTestsMixin, CoverageTest):
+ """Tests of the report include/omit functionality."""
+
+ def coverage_usepkgs(self, **kwargs):
+ """Try coverage.report()."""
+ cov = coverage.coverage()
+ cov.start()
+ import usepkgs # pragma: nested # pylint: disable=F0401,W0612
+ cov.stop() # pragma: nested
+ report = StringIO()
+ cov.report(file=report, **kwargs)
+ return report.getvalue()
+
+
+class XmlIncludeOmitTest(OmitIncludeTestsMixin, CoverageTest):
+ """Tests of the xml include/omit functionality.
+
+ This also takes care of the HTML and annotate include/omit, by virtue
+ of the structure of the code.
+
+ """
+
+ def coverage_usepkgs(self, **kwargs):
+ """Try coverage.xml_report()."""
+ cov = coverage.coverage()
+ cov.start()
+ import usepkgs # pragma: nested # pylint: disable=F0401,W0612
+ cov.stop() # pragma: nested
+ cov.xml_report(outfile="-", **kwargs)
+ return self.stdout()
+
+
+class AnalysisTest(CoverageTest):
+ """Test the numerical analysis of results."""
+ def test_many_missing_branches(self):
+ cov = coverage.coverage(branch=True)
+
+ self.make_file("missing.py", """\
+ def fun1(x):
+ if x == 1:
+ print("one")
+ else:
+ print("not one")
+ print("done") # pragma: nocover
+
+ def fun2(x):
+ print("x")
+
+ fun2(3)
+ """)
+
+ # Import the python file, executing it.
+ self.start_import_stop(cov, "missing")
+
+ nums = cov._analyze("missing.py").numbers
+ self.assertEqual(nums.n_files, 1)
+ self.assertEqual(nums.n_statements, 7)
+ self.assertEqual(nums.n_excluded, 1)
+ self.assertEqual(nums.n_missing, 3)
+ self.assertEqual(nums.n_branches, 2)
+ self.assertEqual(nums.n_partial_branches, 0)
+ self.assertEqual(nums.n_missing_branches, 2)
+
+
+class PluginTest(CoverageTest):
+ """Test that the API works properly the way the plugins call it.
+
+ We don't actually use the plugins, but these tests call the API the same
+ way they do.
+
+ """
+ def pretend_to_be_nose_with_cover(self, erase):
+ """This is what the nose --with-cover plugin does."""
+ cov = coverage.coverage()
+
+ self.make_file("no_biggie.py", """\
+ a = 1
+ b = 2
+ if b == 1:
+ c = 4
+ """)
+
+ if erase:
+ cov.combine()
+ cov.erase()
+ cov.load()
+ self.start_import_stop(cov, "no_biggie")
+ cov.combine()
+ cov.save()
+ cov.report(["no_biggie.py"])
+ self.assertEqual(self.stdout(), textwrap.dedent("""\
+ Name Stmts Miss Cover Missing
+ -----------------------------------------
+ no_biggie 4 1 75% 4
+ """))
+
+ def test_nose_plugin(self):
+ self.pretend_to_be_nose_with_cover(erase=False)
+
+ def test_nose_plugin_with_erase(self):
+ self.pretend_to_be_nose_with_cover(erase=True)