diff --git a/AUTHORS.rst b/AUTHORS.rst index 561367d7..2e598ecb 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -70,3 +70,4 @@ Authors * Markéta Machová - https://github.com/MeggyCal * xuu33030 - https://github.com/xuu33030 * dly - https://github.com/xlyy1 +* Jean-Francois Paquin - https://github.com/jf-paquin-web diff --git a/CHANGELOG.rst b/CHANGELOG.rst index cf403789..e4e1309d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -16,6 +16,8 @@ Unreleased from a subdirectory no longer ignores the project's ``pyproject.toml`` / ``setup.cfg`` / ``tox.ini`` / ``.coveragerc``. See `#692 `_. +* Fixed xdist workers leaving a stray coverage collector running (started because workers inherit the master's ``-n`` option). + It got resumed whenever the worker's coverage was paused, so ``no_cover`` tests still ran under a tracer. 7.1.0 (2026-03-21) ------------------ diff --git a/src/pytest_cov/plugin.py b/src/pytest_cov/plugin.py index 9f23a890..99fac18b 100644 --- a/src/pytest_cov/plugin.py +++ b/src/pytest_cov/plugin.py @@ -312,6 +312,11 @@ def pytest_sessionstart(self, session): self.pid = os.getpid() if self._is_worker(session): + if self.cov_controller is not None: + # xdist builds the worker config from the master's args (it resets numprocesses only afterwards), + # so __init__ has already started a DistMaster here. Stop it or it would be left on coverage's + # collector stack and get resumed whenever the worker's coverage is paused (eg: no_cover). + self.cov_controller.cov.stop() nodeid = session.config.workerinput.get('workerid', session.nodeid) self.start(engine.DistWorker, session.config, nodeid) elif not self._started: diff --git a/tests/test_pytest_cov.py b/tests/test_pytest_cov.py index 098dc3fd..e7f5381f 100644 --- a/tests/test_pytest_cov.py +++ b/tests/test_pytest_cov.py @@ -1560,6 +1560,31 @@ def test_basic(): result.stdout.fnmatch_lines(['mod* 2 * 1 * 50% * 2']) +@xdist_params +def test_no_cover_marker_stops_tracing(testdir, opts): + script = testdir.makepyfile( + """ +import pytest +from coverage.collector import Collector + +def record(name): + with open(name + '.txt', 'w') as fh: + fh.write(str(len(Collector._collectors))) + +def test_normal(): + record('normal') + +@pytest.mark.no_cover +def test_no_cover(): + record('no_cover') +""" + ) + result = testdir.runpytest('-v', f'--cov={script.dirpath()}', script, *opts.split()) + assert result.ret == 0 + assert testdir.tmpdir.join('normal.txt').read() == '1' + assert testdir.tmpdir.join('no_cover.txt').read() == '0' + + def test_no_cover_marker_with_no_cov(testdir): script = testdir.makepyfile( """