diff --git a/src/pytest_cov/engine.py b/src/pytest_cov/engine.py index a6a465fa..17d170dd 100644 --- a/src/pytest_cov/engine.py +++ b/src/pytest_cov/engine.py @@ -55,6 +55,10 @@ class CovController: def __init__(self, options: argparse.Namespace, config: Union[None, object], nodeid: Union[None, str]): """Get some common config used by multiple derived classes.""" self.cov_source = options.cov_source + # An explicit --cov=... must narrow measurement to exactly what was asked for. + # Passing source_pkgs=[] overrides any source_pkgs in the coverage config, which + # would otherwise be added to the sources and widen the report again. + self.cov_source_pkgs = None if options.cov_source is None else [] self.cov_report = options.cov_report self.cov_config = options.cov_config self.cov_append = options.cov_append @@ -236,6 +240,7 @@ class Central(CovController): def start(self): self.cov = coverage.Coverage( source=self.cov_source, + source_pkgs=self.cov_source_pkgs, branch=self.cov_branch, data_suffix=True, config_file=self.cov_config, @@ -249,6 +254,7 @@ def start(self): self.combining_cov = coverage.Coverage( source=self.cov_source, + source_pkgs=self.cov_source_pkgs, branch=self.cov_branch, data_suffix=f'{filename_suffix(True)}.combine', data_file=os.path.abspath(self.cov.config.data_file), # noqa: PTH100 @@ -286,6 +292,7 @@ class DistMaster(CovController): def start(self): self.cov = coverage.Coverage( source=self.cov_source, + source_pkgs=self.cov_source_pkgs, branch=self.cov_branch, data_suffix=True, config_file=self.cov_config, @@ -301,6 +308,7 @@ def start(self): self.cov._warn_preimported_source = False self.combining_cov = coverage.Coverage( source=self.cov_source, + source_pkgs=self.cov_source_pkgs, branch=self.cov_branch, data_suffix=f'{filename_suffix(True)}.combine', data_file=os.path.abspath(self.cov.config.data_file), # noqa: PTH100 @@ -389,6 +397,7 @@ def start(self): # Erase any previous data and start coverage. self.cov = coverage.Coverage( source=self.cov_source, + source_pkgs=self.cov_source_pkgs, branch=self.cov_branch, data_suffix=True, config_file=self.cov_config, diff --git a/tests/test_pytest_cov.py b/tests/test_pytest_cov.py index b291f432..ffdd9ce2 100644 --- a/tests/test_pytest_cov.py +++ b/tests/test_pytest_cov.py @@ -206,6 +206,31 @@ def test_central(pytester, testdir, prop): assert result.ret == 0 +def test_central_explicit_cov_overrides_source_pkgs(testdir): + testdir.tmpdir.join('pkga').ensure_dir().join('__init__.py').write('def fa():\n return 1\n') + testdir.tmpdir.join('pkgb').ensure_dir().join('__init__.py').write('def fb():\n return 2\n') + script = testdir.makepyfile(""" + from pkga import fa + from pkgb import fb + + def test_both(): + assert fa() == 1 + assert fb() == 2 + """) + testdir.tmpdir.join('.coveragerc').write(""" +[run] +source_pkgs = + pkga + pkgb +""") + + result = testdir.runpytest('-v', '--cov=pkga', '--cov-report=term-missing', script) + + result.stdout.fnmatch_lines(['*_ coverage: platform *, python * _*', 'pkga*100%*']) + assert not any(line.startswith('pkgb') for line in result.stdout.lines) + assert result.ret == 0 + + def test_annotate(testdir): script = testdir.makepyfile(SCRIPT)