diff --git a/src/pytest_cov/plugin.py b/src/pytest_cov/plugin.py index d84a6dbc..89b91b38 100644 --- a/src/pytest_cov/plugin.py +++ b/src/pytest_cov/plugin.py @@ -4,11 +4,9 @@ import warnings import pytest -from coverage.misc import CoverageException from . import compat from . import embed -from . import engine class CoverageError(Exception): @@ -146,6 +144,10 @@ def __init__(self, options, pluginmanager, start=True): self.options.cov_report = {} self.options.cov_source = _prepare_cov_source(self.options.cov_source) + # import engine lazily here to avoid importing + # it for unit tests that don't need it + from . import engine + if is_dist and start: self.start(engine.DistMaster) elif start: @@ -188,6 +190,10 @@ def pytest_sessionstart(self, session): self._disabled = True return + # import engine lazily here to avoid importing + # it for unit tests that don't need it + from . import engine + self.pid = os.getpid() if self._is_worker(session): nodeid = ( @@ -239,6 +245,11 @@ def pytest_runtestloop(self, session): self.cov_controller.finish() if not self._is_worker(session) and self._should_report(): + + # import coverage lazily here to avoid importing + # it for unit tests that don't need it + from coverage.misc import CoverageException + try: self.cov_total = self.cov_controller.summary(self.cov_report) except CoverageException as exc: