Skip to content

Commit

Permalink
Avoid importing coverage for test runs that don't need it
Browse files Browse the repository at this point in the history
  • Loading branch information
boxed committed Sep 16, 2019
1 parent f1671be commit 81b779f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/pytest_cov/engine.py
Expand Up @@ -5,9 +5,6 @@
import socket
import sys

import coverage
from coverage.data import CoverageData

from .embed import cleanup
from .compat import StringIO, workeroutput, workerinput

Expand Down Expand Up @@ -93,6 +90,10 @@ def summary(self, stream):
if not self.cov_report:
return self.cov.report(show_missing=True, ignore_errors=True, file=_NullFile)

# import coverage lazily here to avoid importing
# it for unit tests that don't need it
import coverage

# Output coverage section header.
if len(self.node_descs) == 1:
self.sep(stream, '-', 'coverage: %s' % ''.join(self.node_descs))
Expand Down Expand Up @@ -152,6 +153,10 @@ class Central(CovController):
def start(self):
cleanup()

# import coverage lazily here to avoid importing
# it for unit tests that don't need it
import coverage

self.cov = coverage.Coverage(source=self.cov_source,
branch=self.cov_branch,
config_file=self.cov_config)
Expand Down Expand Up @@ -190,6 +195,10 @@ class DistMaster(CovController):
def start(self):
cleanup()

# import coverage lazily here to avoid importing
# it for unit tests that don't need it
import coverage

# Ensure coverage rc file rsynced if appropriate.
if self.cov_config and os.path.exists(self.cov_config):
self.config.option.rsyncdir.append(self.cov_config)
Expand Down Expand Up @@ -236,11 +245,17 @@ def testnodedown(self, node, error):
output['cov_worker_node_id']
)

# import coverage lazily here to avoid importing
# it for unit tests that don't need it
import coverage
from coverage.data import CoverageData

cov = coverage.Coverage(source=self.cov_source,
branch=self.cov_branch,
data_suffix=data_suffix,
config_file=self.cov_config)
cov.start()

data = CoverageData()
data.read_fileobj(StringIO(output['cov_worker_data']))
cov.data.update(data)
Expand Down Expand Up @@ -272,6 +287,10 @@ class DistWorker(CovController):
def start(self):
cleanup()

# import coverage lazily here to avoid importing
# it for unit tests that don't need it
import coverage

# Determine whether we are collocated with master.
self.is_collocated = (socket.gethostname() == workerinput(self.config)['cov_master_host'] and
self.topdir == workerinput(self.config)['cov_master_topdir'])
Expand Down
6 changes: 5 additions & 1 deletion src/pytest_cov/plugin.py
Expand Up @@ -4,7 +4,6 @@
import warnings

import pytest
from coverage.misc import CoverageException

from . import compat
from . import embed
Expand Down Expand Up @@ -239,6 +238,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

0 comments on commit 81b779f

Please sign in to comment.