Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid importing coverage for test runs that don't need it #339

Merged
merged 2 commits into from Jan 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -37,3 +37,4 @@ Authors
* Martín Gaitán - https://github.com/mgaitan
* Hugo van Kemenade - https://github.com/hugovk
* Michael Manganiello - https://github.com/adamantike
* Anders Hovmöller - https://github.com/boxed
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -6,6 +6,8 @@ Changelog

* Fixed ``RemovedInPytest4Warning`` when using Pytest 3.10.
Contributed by Michael Manganiello in `#354 <https://github.com/pytest-dev/pytest-cov/pull/354>`_.
* Made pytest startup faster when plugin not active by lazy-importing.
Contributed by Anders Hovmöller in `#339 <https://github.com/pytest-dev/pytest-cov/pull/339>`_.

2.8.1 (2019-10-05)
------------------
Expand Down
15 changes: 13 additions & 2 deletions src/pytest_cov/plugin.py
Expand Up @@ -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]))

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -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:
Expand Down