Skip to content

Commit 54a584a

Browse files
committed
Move summary list reporting back to QuietTestResultHandler
1 parent b2e6dcf commit 54a584a

File tree

1 file changed

+87
-116
lines changed

1 file changed

+87
-116
lines changed

haas/plugins/result_handler.py

Lines changed: 87 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,41 @@ def sort(items):
6363
return list(sort(handlers))
6464

6565

66+
def _summary_report_config(args=None):
67+
""" Get the summary report config based on arguments
68+
"""
69+
report_lists = [TestCompletionStatus.error, TestCompletionStatus.failure]
70+
if args is None:
71+
return report_lists
72+
if args.report_skipped or args.report_all:
73+
report_lists.append(TestCompletionStatus.skipped)
74+
if args.report_expected_failures or args.report_all:
75+
report_lists.append(TestCompletionStatus.expected_failure)
76+
if args.report_unxpected_success or args.report_all:
77+
report_lists.append(TestCompletionStatus.unexpected_success)
78+
return report_lists
79+
80+
6681
class QuietTestResultHandler(IResultHandlerPlugin):
6782
separator1 = '=' * 70
6883
separator2 = separator2
6984

70-
def __init__(self, test_count):
85+
_summary_formats = {
86+
TestCompletionStatus.failure: 'FAIL',
87+
TestCompletionStatus.error: 'ERROR',
88+
TestCompletionStatus.unexpected_success: 'unexpected success',
89+
TestCompletionStatus.expected_failure: 'expected failure',
90+
TestCompletionStatus.skipped: 'skipped',
91+
}
92+
93+
def __init__(self, test_count, report_lists=None):
7194
self.enabled = True
7295
self.stream = _WritelnDecorator(sys.stderr)
7396
self._test_count = test_count
97+
if report_lists is None:
98+
self._report_lists = _summary_report_config()
99+
else:
100+
self._report_lists = report_lists
74101
self.tests_run = 0
75102
self.descriptions = True
76103
self.expectedFailures = expectedFailures = []
@@ -90,12 +117,32 @@ def __init__(self, test_count):
90117

91118
@classmethod
92119
def from_args(cls, args, name, dest_prefix, test_count):
120+
report_lists = _summary_report_config(args)
93121
if args.verbosity == 0:
94-
return cls(test_count=test_count)
122+
return cls(test_count=test_count, report_lists=report_lists)
95123

96124
@classmethod
97-
def add_parser_arguments(self, parser, name, option_prefix, dest_prefix):
98-
pass
125+
def add_parser_arguments(cls, parser, name, option_prefix, dest_prefix):
126+
parser.add_argument(
127+
'--report-skipped', action='store', type=bool,
128+
metavar='REPORT_SKIPPED_TESTS',
129+
default=cls.OPTION_DEFAULT,
130+
help=('Report skipped tests'))
131+
parser.add_argument(
132+
'--report-expected-failures', action='store', type=bool,
133+
metavar='REPORT_EXPECTED_FAILURE_TESTS',
134+
default=cls.OPTION_DEFAULT,
135+
help=('Report expected failure tests'))
136+
parser.add_argument(
137+
'--report-unexpected-success', action='store', type=bool,
138+
metavar='REPORT_UNEXPECTED_SUCCESS_TESTS',
139+
default=cls.OPTION_DEFAULT,
140+
help=('Report unexpected success tests'))
141+
parser.add_argument(
142+
'--report-all', action='store', type=bool,
143+
metavar='REPORT_ALL_TESTS',
144+
default=cls.OPTION_DEFAULT,
145+
help=('Report list for all test types'))
99146

100147
def get_test_description(self, test):
101148
return get_test_description(test, descriptions=self.descriptions)
@@ -111,6 +158,8 @@ def start_test_run(self):
111158

112159
def stop_test_run(self):
113160
self.stop_time = time.time()
161+
for status in self._report_lists:
162+
self.print_list_summary(status)
114163
self.print_summary()
115164

116165
def print_summary(self):
@@ -149,6 +198,36 @@ def print_summary(self):
149198
else:
150199
self.stream.write("\n")
151200

201+
def print_list_summary(self, status):
202+
"""Print the list of tests of a given status.
203+
204+
Parameters
205+
----------
206+
status : TestCompletionStatus
207+
208+
"""
209+
results = self._collectors[status]
210+
if len(results) == 0:
211+
return
212+
213+
stream = self.stream
214+
template = self._summary_formats[status] + ': %s'
215+
raised_exception = status in (
216+
TestCompletionStatus.error, TestCompletionStatus.failure)
217+
218+
if not raised_exception:
219+
stream.writeln(self.separator1)
220+
for result in results:
221+
if raised_exception:
222+
stream.writeln(self.separator1)
223+
stream.writeln(
224+
template % self.get_test_description(result.test))
225+
if raised_exception:
226+
stream.writeln(self.separator2)
227+
stream.writeln(result.exception)
228+
if not raised_exception:
229+
stream.writeln(self.separator2)
230+
152231
def was_successful(self):
153232
return (len(self.errors) == 0 and
154233
len(self.failures) == 0 and
@@ -173,8 +252,9 @@ class StandardTestResultHandler(QuietTestResultHandler):
173252

174253
@classmethod
175254
def from_args(cls, args, name, dest_prefix, test_count):
255+
report_lists = _summary_report_config(args)
176256
if args.verbosity == 1:
177-
return cls(test_count=test_count)
257+
return cls(test_count=test_count, report_lists=report_lists)
178258

179259
def __call__(self, result):
180260
super(StandardTestResultHandler, self).__call__(result)
@@ -195,8 +275,9 @@ class VerboseTestResultHandler(StandardTestResultHandler):
195275

196276
@classmethod
197277
def from_args(cls, args, name, dest_prefix, test_count):
278+
report_lists = _summary_report_config(args)
198279
if args.verbosity == 2:
199-
return cls(test_count=test_count)
280+
return cls(test_count=test_count, report_lists=report_lists)
200281

201282
def start_test(self, test):
202283
super(VerboseTestResultHandler, self).start_test(test)
@@ -221,116 +302,6 @@ def __call__(self, result):
221302
self.stream.flush()
222303

223304

224-
class ListSummaryHandler(IResultHandlerPlugin):
225-
226-
_result_formats = {
227-
TestCompletionStatus.failure: 'FAIL',
228-
TestCompletionStatus.error: 'ERROR',
229-
TestCompletionStatus.unexpected_success: 'UNEXPECTED',
230-
TestCompletionStatus.expected_failure: 'EXPECTED',
231-
TestCompletionStatus.skipped: 'SKIPPED',
232-
}
233-
234-
def __init__(
235-
self, list_skipped, list_expected_failures,
236-
list_unexpected_successes):
237-
self.enabled = True
238-
self.stream = _WritelnDecorator(sys.stderr)
239-
self._collectors = collectors = {
240-
TestCompletionStatus.failure: [],
241-
TestCompletionStatus.error: [],
242-
}
243-
if list_skipped:
244-
collectors[TestCompletionStatus.skipped] = []
245-
if list_expected_failures:
246-
collectors[TestCompletionStatus.expected_failure] = []
247-
if list_unexpected_successes:
248-
collectors[TestCompletionStatus.unexpected_success] = []
249-
250-
@classmethod
251-
def add_parser_arguments(cls, parser, name, option_prefix, dest_prefix):
252-
parser.add_argument(
253-
'--report-skipped', action='store', type=bool,
254-
metavar='REPORT_SKIPPED_TESTS',
255-
default=cls.OPTION_DEFAULT,
256-
help=('Report skipped tests'))
257-
parser.add_argument(
258-
'--report-expected-failures', action='store', type=bool,
259-
metavar='REPORT_EXPECTED_FAILURE_TESTS',
260-
default=cls.OPTION_DEFAULT,
261-
help=('Report expected failure tests'))
262-
parser.add_argument(
263-
'--report-unexpected-success', action='store', type=bool,
264-
metavar='REPORT_UNEXPECTED_SUCCESS_TESTS',
265-
default=cls.OPTION_DEFAULT,
266-
help=('Report unexpected success tests'))
267-
parser.add_argument(
268-
'--report-all', action='store', type=bool,
269-
metavar='REPORT_ALL_TESTS',
270-
default=cls.OPTION_DEFAULT,
271-
help=('Report list for all test types'))
272-
273-
@classmethod
274-
def from_args(cls, args, name, dest_prefix, test_count):
275-
return cls(
276-
args.list_skipped,
277-
args.list_expected_failures,
278-
args.list_unexpected_successes)
279-
280-
def start_test(self, test):
281-
pass
282-
283-
def stop_test(self, test):
284-
pass
285-
286-
def start_test_run(self):
287-
pass
288-
289-
def stop_test_run(self):
290-
self.print_list_summary(TestCompletionStatus.error)
291-
self.print_list_summary(TestCompletionStatus.failure)
292-
skipped = TestCompletionStatus.skipped
293-
expected = TestCompletionStatus.expected_failure
294-
unexpected = TestCompletionStatus.unexpected_success
295-
if unexpected in self._collectors:
296-
self.print_list_summary(unexpected)
297-
if skipped in self._collectors:
298-
self.print_list_summary(skipped)
299-
if expected in self._collectors:
300-
self.print_list_summary(expected)
301-
302-
def print_list_summary(self, status):
303-
"""Print the list of tests of a given status.
304-
305-
Parameters
306-
----------
307-
status : TestCompletionStatus
308-
309-
"""
310-
results = self._collectors[status]
311-
template = self._result_formats['status'] + ': %s'
312-
stream = self.stream
313-
raised_exception = status in (
314-
TestCompletionStatus.error, TestCompletionStatus.failure)
315-
if not raised_exception:
316-
stream.writeln(self.separator1)
317-
for result in results:
318-
if raised_exception:
319-
stream.writeln(self.separator1)
320-
stream.writeln(
321-
template % self.get_test_description(result.test))
322-
if raised_exception:
323-
stream.writeln(self.separator2)
324-
stream.writeln(result.exception)
325-
if not raised_exception:
326-
stream.writeln(self.separator2)
327-
328-
def __call__(self, result):
329-
collector = self._collectors.get(result.status)
330-
if collector is not None:
331-
collector.append(result)
332-
333-
334305
class TimingResultHandler(IResultHandlerPlugin):
335306
separator1 = '=' * 70
336307
separator2 = separator2

0 commit comments

Comments
 (0)