Skip to content

Commit a9ea7b7

Browse files
committed
Implement error/warning for the bad dynamic_context being set in config.
1 parent c299e01 commit a9ea7b7

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

Diff for: src/pytest_cov/engine.py

+27
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import random
88
import socket
99
import sys
10+
import warnings
1011
from io import StringIO
1112
from pathlib import Path
1213

@@ -15,6 +16,11 @@
1516
from coverage.sqldata import filename_suffix
1617

1718
from .embed import cleanup
19+
from .plugin import PytestCovWarning
20+
21+
22+
class BrokenCovConfigError(Exception):
23+
pass
1824

1925

2026
class _NullFile:
@@ -225,6 +231,10 @@ def summary(self, stream):
225231
return total
226232

227233

234+
class CentralCovContextWarning(PytestCovWarning):
235+
pass
236+
237+
228238
class Central(CovController):
229239
"""Implementation for centralised operation."""
230240

@@ -238,6 +248,13 @@ def start(self):
238248
data_suffix=_data_suffix('c'),
239249
config_file=self.cov_config,
240250
)
251+
if self.cov.config.dynamic_context == 'test_function':
252+
message = (
253+
'Detected dynamic_context=test_function in coverage configuration. '
254+
'This is unnecessary as this plugin provides the more complete --cov-context option.'
255+
)
256+
warnings.warn(CentralCovContextWarning(message), stacklevel=1)
257+
241258
self.combining_cov = coverage.Coverage(
242259
source=self.cov_source,
243260
branch=self.cov_branch,
@@ -269,6 +286,10 @@ def finish(self):
269286
self.node_descs.add(node_desc)
270287

271288

289+
class DistCovError(Exception):
290+
pass
291+
292+
272293
class DistMaster(CovController):
273294
"""Implementation for distributed master."""
274295

@@ -282,6 +303,12 @@ def start(self):
282303
data_suffix=_data_suffix('m'),
283304
config_file=self.cov_config,
284305
)
306+
if self.cov.config.dynamic_context == 'test_function':
307+
raise DistCovError(
308+
'Detected dynamic_context=test_function in coverage configuration. '
309+
'This is known to cause issues when using xdist, see: https://github.com/pytest-dev/pytest-cov/issues/604\n'
310+
'It is recommended to use --cov-context instead.'
311+
)
285312
self.cov._warn_no_data = False
286313
self.cov._warn_unimported_source = False
287314
self.cov._warn_preimported_source = False

Diff for: tests/test_pytest_cov.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1673,11 +1673,15 @@ def test_dynamic_context(pytester, testdir, opts, prop):
16731673
{prop.conf}
16741674
""")
16751675
result = testdir.runpytest('-v', f'--cov={script.dirpath()}', script, *opts.split() + prop.args)
1676-
result.stdout.fnmatch_lines(
1677-
[
1678-
f'test_1* {prop.result}*',
1679-
]
1680-
)
1676+
if opts:
1677+
result.stderr.fnmatch_lines(['pytest_cov.engine.DistCovError: Detected dynamic_context=test_function*'])
1678+
else:
1679+
result.stdout.fnmatch_lines(
1680+
[
1681+
'* CentralCovContextWarning: Detected dynamic_context=test_function*',
1682+
f'test_1* {prop.result}*',
1683+
]
1684+
)
16811685

16821686

16831687
@xdist_params

0 commit comments

Comments
 (0)