From 7ce7f10494bd7271ad5c17100021301a6ff4128e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Hovm=C3=B6ller?= Date: Mon, 16 Sep 2019 15:32:08 +0200 Subject: [PATCH] Avoid importing coverage for test runs that don't need it --- src/pytest_cov/plugin.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/pytest_cov/plugin.py b/src/pytest_cov/plugin.py index 1127e4c7..c0f28a05 100644 --- a/src/pytest_cov/plugin.py +++ b/src/pytest_cov/plugin.py @@ -5,11 +5,9 @@ import coverage import pytest -from coverage.misc import CoverageException from . import compat from . import embed -from . import engine PYTEST_VERSION = tuple(map(int, pytest.__version__.split('.')[:3])) @@ -160,6 +158,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: @@ -202,6 +204,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 = ( @@ -256,6 +262,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: