From a59548f3adcb757ea8afcb40d8d849af49f6e925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionel=20Cristian=20M=C4=83rie=C8=99?= Date: Sat, 5 Apr 2025 16:22:33 +0300 Subject: [PATCH 1/3] Allow the context plugin to check if the controller is running or not. Fixes #685. --- src/pytest_cov/engine.py | 15 +++++++++++++++ src/pytest_cov/plugin.py | 15 +++++++++------ tests/test_pytest_cov.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/pytest_cov/engine.py b/src/pytest_cov/engine.py index 5acafec0..64f5cafe 100644 --- a/src/pytest_cov/engine.py +++ b/src/pytest_cov/engine.py @@ -87,6 +87,7 @@ def __init__(self, options: argparse.Namespace, config: Union[None, object], nod self.failed_workers = [] self.topdir = os.fspath(Path.cwd()) self.is_collocated = None + self.started = False @contextlib.contextmanager def ensure_topdir(self): @@ -97,6 +98,7 @@ def ensure_topdir(self): @_ensure_topdir def pause(self): + self.started = False self.cov.stop() self.unset_env() @@ -104,6 +106,13 @@ def pause(self): def resume(self): self.cov.start() self.set_env() + self.started = True + + def start(self): + self.started = True + + def finish(self): + self.started = False @_ensure_topdir def set_env(self): @@ -295,9 +304,12 @@ def start(self): self.cov.start() self.set_env() + super().start() + @_ensure_topdir def finish(self): """Stop coverage, save data to file and set the list of coverage objects to report on.""" + super().finish() self.unset_env() self.cov.stop() @@ -440,10 +452,13 @@ def start(self): ) self.cov.start() self.set_env() + super().start() @_ensure_topdir def finish(self): """Stop coverage and send relevant info back to the master.""" + super().finish() + self.unset_env() self.cov.stop() diff --git a/src/pytest_cov/plugin.py b/src/pytest_cov/plugin.py index b131742b..aca89403 100644 --- a/src/pytest_cov/plugin.py +++ b/src/pytest_cov/plugin.py @@ -284,7 +284,7 @@ def pytest_sessionstart(self, session): self.start(engine.Central) if self.options.cov_context == 'test': - session.config.pluginmanager.register(TestContextPlugin(self.cov_controller.cov), '_cov_contexts') + session.config.pluginmanager.register(TestContextPlugin(self.cov_controller), '_cov_contexts') @pytest.hookimpl(optionalhook=True) def pytest_configure_node(self, node): @@ -408,8 +408,10 @@ def pytest_runtest_call(self, item): class TestContextPlugin: - def __init__(self, cov): - self.cov = cov + cov_controller: 'CovController' + + def __init__(self, cov_controller): + self.cov_controller = cov_controller def pytest_runtest_setup(self, item): self.switch_context(item, 'setup') @@ -421,9 +423,10 @@ def pytest_runtest_call(self, item): self.switch_context(item, 'run') def switch_context(self, item, when): - context = f'{item.nodeid}|{when}' - self.cov.switch_context(context) - os.environ['COV_CORE_CONTEXT'] = context + if self.cov_controller.started: + context = f'{item.nodeid}|{when}' + self.cov_controller.cov.switch_context(context) + os.environ['COV_CORE_CONTEXT'] = context @pytest.fixture diff --git a/tests/test_pytest_cov.py b/tests/test_pytest_cov.py index 81184f8b..15348ce2 100644 --- a/tests/test_pytest_cov.py +++ b/tests/test_pytest_cov.py @@ -1942,6 +1942,36 @@ def test_contexts_not_supported(testdir): assert result.ret != 0 +def test_contexts_no_cover(testdir): + script = testdir.makepyfile(""" +import pytest + +def foobar(): + return 1 + +def test_with_coverage(): + foobar() + +@pytest.mark.no_cover() +def test_without_coverage(): + foobar() +""") + result = testdir.runpytest( + '-v', + '--cov-context=test', + '--cov=test_contexts_no_cover', + script, + ) + result.stdout.fnmatch_lines( + [ + 'test_contexts_no_cover.py 8 1 88%', + 'TOTAL 8 1 88%', + ] + ) + assert result.stderr.lines == [] + assert result.ret == 0 + + def test_issue_417(testdir): # https://github.com/pytest-dev/pytest-cov/issues/417 whatever = testdir.maketxtfile(whatever='') From 7f2781b47fc9bd4a8e94ff86b4f69f5959c3d907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionel=20Cristian=20M=C4=83rie=C8=99?= Date: Sat, 5 Apr 2025 16:33:28 +0300 Subject: [PATCH 2/3] Update changelog. --- CHANGELOG.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7de457fd..22e585d5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,11 @@ Changelog ========= +6.1.1 (2025-04-05) +------------------ + +* Fixed breakage that occurs when ``--cov-context`` and the ``no_cover`` marker are used together. + 6.1.0 (2025-04-01) ------------------ From 9463242e3a7bc18a56b8f18c01b4dfb50087e5ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionel=20Cristian=20M=C4=83rie=C8=99?= Date: Sat, 5 Apr 2025 17:07:23 +0300 Subject: [PATCH 3/3] =?UTF-8?q?Bump=20version:=206.1.0=20=E2=86=92=206.1.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bumpversion.cfg | 2 +- .cookiecutterrc | 2 +- README.rst | 4 ++-- docs/conf.py | 2 +- setup.py | 2 +- src/pytest_cov/__init__.py | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 98e4fe0e..2ce50997 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 6.1.0 +current_version = 6.1.1 commit = True tag = True diff --git a/.cookiecutterrc b/.cookiecutterrc index 46b134a9..3703b848 100644 --- a/.cookiecutterrc +++ b/.cookiecutterrc @@ -40,7 +40,7 @@ default_context: sphinx_doctest: 'no' sphinx_theme: sphinx-py3doc-enhanced-theme test_matrix_separate_coverage: 'no' - version: 6.1.0 + version: 6.1.1 version_manager: bump2version website: http://blog.ionelmc.ro year_from: '2010' diff --git a/README.rst b/README.rst index d0b3464d..701de60c 100644 --- a/README.rst +++ b/README.rst @@ -39,9 +39,9 @@ Overview :alt: Supported implementations :target: https://pypi.org/project/pytest-cov -.. |commits-since| image:: https://img.shields.io/github/commits-since/pytest-dev/pytest-cov/v6.1.0.svg +.. |commits-since| image:: https://img.shields.io/github/commits-since/pytest-dev/pytest-cov/v6.1.1.svg :alt: Commits since latest release - :target: https://github.com/pytest-dev/pytest-cov/compare/v6.1.0...master + :target: https://github.com/pytest-dev/pytest-cov/compare/v6.1.1...master .. end-badges diff --git a/docs/conf.py b/docs/conf.py index f73bf5c2..24cd51ab 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -21,7 +21,7 @@ year = '2010-2024' author = 'pytest-cov contributors' copyright = f'{year}, {author}' -version = release = '6.1.0' +version = release = '6.1.1' pygments_style = 'trac' templates_path = ['.'] diff --git a/setup.py b/setup.py index d773a455..f85db87f 100755 --- a/setup.py +++ b/setup.py @@ -77,7 +77,7 @@ def run(self): setup( name='pytest-cov', - version='6.1.0', + version='6.1.1', license='MIT', description='Pytest plugin for measuring coverage.', long_description='{}\n{}'.format(read('README.rst'), re.sub(':[a-z]+:`~?(.*?)`', r'``\1``', read('CHANGELOG.rst'))), diff --git a/src/pytest_cov/__init__.py b/src/pytest_cov/__init__.py index 716bc6a9..ca111a38 100644 --- a/src/pytest_cov/__init__.py +++ b/src/pytest_cov/__init__.py @@ -1,6 +1,6 @@ """pytest-cov: avoid already-imported warning: PYTEST_DONT_REWRITE.""" -__version__ = '6.1.0' +__version__ = '6.1.1' import pytest