Skip to content

Commit

Permalink
Remove imported third-party packages so that the auto-cov functionali…
Browse files Browse the repository at this point in the history
…ty will happen early and not affect test coverage on those packages. Fixes #2.
  • Loading branch information
jaraco committed Nov 21, 2020
1 parent 1e124ca commit e20482c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v4.0.1
======

#2: In enabler, remove third-party packages from sys.modules
to avoid spurious non-coverage of lines covered at import time.

v4.0.0
======

Expand Down
16 changes: 16 additions & 0 deletions jaraco/test/pytest/enabler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import shlex
import types
import sys

import toml
from jaraco.context import suppress
Expand Down Expand Up @@ -35,6 +36,20 @@ def pytest_load_initial_conftests(early_config, parser, args):
_pytest_cov_check(plugins, early_config, parser, args)


def _remove_deps():
"""
Coverage will not detect function definitions as being covered
if the functions are defined before coverage is invoked. As
a result, when testing any of the dependencies above, their
functions will appear not to be covered. To avoid this behavior,
unload the modules above so they may be tested for coverage
on import as well.
"""
del sys.modules['jaraco.functools']
del sys.modules['jaraco.context']
del sys.modules['toml']


def _pytest_cov_check(plugins, early_config, parser, args): # pragma: nocover
"""
pytest_cov runs its command-line checks so early that no hooks
Expand All @@ -50,6 +65,7 @@ def _pytest_cov_check(plugins, early_config, parser, args): # pragma: nocover
)
if not new_args or not plugin_enabled:
return
_remove_deps()
parser.parse_known_and_unknown_args(new_args, early_config.known_args_namespace)
with contextlib.suppress(ImportError):
import pytest_cov.plugin
Expand Down

0 comments on commit e20482c

Please sign in to comment.