summaryrefslogtreecommitdiff
path: root/tests/test_api.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_api.py')
-rw-r--r--tests/test_api.py129
1 files changed, 115 insertions, 14 deletions
diff --git a/tests/test_api.py b/tests/test_api.py
index 6f142100..9a3fc820 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -11,11 +11,11 @@ import warnings
import coverage
from coverage import env
-from coverage.backward import StringIO
+from coverage.backward import StringIO, import_local_file
from coverage.misc import CoverageException
from coverage.report import Reporter
-from tests.coveragetest import CoverageTest
+from tests.coveragetest import CoverageTest, CoverageTestMethodsMixin
class ApiTest(CoverageTest):
@@ -35,7 +35,7 @@ class ApiTest(CoverageTest):
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__"])
+ here = self.clean_files(here, ["*.pyc", "__pycache__", "*$py.class"])
self.assertCountEqual(here, files)
def test_unexecuted_file(self):
@@ -282,17 +282,70 @@ class ApiTest(CoverageTest):
self.check_code1_code2(cov)
def test_start_save_stop(self):
- self.skipTest("Expected failure: https://bitbucket.org/ned/coveragepy/issue/79")
self.make_code1_code2()
cov = coverage.Coverage()
cov.start()
- self.import_local_file("code1")
- cov.save()
- self.import_local_file("code2")
- cov.stop()
-
+ import_local_file("code1") # pragma: nested
+ cov.save() # pragma: nested
+ import_local_file("code2") # pragma: nested
+ cov.stop() # pragma: nested
self.check_code1_code2(cov)
+ def test_start_save_nostop(self):
+ self.make_code1_code2()
+ cov = coverage.Coverage()
+ cov.start()
+ import_local_file("code1") # pragma: nested
+ cov.save() # pragma: nested
+ import_local_file("code2") # pragma: nested
+ self.check_code1_code2(cov) # pragma: nested
+ # Then stop it, or the test suite gets out of whack.
+ cov.stop() # pragma: nested
+
+ def test_two_getdata_only_warn_once(self):
+ self.make_code1_code2()
+ cov = coverage.Coverage(source=["."], omit=["code1.py"])
+ cov.start()
+ import_local_file("code1") # pragma: nested
+ cov.stop() # pragma: nested
+ # We didn't collect any data, so we should get a warning.
+ with self.assert_warnings(cov, ["No data was collected"]):
+ cov.get_data()
+ # But calling get_data a second time with no intervening activity
+ # won't make another warning.
+ with self.assert_warnings(cov, []):
+ cov.get_data()
+
+ def test_two_getdata_only_warn_once_nostop(self):
+ self.make_code1_code2()
+ cov = coverage.Coverage(source=["."], omit=["code1.py"])
+ cov.start()
+ import_local_file("code1") # pragma: nested
+ # We didn't collect any data, so we should get a warning.
+ with self.assert_warnings(cov, ["No data was collected"]): # pragma: nested
+ cov.get_data() # pragma: nested
+ # But calling get_data a second time with no intervening activity
+ # won't make another warning.
+ with self.assert_warnings(cov, []): # pragma: nested
+ cov.get_data() # pragma: nested
+ # Then stop it, or the test suite gets out of whack.
+ cov.stop() # pragma: nested
+
+ def test_two_getdata_warn_twice(self):
+ self.make_code1_code2()
+ cov = coverage.Coverage(source=["."], omit=["code1.py", "code2.py"])
+ cov.start()
+ import_local_file("code1") # pragma: nested
+ # We didn't collect any data, so we should get a warning.
+ with self.assert_warnings(cov, ["No data was collected"]): # pragma: nested
+ cov.save() # pragma: nested
+ import_local_file("code2") # pragma: nested
+ # Calling get_data a second time after tracing some more will warn again.
+ with self.assert_warnings(cov, ["No data was collected"]): # pragma: nested
+ cov.get_data() # pragma: nested
+ # Then stop it, or the test suite gets out of whack.
+ cov.stop() # pragma: nested
+
def make_good_data_files(self):
"""Make some good data files."""
self.make_code1_code2()
@@ -348,6 +401,49 @@ class ApiTest(CoverageTest):
self.assertEqual(statements, [1, 2])
self.assertEqual(missing, [1, 2])
+ def test_warnings(self):
+ self.make_file("hello.py", """\
+ import sys, os
+ print("Hello")
+ """)
+ cov = coverage.Coverage(source=["sys", "xyzzy", "quux"])
+ self.start_import_stop(cov, "hello")
+ cov.get_data()
+
+ out = self.stdout()
+ self.assertIn("Hello\n", out)
+
+ err = self.stderr()
+ self.assertIn(textwrap.dedent("""\
+ Coverage.py warning: Module sys has no Python source. (module-not-python)
+ Coverage.py warning: Module xyzzy was never imported. (module-not-imported)
+ Coverage.py warning: Module quux was never imported. (module-not-imported)
+ Coverage.py warning: No data was collected. (no-data-collected)
+ """), err)
+
+ def test_warnings_suppressed(self):
+ self.make_file("hello.py", """\
+ import sys, os
+ print("Hello")
+ """)
+ self.make_file(".coveragerc", """\
+ [run]
+ disable_warnings = no-data-collected, module-not-imported
+ """)
+ cov = coverage.Coverage(source=["sys", "xyzzy", "quux"])
+ self.start_import_stop(cov, "hello")
+ cov.get_data()
+
+ out = self.stdout()
+ self.assertIn("Hello\n", out)
+
+ err = self.stderr()
+ self.assertIn(textwrap.dedent("""\
+ Coverage.py warning: Module sys has no Python source. (module-not-python)
+ """), err)
+ self.assertNotIn("module-not-imported", err)
+ self.assertNotIn("no-data-collected", err)
+
class NamespaceModuleTest(CoverageTest):
"""Test PEP-420 namespace modules."""
@@ -376,16 +472,14 @@ class UsingModulesMixin(object):
def setUp(self):
super(UsingModulesMixin, self).setUp()
- old_dir = os.getcwd()
- os.chdir(self.nice_file(os.path.dirname(__file__), 'modules'))
- self.addCleanup(os.chdir, old_dir)
+ self.chdir(self.nice_file(os.path.dirname(__file__), 'modules'))
# Parent class saves and restores sys.path, we can just modify it.
sys.path.append(".")
sys.path.append("../moremodules")
-class OmitIncludeTestsMixin(UsingModulesMixin):
+class OmitIncludeTestsMixin(UsingModulesMixin, CoverageTestMethodsMixin):
"""Test methods for coverage methods taking include and omit."""
def filenames_in(self, summary, filenames):
@@ -461,13 +555,20 @@ class SourceOmitIncludeTest(OmitIncludeTestsMixin, CoverageTest):
summary[k[:-3]] = v
return summary
- def test_source_package(self):
+ def test_source_package_as_dir(self):
+ # pkg1 is a directory, since we cd'd into tests/modules in setUp.
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_as_package(self):
+ lines = self.coverage_usepkgs(source=["pkg1.sub"])
+ self.filenames_not_in(lines, "p2a p2b othera otherb osa osb")
+ # Because source= was specified, we do search for unexecuted files.
+ self.assertEqual(lines['runmod3'], 0)
+
def test_source_package_dotted(self):
lines = self.coverage_usepkgs(source=["pkg1.p1b"])
self.filenames_in(lines, "p1b")