diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2021-05-30 17:39:20 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2021-05-30 18:03:05 -0400 |
commit | 30c023b5b74f9c798645cbb3f35362ae046a4c25 (patch) | |
tree | e86df1a4c044ec9b2919068297dfd91a382eeb84 /tests/test_testing.py | |
parent | 22fe2eb167a18dda8fd3e14cbf9166a1c7331fb9 (diff) | |
download | python-coveragepy-git-30c023b5b74f9c798645cbb3f35362ae046a4c25.tar.gz |
feat: warnings are now real warnings
This makes coverage warnings visible when running test suites under
pytest. But it also means some uninteresting warnings would show up in
our own test suite, so we had to catch or suppress those.
Diffstat (limited to 'tests/test_testing.py')
-rw-r--r-- | tests/test_testing.py | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/tests/test_testing.py b/tests/test_testing.py index 3a563efe..7219ff0b 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -7,16 +7,18 @@ import datetime import os import re import sys +import warnings import pytest import coverage from coverage import tomlconfig +from coverage.exceptions import CoverageWarning from coverage.files import actual_path from tests.coveragetest import CoverageTest from tests.helpers import ( - arcs_to_arcz_repr, arcz_to_arcs, assert_count_equal, + arcs_to_arcz_repr, arcz_to_arcs, assert_count_equal, assert_coverage_warnings, CheckUniqueFilenames, re_lines, re_line, without_module, ) @@ -383,3 +385,57 @@ class ArczTest(CoverageTest): ]) def test_arcs_to_arcz_repr(self, arcs, arcz_repr): assert arcs_to_arcz_repr(arcs) == arcz_repr + + +class AssertCoverageWarningsTest(CoverageTest): + """Tests of assert_coverage_warnings""" + + def test_one_warning(self): + with pytest.warns(Warning) as warns: + warnings.warn("Hello there", category=CoverageWarning) + assert_coverage_warnings(warns, "Hello there") + + 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_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_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_number_too_many(self): + with pytest.warns(Warning) as warns: + warnings.warn("The first", category=CoverageWarning) + warnings.warn("The second", category=CoverageWarning) + with pytest.raises(AssertionError): + assert_coverage_warnings(warns, "The first", "The second", "The third") + + 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_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_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")) |