diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2021-12-09 12:24:12 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2022-02-04 06:38:50 -0500 |
commit | 340c56ba503f0000bf684110c2a8c8ade9d5d60b (patch) | |
tree | 9713c428526e29df9a113ae20a41ed5e9c98ff12 /tests | |
parent | cf712c665dffcd2e1e939b9eb079974449437828 (diff) | |
download | python-coveragepy-git-340c56ba503f0000bf684110c2a8c8ade9d5d60b.tar.gz |
refactor(test): a context manager to swallow warnings
Diffstat (limited to 'tests')
-rw-r--r-- | tests/conftest.py | 5 | ||||
-rw-r--r-- | tests/helpers.py | 12 | ||||
-rw-r--r-- | tests/test_oddball.py | 5 | ||||
-rw-r--r-- | tests/test_plugins.py | 7 |
4 files changed, 24 insertions, 5 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 16999d96..39f39e25 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -45,6 +45,11 @@ def set_warnings(): message=r".*imp module is deprecated in favour of importlib", ) + warnings.filterwarnings( + "ignore", + category=pytest.PytestRemovedIn8Warning, + ) + if env.PYPY: # pypy3 warns about unclosed files a lot. warnings.filterwarnings("ignore", r".*unclosed file", category=ResourceWarning) diff --git a/tests/helpers.py b/tests/helpers.py index 29464d1c..890bd7c5 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -12,6 +12,7 @@ import re import shutil import subprocess import textwrap +import warnings from unittest import mock @@ -308,3 +309,14 @@ def assert_coverage_warnings(warns, *msgs): assert expected.search(actual), f"{actual!r} didn't match {expected!r}" else: assert expected == actual + + +@contextlib.contextmanager +def swallow_warnings(message=r".", category=CoverageWarning): + """Swallow particular warnings. + + It's OK if they happen, or if they don't happen. Just ignore them. + """ + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=category, message=message) + yield diff --git a/tests/test_oddball.py b/tests/test_oddball.py index e2c17517..9536c121 100644 --- a/tests/test_oddball.py +++ b/tests/test_oddball.py @@ -16,6 +16,7 @@ from coverage.files import abs_file from coverage.misc import import_local_file from tests.coveragetest import CoverageTest +from tests.helpers import swallow_warnings from tests import osinfo @@ -82,7 +83,7 @@ class RecursionTest(CoverageTest): def test_long_recursion(self): # We can't finish a very deep recursion, but we don't crash. with pytest.raises(RuntimeError): - with pytest.warns(None): + with swallow_warnings("Trace function changed, measurement is likely wrong: None"): self.check_coverage("""\ def recur(n): if n == 0: @@ -119,7 +120,7 @@ class RecursionTest(CoverageTest): """) cov = coverage.Coverage() - with pytest.warns(None): + with swallow_warnings("Trace function changed, measurement is likely wrong: None"): self.start_import_stop(cov, "recur") pytrace = (cov._collector.tracer_name() == "PyTracer") diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 45b3bc9e..edf849f6 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -21,7 +21,7 @@ from coverage.misc import import_local_file import coverage.plugin from tests.coveragetest import CoverageTest -from tests.helpers import CheckUniqueFilenames +from tests.helpers import CheckUniqueFilenames, swallow_warnings class FakeConfig: @@ -194,8 +194,9 @@ class PluginTest(CoverageTest): cov = coverage.Coverage(debug=["sys"]) cov._debug_file = debug_out cov.set_option("run:plugins", ["plugin_sys_info"]) - with pytest.warns(None): - # Catch warnings so we don't see "plugins aren't supported on PyTracer" + with swallow_warnings( + r"Plugin file tracers \(plugin_sys_info.Plugin\) aren't supported with PyTracer" + ): cov.start() cov.stop() # pragma: nested |