forked from hyperledger-archives/aries-protocol-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
252 lines (210 loc) · 7.07 KB
/
conftest.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
""" Pytest behavior customizations.
"""
import os
import sys
import time
import re
import itertools
import logging
import pytest
from _pytest.terminal import TerminalReporter
from config import load_config, default
from reporting import ReportSingleton, TestFunction, TestReport
class AgentTerminalReporter(TerminalReporter):
""" Customized PyTest Output """
@pytest.hookimpl(trylast=True)
def pytest_sessionstart(self, session):
self._session = session
self._sessionstarttime = time.time()
def pytest_runtest_logstart(self, nodeid, location):
line = self._locationline(nodeid, *location)
self.write_sep('=', line, bold=True)
self.write('\n')
def pytest_addoption(parser):
""" Load in config path. """
group = parser.getgroup(
"Aries Protocol Test Suite Configuration",
"Aries Protocol Test Suite Configuration",
after="general"
)
group.addoption(
"--sc",
"--suite-config",
dest='suite_config',
action="store",
metavar="SUITE_CONFIG",
help="Load suite configuration from SUITE_CONFIG",
)
group.addoption(
"-S",
"--select",
dest='select',
action='store',
metavar='SELECT_REGEX',
help='Run tests matching SELECT_REGEX. '
'Overrides tests selected in configuration.'
)
group.addoption(
"-O",
"--output",
dest="save_path",
action="store",
metavar="PATH",
help="Save interop profile to PATH."
)
group.addoption(
"-L",
"--list",
dest="list_tests",
action="store_true",
help="List available tests."
)
group.addoption(
"--show-dev-notes",
dest="dev_notes",
action="store_true",
help="Output log messages generated during testing for developers\n"
"take note of."
)
@pytest.hookimpl(trylast=True)
def pytest_configure(config):
""" Load Test Suite Configuration. """
dirname = os.getcwd()
config_path = config.getoption('suite_config')
config_path = 'config.toml' if not config_path else config_path
config_path = os.path.join(dirname, config_path)
print(
'\nAttempting to load configuration from file: %s\n' %
config_path
)
try:
config.suite_config = load_config(config_path)
except FileNotFoundError:
config.suite_config = default()
config.suite_config['save_path'] = config.getoption('save_path')
# Override default terminal reporter for better test output when not capturing
if config.getoption('capture') == 'no':
reporter = config.pluginmanager.get_plugin('terminalreporter')
agent_reporter = AgentTerminalReporter(config, sys.stdout)
config.pluginmanager.unregister(reporter)
config.pluginmanager.register(agent_reporter, 'terminalreporter')
# Compile select regex and test regex if given
select_regex = config.getoption('select')
config.select_regex = re.compile(select_regex) if select_regex else None
config.tests_regex = list(map(
re.compile, config.suite_config['tests']
))
def pytest_collection_modifyitems(session, config, items):
"""Select tests based on config or args."""
# pylint: disable=protected-access
if not items:
return
report = ReportSingleton(session.config.suite_config)
def add_to_report(item):
if callable(item._obj) and hasattr(item._obj, 'meta_set'):
func = item._obj
test_fn = TestFunction(
protocol=func.protocol,
version=func.version,
role=func.role,
name=func.name,
description=func.__doc__
)
item.meta_name = test_fn.flatten()['name']
report.add_test(test_fn)
return item
def test_regex_filter(item):
for regex in config.tests_regex:
if regex.match(item.meta_name):
return True
return False
item_pipeline = map(add_to_report, items)
if config.select_regex:
item_pipeline = filter(
lambda item: bool(config.select_regex.match(item.meta_name)),
item_pipeline
)
elif config.tests_regex:
item_pipeline = filter(
test_regex_filter,
item_pipeline
)
remaining = list(item_pipeline)
deselected = list(set(items)-set(remaining))
# Report the deselected items to pytest
config.hook.pytest_deselected(items=deselected)
items[:] = remaining
@pytest.hookimpl()
def pytest_report_collectionfinish(config, startdir, items):
"""Print available tests if option set."""
if config.getoption('list_tests'):
reporter = config.pluginmanager.get_plugin('terminalreporter')
reporter.write('\n')
reporter.write_sep('-', 'Available Tests', bold=False, yellow=True)
return ReportSingleton(config.suite_config).available_tests_json()
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
""" Customize reporting """
outcome = yield
report = outcome.get_result()
setattr(item, "report_" + report.when, report)
def pytest_terminal_summary(terminalreporter, exitstatus, config):
"""Write Interop Profile to terminal summary."""
if config.getoption('collectonly'):
return
report = ReportSingleton(config.suite_config)
if config.getoption('dev_notes'):
terminalreporter.write('\n')
terminalreporter.write_sep(
'=', 'Developer Notes', bold=True
)
terminalreporter.write('\n')
terminalreporter.write(report.notes_json())
terminalreporter.write('\n')
terminalreporter.write('\n')
terminalreporter.write_sep(
'=', 'Interop Profile', bold=True
)
terminalreporter.write('\n')
terminalreporter.write(report.report_json())
terminalreporter.write('\n')
@pytest.fixture(scope='session')
def report(config):
"""Report fixture."""
report_instance = ReportSingleton(config)
yield report_instance
save_path = config.get('save_path')
if save_path:
report_instance.save(save_path)
@pytest.fixture
def report_on_test(request, caplog, report):
"""Universally loaded fixture for getting test reports."""
yield
passed = False
if hasattr(request.node, 'report_call') and \
request.node.report_call.outcome == 'passed':
passed = True
test_fn = TestFunction(
protocol=request.function.protocol,
version=request.function.version,
role=request.function.role,
name=request.function.name,
description=request.function.__doc__
)
report.add_report(TestReport(test_fn, passed))
notes = itertools.chain([
records for when in ('setup', 'call', 'teardown')
for records in caplog.get_records(when)
])
notes = filter(
lambda log_rec: log_rec.levelno >= logging.WARNING,
notes
)
notes = map(
lambda log_rec: log_rec.message,
notes
)
report.add_notes(
test_fn,
notes
)