From 675f0d079be95d5262924b0997f7640aae054559 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 31 Jul 2022 12:52:32 -0300 Subject: [PATCH 1/2] Use modern approach to specify hook options The old way using marks is being deprecated in pytest 7.2: https://github.com/pytest-dev/pytest/pull/9118 --- CHANGELOG.rst | 2 ++ src/pytest_cov/compat.py | 7 ------- src/pytest_cov/plugin.py | 10 +++++----- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 6dbf514b..95c9eb9e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -22,6 +22,8 @@ Changelog parallel = true sigterm = true +* Use modern way to specify hook options to avoid deprecation warnings with pytest >=7.2. + 3.0.0 (2021-10-04) ------------------- diff --git a/src/pytest_cov/compat.py b/src/pytest_cov/compat.py index f422f25c..614419cb 100644 --- a/src/pytest_cov/compat.py +++ b/src/pytest_cov/compat.py @@ -3,17 +3,10 @@ except ImportError: from io import StringIO -import pytest StringIO # pyflakes, this is for re-export -if hasattr(pytest, 'hookimpl'): - hookwrapper = pytest.hookimpl(hookwrapper=True) -else: - hookwrapper = pytest.mark.hookwrapper - - class SessionWrapper: def __init__(self, session): self._session = session diff --git a/src/pytest_cov/plugin.py b/src/pytest_cov/plugin.py index 252439ed..bf42971e 100644 --- a/src/pytest_cov/plugin.py +++ b/src/pytest_cov/plugin.py @@ -133,7 +133,7 @@ def _prepare_cov_source(cov_source): return None if True in cov_source else [path for path in cov_source if path is not True] -@pytest.mark.tryfirst +@pytest.hookimpl(tryfirst=True) def pytest_load_initial_conftests(early_config, parser, args): options = early_config.known_args_namespace no_cov = options.no_cov_should_warn = False @@ -253,6 +253,7 @@ def pytest_sessionstart(self, session): if self.options.cov_context == 'test': session.config.pluginmanager.register(TestContextPlugin(self.cov_controller.cov), '_cov_contexts') + @pytest.hookimpl(optionalhook=True) def pytest_configure_node(self, node): """Delegate to our implementation. @@ -260,8 +261,8 @@ def pytest_configure_node(self, node): """ if not self._disabled: self.cov_controller.configure_node(node) - pytest_configure_node.optionalhook = True + @pytest.hookimpl(optionalhook=True) def pytest_testnodedown(self, node, error): """Delegate to our implementation. @@ -269,7 +270,6 @@ def pytest_testnodedown(self, node, error): """ if not self._disabled: self.cov_controller.testnodedown(node, error) - pytest_testnodedown.optionalhook = True def _should_report(self): return not (self.failed and self.options.no_cov_on_fail) @@ -280,7 +280,7 @@ def _failed_cov_total(self): # we need to wrap pytest_runtestloop. by the time pytest_sessionfinish # runs, it's too late to set testsfailed - @compat.hookwrapper + @pytest.hookimpl(hookwrapper=True) def pytest_runtestloop(self, session): yield @@ -356,7 +356,7 @@ def pytest_runtest_setup(self, item): def pytest_runtest_teardown(self, item): embed.cleanup() - @compat.hookwrapper + @pytest.hookimpl(hookwrapper=True) def pytest_runtest_call(self, item): if (item.get_closest_marker('no_cover') or 'no_cover' in getattr(item, 'fixturenames', ())): From c8d4963dd59f358d017c3857275bf5b070f7a3d7 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 31 Jul 2022 12:57:39 -0300 Subject: [PATCH 2/2] Fix flake8 error --- tests/test_pytest_cov.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_pytest_cov.py b/tests/test_pytest_cov.py index 4402fc7b..10d2bd58 100644 --- a/tests/test_pytest_cov.py +++ b/tests/test_pytest_cov.py @@ -1761,8 +1761,11 @@ def bad_init(): monkeypatch.setattr(sys, 'stderr', buff) monkeypatch.setitem(os.environ, 'COV_CORE_SOURCE', 'foobar') exec(payload) - assert buff.getvalue() == '''pytest-cov: Failed to setup subprocess coverage. Environ: {'COV_CORE_SOURCE': 'foobar'} Exception: SpecificError() -''' + expected = ( + "pytest-cov: Failed to setup subprocess coverage. " + "Environ: {'COV_CORE_SOURCE': 'foobar'} Exception: SpecificError()\n" + ) + assert buff.getvalue() == expected def test_double_cov(testdir):