Skip to content

Commit

Permalink
Use modern approach to specify hook options
Browse files Browse the repository at this point in the history
The old way using marks is being deprecated in pytest 7.2:

pytest-dev/pytest#9118
  • Loading branch information
nicoddemus authored and ionelmc committed Sep 22, 2022
1 parent 00713b3 commit b077753
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
-------------------
Expand Down
7 changes: 0 additions & 7 deletions src/pytest_cov/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/pytest_cov/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -253,23 +253,23 @@ 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.
Mark this hook as optional in case xdist is not installed.
"""
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.
Mark this hook as optional in case xdist is not installed.
"""
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)
Expand All @@ -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

Expand Down Expand Up @@ -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', ())):
Expand Down

0 comments on commit b077753

Please sign in to comment.