Skip to content

Commit 1633bd3

Browse files
committed
run coverage manually if test is in a new process
credits go to @mocksoul for initial inspiration
1 parent da63f62 commit 1633bd3

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

pytest_cov.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""Coverage plugin for pytest."""
22

3+
import os
34

45
import pytest
56

67
import cov_core
8+
import cov_core_init
79

810

911
def pytest_addoption(parser):
@@ -64,6 +66,8 @@ def __init__(self, options, pluginmanager, start=True):
6466
"""
6567

6668
# Our implementation is unknown at this time.
69+
self.pid = None
70+
self.cov = None
6771
self.cov_controller = None
6872
self.failed = False
6973
self.options = options
@@ -97,6 +101,7 @@ class Config(object):
97101

98102
def pytest_sessionstart(self, session):
99103
"""At session start determine our implementation and delegate to it."""
104+
self.pid = os.getpid()
100105
is_slave = hasattr(session.config, 'slaveinput')
101106
if is_slave:
102107
nodeid = session.config.slaveinput.get('slaveid',
@@ -132,6 +137,17 @@ def pytest_terminal_summary(self, terminalreporter):
132137
if not (self.failed and self.options.no_cov_on_fail):
133138
self.cov_controller.summary(terminalreporter._tw)
134139

140+
def pytest_runtest_setup(self, item):
141+
if os.getpid() != self.pid:
142+
# test is run in another process than session, run
143+
# coverage manually
144+
self.cov = cov_core_init.init()
145+
146+
def pytest_runtest_teardown(self, item):
147+
if self.cov is not None:
148+
cov_core.multiprocessing_finish(self.cov)
149+
self.cov = None
150+
135151

136152
def pytest_funcarg__cov(request):
137153
"""A pytest funcarg that provides access to the underlying coverage

0 commit comments

Comments
 (0)