diff --git a/src/pytest_cov/plugin.py b/src/pytest_cov/plugin.py index 9f5c3f76..cf03a1df 100644 --- a/src/pytest_cov/plugin.py +++ b/src/pytest_cov/plugin.py @@ -232,7 +232,15 @@ def __init__(self, options: argparse.Namespace, pluginmanager, start=True, no_co self.options = options self._wrote_heading = False - is_dist = getattr(options, 'numprocesses', False) or getattr(options, 'distload', False) or getattr(options, 'dist', 'no') != 'no' + # The distribution mode only takes effect when xdist has execution + # environments. ``--dist`` may be configured without ``-n``; in that + # case pytest still runs in the current process and needs the central + # controller to configure subprocess coverage. + is_dist = ( + bool(getattr(options, 'numprocesses', False)) + or getattr(options, 'distload', False) + or (getattr(options, 'dist', 'no') != 'no' and bool(getattr(options, 'tx', ()))) + ) if getattr(options, 'no_cov', False): self._disabled = True return diff --git a/tests/test_pytest_cov.py b/tests/test_pytest_cov.py index b291f432..d57b63f0 100644 --- a/tests/test_pytest_cov.py +++ b/tests/test_pytest_cov.py @@ -1112,6 +1112,30 @@ def test_foo(): assert result.ret == 0 +def test_dist_option_without_numprocesses_uses_central_controller(monkeypatch): + started = [] + monkeypatch.setattr( + pytest_cov.plugin.CovPlugin, + 'start', + lambda self, controller_cls, config=None, nodeid=None: started.append( + controller_cls.__name__ + ), + ) + options = SimpleNamespace( + cov_report={}, + cov_source=['.'], + dist='loadgroup', + distload=False, + no_cov=False, + numprocesses=None, + tx=[], + ) + + pytest_cov.plugin.CovPlugin(options, None) + + assert started == ['Central'] + + @pytest.mark.skipif('sys.platform == "win32" and platform.python_implementation() == "PyPy"') def test_dist_subprocess_collocated(testdir): testdir.makepyprojecttoml(