diff --git a/src/pytest_cov/engine.py b/src/pytest_cov/engine.py index 88d2b35..73e2f8d 100644 --- a/src/pytest_cov/engine.py +++ b/src/pytest_cov/engine.py @@ -7,6 +7,7 @@ import random import socket import sys +import warnings from io import StringIO from pathlib import Path @@ -15,6 +16,11 @@ from coverage.sqldata import filename_suffix from .embed import cleanup +from .plugin import PytestCovWarning + + +class BrokenCovConfigError(Exception): + pass class _NullFile: @@ -225,6 +231,10 @@ def summary(self, stream): return total +class CentralCovContextWarning(PytestCovWarning): + pass + + class Central(CovController): """Implementation for centralised operation.""" @@ -238,6 +248,13 @@ def start(self): data_suffix=_data_suffix('c'), config_file=self.cov_config, ) + if self.cov.config.dynamic_context == 'test_function': + message = ( + 'Detected dynamic_context=test_function in coverage configuration. ' + 'This is unnecessary as this plugin provides the more complete --cov-context option.' + ) + warnings.warn(CentralCovContextWarning(message), stacklevel=1) + self.combining_cov = coverage.Coverage( source=self.cov_source, branch=self.cov_branch, @@ -269,6 +286,10 @@ def finish(self): self.node_descs.add(node_desc) +class DistCovError(Exception): + pass + + class DistMaster(CovController): """Implementation for distributed master.""" @@ -282,6 +303,12 @@ def start(self): data_suffix=_data_suffix('m'), config_file=self.cov_config, ) + if self.cov.config.dynamic_context == 'test_function': + raise DistCovError( + 'Detected dynamic_context=test_function in coverage configuration. ' + 'This is known to cause issues when using xdist, see: https://github.com/pytest-dev/pytest-cov/issues/604\n' + 'It is recommended to use --cov-context instead.' + ) self.cov._warn_no_data = False self.cov._warn_unimported_source = False self.cov._warn_preimported_source = False diff --git a/tests/test_pytest_cov.py b/tests/test_pytest_cov.py index 9f06497..2c9d426 100644 --- a/tests/test_pytest_cov.py +++ b/tests/test_pytest_cov.py @@ -1673,11 +1673,15 @@ def test_dynamic_context(pytester, testdir, opts, prop): {prop.conf} """) result = testdir.runpytest('-v', f'--cov={script.dirpath()}', script, *opts.split() + prop.args) - result.stdout.fnmatch_lines( - [ - f'test_1* {prop.result}*', - ] - ) + if opts: + result.stderr.fnmatch_lines(['pytest_cov.engine.DistCovError: Detected dynamic_context=test_function*']) + else: + result.stdout.fnmatch_lines( + [ + '* CentralCovContextWarning: Detected dynamic_context=test_function*', + f'test_1* {prop.result}*', + ] + ) @xdist_params