-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpytest_insubprocess.py
More file actions
213 lines (173 loc) · 6.79 KB
/
pytest_insubprocess.py
File metadata and controls
213 lines (173 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os
import re
import subprocess
import sys
import tempfile
from pathlib import Path
from typing import Any, Literal, cast
from xml.parsers.expat import ExpatError
import pytest
import xmltodict
from _pytest.reports import TestReport
SYSTEM_OUT_REGEX = re.compile(r'-+ Captured Out -*\n(?P<stdout>.*)', re.MULTILINE | re.DOTALL)
def pytest_addoption(parser: pytest.Parser) -> None:
group = parser.getgroup('insubprocess')
group.addoption(
'--insubprocess',
action='store_true',
default=False,
help='run all tests in isolated subprocesses',
)
def pytest_configure(config: pytest.Config) -> None:
config.addinivalue_line('markers', 'insubprocess: run test in an isolated subprocess')
@pytest.hookimpl(tryfirst=True)
def pytest_runtest_protocol(item: pytest.Item, nextitem: pytest.Item | None) -> object | None:
# Check if the test should be executed in a subprocess
insubprocess_option = item.config.getoption('--insubprocess')
insubprocess_marker = item.get_closest_marker('insubprocess')
# Skip if already running in a subprocess or if neither option nor marker is set
if os.environ.get('_PYTEST_INSUBPROCESS') == '1':
return None # Normal handling
if not insubprocess_option and not insubprocess_marker:
return None # Normal handling
item.session._setupstate.teardown_exact(nextitem)
try:
xml_report = _execute_in_subprocess(item)
except FileNotFoundError:
return _create_error_report(item, 'Could not find the junit XML file.')
pytest_report = _parse_xml_report(item, xml_report)
item.ihook.pytest_runtest_logreport(report=pytest_report)
return True
def _execute_in_subprocess(item: pytest.Item) -> str:
"""Execute the test in a subprocess and returns the generated JUnit XML report as a string."""
with tempfile.TemporaryDirectory() as tmpdir:
junit_xml_path = Path(tmpdir) / 'results.xml'
# Execute the test in a subprocess using its nodeid
cmd = [
sys.executable,
'-m',
'pytest',
'--capture=fd',
'-o',
'junit_logging=all',
'--junitxml=' + junit_xml_path.as_posix(),
item.nodeid,
] + _get_options(item.config)
env = os.environ | {'_PYTEST_INSUBPROCESS': '1'}
result = subprocess.run(
cmd,
capture_output=True,
encoding='utf-8',
check=False,
env=env,
)
# Debug: print subprocess output if needed
if result.returncode != 0 and not junit_xml_path.exists():
import warnings
warnings.warn(
f'Subprocess failed with return code {result.returncode}\n'
f'stdout: {result.stdout}\n'
f'stderr: {result.stderr}'
)
# Parse the generated JUnit XML report
return junit_xml_path.read_text()
def _get_options(config: pytest.Config) -> list[str]:
cmd = []
if (quiet := config.getoption('--quiet')) > 0:
cmd.append('-' + quiet * 'q')
if (verbose := config.getoption('--verbose')) > 0:
cmd.append('-' + verbose * 'v')
return cmd
def _parse_xml_report(item: pytest.Item, junit_xml: str) -> TestReport:
try:
root = xmltodict.parse(junit_xml)
except ExpatError:
# Parsing error
return _create_error_report(item, 'Error when parsing the subprocess test report.')
try:
testcase = _find_testcase(root)
except ValueError:
return _create_error_report(item, 'Test not found in the subprocess report.')
outcome, longrepr, type_ = _parse_testcase_outcome(item, testcase)
when: Literal['setup', 'call', 'teardown']
if isinstance(longrepr, str) and longrepr.startswith('failed on setup'):
when = 'setup'
elif isinstance(longrepr, str) and longrepr.startswith('failed on teardown'):
when = 'teardown'
else:
when = 'call'
duration = float(testcase['@time'])
system_out = testcase['system-out']
system_err = testcase['system-err']
capture = item.config.getoption('--capture')
if capture in ['no', 'tee-sys']:
match = SYSTEM_OUT_REGEX.search(system_out)
if match:
print(match['stdout'])
else:
print(system_out)
# Create the test report
report = TestReport(
nodeid=item.nodeid,
location=item.location,
keywords=item.keywords,
outcome=outcome,
longrepr=longrepr,
when=when,
duration=duration,
sections=[
('Captured stdout', system_out),
('Captured stderr', system_err),
],
)
xfail_marker = item.get_closest_marker('xfail')
if outcome == 'skipped' and type_ == 'pytest.xfail':
if xfail_marker is not None:
reason = xfail_marker.kwargs['reason']
else:
assert isinstance(longrepr, tuple)
reason = longrepr[2]
report.wasxfail = f'reason: {reason}'
elif outcome == 'passed' and xfail_marker is not None:
reason = xfail_marker.kwargs['reason']
report.wasxfail = f'reason: {reason}'
return report
def _find_testcase(root: dict[str, Any]) -> dict[str, Any]:
"""Find the testcase element in the XML tree."""
element = root.get('testsuites')
if element is None:
raise ValueError('No testsuites element in the XML tree.')
element = element.get('testsuite')
if element is None:
raise ValueError('No testsuite element in the XML tree.')
element = element.get('testcase')
if element is None:
raise ValueError('No testcase element in the XML tree.')
if not isinstance(element, dict):
raise ValueError('The testcase element is not a dictionary.')
return cast(dict[str, Any], element)
def _parse_testcase_outcome(
item: pytest.Item,
testcase: dict[str, Any],
) -> tuple[Literal['failed', 'passed', 'skipped'], str | tuple[str, int, str] | None, str | None]:
"""Return the outcome of the testcase and its longrepr, if any, in the XML tree."""
if (failure := testcase.get('failure')) is not None:
return 'failed', failure['@message'], None
if (skipped := testcase.get('skipped')) is not None:
path, line = item.reportinfo()[:2]
assert line is not None
longrepr = str(path), line, cast(str, skipped['@message'])
return 'skipped', longrepr, skipped['@type']
return 'passed', None, None
def _create_error_report(item: pytest.Item, message: str) -> pytest.TestReport:
"""Creates an error report for the exceptional cases."""
return TestReport(
nodeid=item.nodeid,
location=item.location,
keywords=item.keywords,
outcome='failed',
longrepr=message,
when='call',
duration=0.0,
sections=[],
)