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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
#
# subunit: extensions to Python unittest to get test results from subprocesses.
# Copyright (C) 2009 Robert Collins <robertc@robertcollins.net>
#
# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
# license at the users choice. A copy of both licenses are available in the
# project source as Apache-2.0 and BSD. You may not use this file except in
# compliance with one of these two licences.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# license you chose for the specific language governing permissions and
# limitations under that license.
#
import datetime
import unittest
from StringIO import StringIO
import os
import sys
import subunit
import subunit.iso8601 as iso8601
import subunit.test_results
from subunit.content_type import ContentType
from subunit.content import Content
class LoggingDecorator(subunit.test_results.HookedTestResultDecorator):
def __init__(self, decorated):
self._calls = 0
super(LoggingDecorator, self).__init__(decorated)
def _before_event(self):
self._calls += 1
class AssertBeforeTestResult(LoggingDecorator):
"""A TestResult for checking preconditions."""
def __init__(self, decorated, test):
self.test = test
super(AssertBeforeTestResult, self).__init__(decorated)
def _before_event(self):
self.test.assertEqual(1, self.earlier._calls)
super(AssertBeforeTestResult, self)._before_event()
class TimeCapturingResult(unittest.TestResult):
def __init__(self):
super(TimeCapturingResult, self).__init__()
self._calls = []
def time(self, a_datetime):
self._calls.append(a_datetime)
class LoggingResult(object):
"""Basic support for logging of results."""
def __init__(self):
self._calls = []
class Python26TestResult(LoggingResult):
"""A python 2.6 like test result, that logs."""
def addError(self, test, err):
self._calls.append(('addError', test, err))
def addFailure(self, test, err):
self._calls.append(('addFailure', test, err))
def addSuccess(self, test):
self._calls.append(('addSuccess', test))
def startTest(self, test):
self._calls.append(('startTest', test))
def stopTest(self, test):
self._calls.append(('stopTest', test))
class Python27TestResult(Python26TestResult):
"""A python 2.7 like test result, that logs."""
def addExpectedFailure(self, test, err):
self._calls.append(('addExpectedFailure', test, err))
def addSkip(self, test, reason):
self._calls.append(('addSkip', test, reason))
def addUnexpectedSuccess(self, test):
self._calls.append(('addUnexpectedSuccess', test))
def startTestRun(self):
self._calls.append(('startTestRun',))
def stopTestRun(self):
self._calls.append(('stopTestRun',))
class ExtendedTestResult(Python27TestResult):
"""A test result like the proposed extended unittest result API."""
def addError(self, test, err=None, details=None):
self._calls.append(('addError', test, err or details))
def addFailure(self, test, err=None, details=None):
self._calls.append(('addFailure', test, err or details))
def addExpectedFailure(self, test, err=None, details=None):
self._calls.append(('addExpectedFailure', test, err or details))
def addSkip(self, test, reason=None, details=None):
self._calls.append(('addSkip', test, reason or details))
def addSuccess(self, test, details=None):
if details:
self._calls.append(('addSuccess', test, details))
else:
self._calls.append(('addSuccess', test))
def addUnexpectedSuccess(self, test, details=None):
if details:
self._calls.append(('addUnexpectedSuccess', test, details))
else:
self._calls.append(('addUnexpectedSuccess', test))
def progress(self, offset, whence):
self._calls.append(('progress', offset, whence))
def tags(self, new_tags, gone_tags):
self._calls.append(('tags', new_tags, gone_tags))
def time(self, time):
self._calls.append(('time', time))
class TestExtendedToOriginalResultDecoratorBase(unittest.TestCase):
def make_26_result(self):
self.result = Python26TestResult()
self.make_converter()
def make_27_result(self):
self.result = Python27TestResult()
self.make_converter()
def make_converter(self):
self.converter = \
subunit.test_results.ExtendedToOriginalDecorator(self.result)
def make_extended_result(self):
self.result = ExtendedTestResult()
self.make_converter()
def check_outcome_details(self, outcome):
"""Call an outcome with a details dict to be passed through."""
# This dict is /not/ convertible - thats deliberate, as it should
# not hit the conversion code path.
details = {'foo': 'bar'}
getattr(self.converter, outcome)(self, details=details)
self.assertEqual([(outcome, self, details)], self.result._calls)
def get_details_and_string(self):
"""Get a details dict and expected string."""
text1 = lambda:["1\n2\n"]
text2 = lambda:["3\n4\n"]
bin1 = lambda:["5\n"]
details = {'text 1': Content(ContentType('text', 'plain'), text1),
'text 2': Content(ContentType('text', 'strange'), text2),
'bin 1': Content(ContentType('application', 'binary'), bin1)}
return (details, "Binary content: bin 1\n"
"Text attachment: text 1\n------------\n1\n2\n"
"------------\nText attachment: text 2\n------------\n"
"3\n4\n------------\n")
def check_outcome_details_to_exec_info(self, outcome, expected=None):
"""Call an outcome with a details dict to be made into exc_info."""
# The conversion is a done using RemoteError and the string contents
# of the text types in the details dict.
if not expected:
expected = outcome
details, err_str = self.get_details_and_string()
getattr(self.converter, outcome)(self, details=details)
err = subunit.RemoteError(err_str)
self.assertEqual([(expected, self, err)], self.result._calls)
def check_outcome_details_to_nothing(self, outcome, expected=None):
"""Call an outcome with a details dict to be swallowed."""
if not expected:
expected = outcome
details = {'foo': 'bar'}
getattr(self.converter, outcome)(self, details=details)
self.assertEqual([(expected, self)], self.result._calls)
def check_outcome_details_to_string(self, outcome):
"""Call an outcome with a details dict to be stringified."""
details, err_str = self.get_details_and_string()
getattr(self.converter, outcome)(self, details=details)
self.assertEqual([(outcome, self, err_str)], self.result._calls)
def check_outcome_exc_info(self, outcome, expected=None):
"""Check that calling a legacy outcome still works."""
# calling some outcome with the legacy exc_info style api (no keyword
# parameters) gets passed through.
if not expected:
expected = outcome
err = subunit.RemoteError("foo\nbar\n")
getattr(self.converter, outcome)(self, err)
self.assertEqual([(expected, self, err)], self.result._calls)
def check_outcome_exc_info_to_nothing(self, outcome, expected=None):
"""Check that calling a legacy outcome on a fallback works."""
# calling some outcome with the legacy exc_info style api (no keyword
# parameters) gets passed through.
if not expected:
expected = outcome
err = subunit.RemoteError("foo\nbar\n")
getattr(self.converter, outcome)(self, err)
self.assertEqual([(expected, self)], self.result._calls)
def check_outcome_nothing(self, outcome, expected=None):
"""Check that calling a legacy outcome still works."""
if not expected:
expected = outcome
getattr(self.converter, outcome)(self)
self.assertEqual([(expected, self)], self.result._calls)
def check_outcome_string_nothing(self, outcome, expected):
"""Check that calling outcome with a string calls expected."""
getattr(self.converter, outcome)(self, "foo")
self.assertEqual([(expected, self)], self.result._calls)
def check_outcome_string(self, outcome):
"""Check that calling outcome with a string works."""
getattr(self.converter, outcome)(self, "foo")
self.assertEqual([(outcome, self, "foo")], self.result._calls)
class TestExtendedToOriginalResultDecorator(
TestExtendedToOriginalResultDecoratorBase):
def test_progress_py26(self):
self.make_26_result()
self.converter.progress(1, 2)
def test_progress_py27(self):
self.make_27_result()
self.converter.progress(1, 2)
def test_progress_pyextended(self):
self.make_extended_result()
self.converter.progress(1, 2)
self.assertEqual([('progress', 1, 2)], self.result._calls)
def test_startTest_py26(self):
self.make_26_result()
self.converter.startTest(self)
self.assertEqual([('startTest', self)], self.result._calls)
def test_startTest_py27(self):
self.make_27_result()
self.converter.startTest(self)
self.assertEqual([('startTest', self)], self.result._calls)
def test_startTest_pyextended(self):
self.make_extended_result()
self.converter.startTest(self)
self.assertEqual([('startTest', self)], self.result._calls)
def test_startTestRun_py26(self):
self.make_26_result()
self.converter.startTestRun()
self.assertEqual([], self.result._calls)
def test_startTestRun_py27(self):
self.make_27_result()
self.converter.startTestRun()
self.assertEqual([('startTestRun',)], self.result._calls)
def test_startTestRun_pyextended(self):
self.make_extended_result()
self.converter.startTestRun()
self.assertEqual([('startTestRun',)], self.result._calls)
def test_stopTest_py26(self):
self.make_26_result()
self.converter.stopTest(self)
self.assertEqual([('stopTest', self)], self.result._calls)
def test_stopTest_py27(self):
self.make_27_result()
self.converter.stopTest(self)
self.assertEqual([('stopTest', self)], self.result._calls)
def test_stopTest_pyextended(self):
self.make_extended_result()
self.converter.stopTest(self)
self.assertEqual([('stopTest', self)], self.result._calls)
def test_stopTestRun_py26(self):
self.make_26_result()
self.converter.stopTestRun()
self.assertEqual([], self.result._calls)
def test_stopTestRun_py27(self):
self.make_27_result()
self.converter.stopTestRun()
self.assertEqual([('stopTestRun',)], self.result._calls)
def test_stopTestRun_pyextended(self):
self.make_extended_result()
self.converter.stopTestRun()
self.assertEqual([('stopTestRun',)], self.result._calls)
def test_tags_py26(self):
self.make_26_result()
self.converter.tags(1, 2)
def test_tags_py27(self):
self.make_27_result()
self.converter.tags(1, 2)
def test_tags_pyextended(self):
self.make_extended_result()
self.converter.tags(1, 2)
self.assertEqual([('tags', 1, 2)], self.result._calls)
def test_time_py26(self):
self.make_26_result()
self.converter.time(1)
def test_time_py27(self):
self.make_27_result()
self.converter.time(1)
def test_time_pyextended(self):
self.make_extended_result()
self.converter.time(1)
self.assertEqual([('time', 1)], self.result._calls)
class TestExtendedToOriginalAddError(TestExtendedToOriginalResultDecoratorBase):
outcome = 'addError'
def test_outcome_Original_py26(self):
self.make_26_result()
self.check_outcome_exc_info(self.outcome)
def test_outcome_Original_py27(self):
self.make_27_result()
self.check_outcome_exc_info(self.outcome)
def test_outcome_Original_pyextended(self):
self.make_extended_result()
self.check_outcome_exc_info(self.outcome)
def test_outcome_Extended_py26(self):
self.make_26_result()
self.check_outcome_details_to_exec_info(self.outcome)
def test_outcome_Extended_py27(self):
self.make_27_result()
self.check_outcome_details_to_exec_info(self.outcome)
def test_outcome_Extended_pyextended(self):
self.make_extended_result()
self.check_outcome_details(self.outcome)
def test_outcome__no_details(self):
self.make_extended_result()
self.assertRaises(ValueError,
getattr(self.converter, self.outcome), self)
class TestExtendedToOriginalAddFailure(
TestExtendedToOriginalAddError):
outcome = 'addFailure'
class TestExtendedToOriginalAddExpectedFailure(
TestExtendedToOriginalAddError):
outcome = 'addExpectedFailure'
def test_outcome_Original_py26(self):
self.make_26_result()
self.check_outcome_exc_info_to_nothing(self.outcome, 'addSuccess')
def test_outcome_Extended_py26(self):
self.make_26_result()
self.check_outcome_details_to_nothing(self.outcome, 'addSuccess')
class TestExtendedToOriginalAddSkip(
TestExtendedToOriginalResultDecoratorBase):
outcome = 'addSkip'
def test_outcome_Original_py26(self):
self.make_26_result()
self.check_outcome_string_nothing(self.outcome, 'addSuccess')
def test_outcome_Original_py27(self):
self.make_27_result()
self.check_outcome_string(self.outcome)
def test_outcome_Original_pyextended(self):
self.make_extended_result()
self.check_outcome_string(self.outcome)
def test_outcome_Extended_py26(self):
self.make_26_result()
self.check_outcome_string_nothing(self.outcome, 'addSuccess')
def test_outcome_Extended_py27(self):
self.make_27_result()
self.check_outcome_details_to_string(self.outcome)
def test_outcome_Extended_pyextended(self):
self.make_extended_result()
self.check_outcome_details(self.outcome)
def test_outcome__no_details(self):
self.make_extended_result()
self.assertRaises(ValueError,
getattr(self.converter, self.outcome), self)
class TestExtendedToOriginalAddSuccess(
TestExtendedToOriginalResultDecoratorBase):
outcome = 'addSuccess'
expected = 'addSuccess'
def test_outcome_Original_py26(self):
self.make_26_result()
self.check_outcome_nothing(self.outcome, self.expected)
def test_outcome_Original_py27(self):
self.make_27_result()
self.check_outcome_nothing(self.outcome)
def test_outcome_Original_pyextended(self):
self.make_extended_result()
self.check_outcome_nothing(self.outcome)
def test_outcome_Extended_py26(self):
self.make_26_result()
self.check_outcome_details_to_nothing(self.outcome, self.expected)
def test_outcome_Extended_py27(self):
self.make_27_result()
self.check_outcome_details_to_nothing(self.outcome)
def test_outcome_Extended_pyextended(self):
self.make_extended_result()
self.check_outcome_details(self.outcome)
class TestExtendedToOriginalAddUnexpectedSuccess(
TestExtendedToOriginalAddSuccess):
outcome = 'addUnexpectedSuccess'
class TestHookedTestResultDecorator(unittest.TestCase):
def setUp(self):
# And end to the chain
terminal = unittest.TestResult()
# Asserts that the call was made to self.result before asserter was
# called.
asserter = AssertBeforeTestResult(terminal, self)
# The result object we call, which much increase its call count.
self.result = LoggingDecorator(asserter)
asserter.earlier = self.result
def tearDown(self):
# The hook in self.result must have been called
self.assertEqual(1, self.result._calls)
# The hook in asserter must have been called too, otherwise the
# assertion about ordering won't have completed.
self.assertEqual(1, self.result.decorated._calls)
def test_startTest(self):
self.result.startTest(self)
def test_startTestRun(self):
self.result.startTestRun()
def test_stopTest(self):
self.result.stopTest(self)
def test_stopTestRun(self):
self.result.stopTestRun()
def test_addError(self):
self.result.addError(self, subunit.RemoteError())
def test_addFailure(self):
self.result.addFailure(self, subunit.RemoteError())
def test_addSuccess(self):
self.result.addSuccess(self)
def test_addSkip(self):
self.result.addSkip(self, "foo")
def test_addExpectedFailure(self):
self.result.addExpectedFailure(self, subunit.RemoteError())
def test_addUnexpectedSuccess(self):
self.result.addUnexpectedSuccess(self)
def test_progress(self):
self.result.progress(1, subunit.PROGRESS_SET)
def test_wasSuccessful(self):
self.result.wasSuccessful()
def test_shouldStop(self):
self.result.shouldStop
def test_stop(self):
self.result.stop()
def test_time(self):
self.result.time(None)
class TestAutoTimingTestResultDecorator(unittest.TestCase):
def setUp(self):
# And end to the chain which captures time events.
terminal = TimeCapturingResult()
# The result object under test.
self.result = subunit.test_results.AutoTimingTestResultDecorator(
terminal)
def test_without_time_calls_time_is_called_and_not_None(self):
self.result.startTest(self)
self.assertEqual(1, len(self.result.decorated._calls))
self.assertNotEqual(None, self.result.decorated._calls[0])
def test_no_time_from_progress(self):
self.result.progress(1, subunit.PROGRESS_CUR)
self.assertEqual(0, len(self.result.decorated._calls))
def test_no_time_from_shouldStop(self):
self.result.decorated.stop()
self.result.shouldStop
self.assertEqual(0, len(self.result.decorated._calls))
def test_calling_time_inhibits_automatic_time(self):
# Calling time() outputs a time signal immediately and prevents
# automatically adding one when other methods are called.
time = datetime.datetime(2009,10,11,12,13,14,15, iso8601.Utc())
self.result.time(time)
self.result.startTest(self)
self.result.stopTest(self)
self.assertEqual(1, len(self.result.decorated._calls))
self.assertEqual(time, self.result.decorated._calls[0])
def test_calling_time_None_enables_automatic_time(self):
time = datetime.datetime(2009,10,11,12,13,14,15, iso8601.Utc())
self.result.time(time)
self.assertEqual(1, len(self.result.decorated._calls))
self.assertEqual(time, self.result.decorated._calls[0])
# Calling None passes the None through, in case other results care.
self.result.time(None)
self.assertEqual(2, len(self.result.decorated._calls))
self.assertEqual(None, self.result.decorated._calls[1])
# Calling other methods doesn't generate an automatic time event.
self.result.startTest(self)
self.assertEqual(3, len(self.result.decorated._calls))
self.assertNotEqual(None, self.result.decorated._calls[2])
def test_suite():
loader = subunit.tests.TestUtil.TestLoader()
result = loader.loadTestsFromName(__name__)
return result
|