def test_wrong_number_too_few(self): with pytest.warns(Warning) as warns: warnings.warn("The first", category=CoverageWarning) warnings.warn("The second", category=CoverageWarning) warnings.warn("The third", category=CoverageWarning) with pytest.raises(AssertionError): assert_coverage_warnings(warns, "The first", "The second")
def test_warn_once(self): with pytest.warns(Warning) as warns: cov = coverage.Coverage() cov.load() cov._warn("Warning, warning 1!", slug="bot", once=True) cov._warn("Warning, warning 2!", slug="bot", once=True) assert_coverage_warnings(warns, "Warning, warning 1! (bot)")
def test_dynamic_context_conflict(self): cov = coverage.Coverage(source=["."]) cov.set_option("run:dynamic_context", "test_function") cov.start() with pytest.warns(Warning) as warns: # Switch twice, but only get one warning. cov.switch_context("test1") # pragma: nested cov.switch_context("test2") # pragma: nested cov.stop() # pragma: nested assert_coverage_warnings(warns, "Conflicting dynamic contexts (dynamic-conflict)")
def test_no_source(self): # Written while investigating a bug, might as well keep it. # https://github.com/nedbat/coveragepy/issues/208 self.make_file("innocuous.py", "a = 4") cov = coverage.Coverage() self.start_import_stop(cov, "innocuous") os.remove("innocuous.py") with pytest.warns(Warning) as warns: cov.xml_report(ignore_errors=True) assert_coverage_warnings( warns, re.compile(r"Couldn't parse '.*innocuous.py'. \(couldnt-parse\)"), ) self.assert_exists("coverage.xml")
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 """) with pytest.warns(Warning) as warns: cov = coverage.Coverage(source=["sys", "xyzzy", "quux"]) self.start_import_stop(cov, "hello") cov.get_data() assert "Hello\n" == self.stdout() assert_coverage_warnings(warns, "Module sys has no Python source. (module-not-python)")
def test_dotpy_not_python_ignored(self): self.make_file("main.py", "import innocuous") self.make_file("innocuous.py", "a = 2") cov = coverage.Coverage() self.start_import_stop(cov, "main") self.make_file("innocuous.py", "<h1>This isn't python!</h1>") with pytest.warns(Warning) as warns: cov.html_report(ignore_errors=True) assert_coverage_warnings( warns, re.compile(r"Couldn't parse Python file '.*innocuous.py' \(couldnt-parse\)"), ) self.assert_exists("htmlcov/index.html") # This would be better as a glob, if the HTML layout changes: self.assert_doesnt_exist("htmlcov/innocuous.html")
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, # though we still get a warning. self.make_file("mycode.py", "This isn't python at all!") self.make_data_file(lines={"mycode.py": [1]}) cov = coverage.Coverage() cov.load() with pytest.raises(NoDataError, match="No data to report."): with pytest.warns(Warning) as warns: self.get_report(cov, morfs=["mycode.py"], ignore_errors=True) assert_coverage_warnings( warns, re.compile( r"Couldn't parse Python file '.*[/\\]mycode.py' \(couldnt-parse\)" ), )
def test_deep_source(self): # When using source=, the XML report needs to mention those directories # in the <source> elements. # https://github.com/nedbat/coveragepy/issues/439 self.make_file("src/main/foo.py", "a = 1") self.make_file("also/over/there/bar.py", "b = 2") cov = coverage.Coverage( source=["src/main", "also/over/there", "not/really"]) cov.start() mod_foo = import_local_file("foo", "src/main/foo.py") # pragma: nested mod_bar = import_local_file("bar", "also/over/there/bar.py") # pragma: nested cov.stop() # pragma: nested with pytest.warns(Warning) as warns: cov.xml_report([mod_foo, mod_bar]) assert_coverage_warnings( warns, "Module not/really was never imported. (module-not-imported)", ) dom = ElementTree.parse("coverage.xml") self.assert_source(dom, "src/main") self.assert_source(dom, "also/over/there") sources = dom.findall(".//source") assert len(sources) == 2 foo_class = dom.findall(".//class[@name='foo.py']") assert len(foo_class) == 1 assert foo_class[0].attrib == { 'branch-rate': '0', 'complexity': '0', 'filename': 'foo.py', 'line-rate': '1', 'name': 'foo.py', } bar_class = dom.findall(".//class[@name='bar.py']") assert len(bar_class) == 1 assert bar_class[0].attrib == { 'branch-rate': '0', 'complexity': '0', 'filename': 'bar.py', 'line-rate': '1', 'name': 'bar.py', }
def test_warnings(self): self.make_file("hello.py", """\ import sys, os print("Hello") """) with pytest.warns(Warning) as warns: cov = coverage.Coverage(source=["sys", "xyzzy", "quux"]) self.start_import_stop(cov, "hello") cov.get_data() assert "Hello\n" == self.stdout() assert_coverage_warnings( warns, "Module sys has no Python source. (module-not-python)", "Module xyzzy was never imported. (module-not-imported)", "Module quux was never imported. (module-not-imported)", "No data was collected. (no-data-collected)", )
def test_completely_zero_reporting(self): # https://github.com/nedbat/coveragepy/issues/884 # If nothing was measured, the file-touching didn't happen properly. self.make_file("foo/bar.py", "print('Never run')") self.make_file("test.py", "assert True") with pytest.warns(Warning) as warns: cov = coverage.Coverage(source=["foo"]) self.start_import_stop(cov, "test") cov.report() assert_coverage_warnings(warns, "No data was collected. (no-data-collected)") # Name Stmts Miss Cover # -------------------------------- # foo/bar.py 1 1 0% # -------------------------------- # TOTAL 1 1 0% last = self.last_line_squeezed(self.stdout()) assert "TOTAL 1 1 0%" == last
def test_regex_doesnt_match(self): with pytest.warns(Warning) as warns: warnings.warn("The first", category=CoverageWarning) with pytest.raises(AssertionError): assert_coverage_warnings(warns, re.compile("second"))
def test_regex_matches(self): with pytest.warns(Warning) as warns: warnings.warn("The first", category=CoverageWarning) assert_coverage_warnings(warns, re.compile("f?rst"))
def test_wrong_message(self): with pytest.warns(Warning) as warns: warnings.warn("Goodbye", category=CoverageWarning) with pytest.raises(AssertionError): assert_coverage_warnings(warns, "Hello there")
def test_wrong_type(self): with pytest.warns(Warning) as warns: warnings.warn("Not ours", category=Warning) with pytest.raises(AssertionError): assert_coverage_warnings(warns, "Not ours")
def test_many_warnings(self): with pytest.warns(Warning) as warns: warnings.warn("The first", category=CoverageWarning) warnings.warn("The second", category=CoverageWarning) warnings.warn("The third", category=CoverageWarning) assert_coverage_warnings(warns, "The first", "The second", "The third")
def test_one_warning(self): with pytest.warns(Warning) as warns: warnings.warn("Hello there", category=CoverageWarning) assert_coverage_warnings(warns, "Hello there")