|
1 | 1 | """Coverage plugin for pytest."""
|
2 | 2 |
|
| 3 | +import os |
3 | 4 |
|
4 | 5 | import pytest
|
5 | 6 |
|
6 | 7 | import cov_core
|
| 8 | +import cov_core_init |
7 | 9 |
|
8 | 10 |
|
9 | 11 | def pytest_addoption(parser):
|
@@ -64,6 +66,8 @@ def __init__(self, options, pluginmanager, start=True):
|
64 | 66 | """
|
65 | 67 |
|
66 | 68 | # Our implementation is unknown at this time.
|
| 69 | + self.pid = None |
| 70 | + self.cov = None |
67 | 71 | self.cov_controller = None
|
68 | 72 | self.failed = False
|
69 | 73 | self.options = options
|
@@ -97,6 +101,7 @@ class Config(object):
|
97 | 101 |
|
98 | 102 | def pytest_sessionstart(self, session):
|
99 | 103 | """At session start determine our implementation and delegate to it."""
|
| 104 | + self.pid = os.getpid() |
100 | 105 | is_slave = hasattr(session.config, 'slaveinput')
|
101 | 106 | if is_slave:
|
102 | 107 | nodeid = session.config.slaveinput.get('slaveid',
|
@@ -132,6 +137,17 @@ def pytest_terminal_summary(self, terminalreporter):
|
132 | 137 | if not (self.failed and self.options.no_cov_on_fail):
|
133 | 138 | self.cov_controller.summary(terminalreporter._tw)
|
134 | 139 |
|
| 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 | + |
135 | 151 |
|
136 | 152 | def pytest_funcarg__cov(request):
|
137 | 153 | """A pytest funcarg that provides access to the underlying coverage
|
|
0 commit comments