35
class TestPlaceHolder(TestCase):
37
def makePlaceHolder(self, test_id="foo", short_description=None):
38
return PlaceHolder(test_id, short_description)
40
def test_id_comes_from_constructor(self):
41
# The id() of a PlaceHolder is whatever you pass into the constructor.
42
test = PlaceHolder("test id")
43
self.assertEqual("test id", test.id())
45
def test_shortDescription_is_id(self):
46
# The shortDescription() of a PlaceHolder is the id, by default.
47
test = PlaceHolder("test id")
48
self.assertEqual(test.id(), test.shortDescription())
50
def test_shortDescription_specified(self):
51
# If a shortDescription is provided to the constructor, then
52
# shortDescription() returns that instead.
53
test = PlaceHolder("test id", "description")
54
self.assertEqual("description", test.shortDescription())
56
def test_repr_just_id(self):
57
# repr(placeholder) shows you how the object was constructed.
58
test = PlaceHolder("test id")
60
"<testtools.testcase.PlaceHolder(%s)>" % repr(test.id()),
63
def test_repr_with_description(self):
64
# repr(placeholder) shows you how the object was constructed.
65
test = PlaceHolder("test id", "description")
67
"<testtools.testcase.PlaceHolder(%r, %r)>" % (
68
test.id(), test.shortDescription()),
71
def test_counts_as_one_test(self):
72
# A placeholder test counts as one test.
73
test = self.makePlaceHolder()
74
self.assertEqual(1, test.countTestCases())
76
def test_str_is_id(self):
77
# str(placeholder) is always the id(). We are not barbarians.
78
test = self.makePlaceHolder()
79
self.assertEqual(test.id(), str(test))
81
def test_runs_as_success(self):
82
# When run, a PlaceHolder test records a success.
83
test = self.makePlaceHolder()
85
test.run(LoggingResult(log))
87
[('startTest', test), ('addSuccess', test), ('stopTest', test)],
90
def test_call_is_run(self):
91
# A PlaceHolder can be called, in which case it behaves like run.
92
test = self.makePlaceHolder()
94
test.run(LoggingResult(run_log))
96
test(LoggingResult(call_log))
97
self.assertEqual(run_log, call_log)
99
def test_runs_without_result(self):
100
# A PlaceHolder can be run without a result, in which case there's no
101
# way to actually get at the result.
102
self.makePlaceHolder().run()
104
def test_debug(self):
105
# A PlaceHolder can be debugged.
106
self.makePlaceHolder().debug()
109
class TestErrorHolder(TestCase):
111
def makeException(self):
113
raise RuntimeError("danger danger")
115
return sys.exc_info()
117
def makePlaceHolder(self, test_id="foo", error=None,
118
short_description=None):
120
error = self.makeException()
121
return ErrorHolder(test_id, error, short_description)
123
def test_id_comes_from_constructor(self):
124
# The id() of a PlaceHolder is whatever you pass into the constructor.
125
test = ErrorHolder("test id", self.makeException())
126
self.assertEqual("test id", test.id())
128
def test_shortDescription_is_id(self):
129
# The shortDescription() of a PlaceHolder is the id, by default.
130
test = ErrorHolder("test id", self.makeException())
131
self.assertEqual(test.id(), test.shortDescription())
133
def test_shortDescription_specified(self):
134
# If a shortDescription is provided to the constructor, then
135
# shortDescription() returns that instead.
136
test = ErrorHolder("test id", self.makeException(), "description")
137
self.assertEqual("description", test.shortDescription())
139
def test_repr_just_id(self):
140
# repr(placeholder) shows you how the object was constructed.
141
error = self.makeException()
142
test = ErrorHolder("test id", error)
144
"<testtools.testcase.ErrorHolder(%r, %r)>" % (test.id(), error),
147
def test_repr_with_description(self):
148
# repr(placeholder) shows you how the object was constructed.
149
error = self.makeException()
150
test = ErrorHolder("test id", error, "description")
152
"<testtools.testcase.ErrorHolder(%r, %r, %r)>" % (
153
test.id(), error, test.shortDescription()),
156
def test_counts_as_one_test(self):
157
# A placeholder test counts as one test.
158
test = self.makePlaceHolder()
159
self.assertEqual(1, test.countTestCases())
161
def test_str_is_id(self):
162
# str(placeholder) is always the id(). We are not barbarians.
163
test = self.makePlaceHolder()
164
self.assertEqual(test.id(), str(test))
166
def test_runs_as_error(self):
167
# When run, a PlaceHolder test records a success.
168
error = self.makeException()
169
test = self.makePlaceHolder(error=error)
171
test.run(LoggingResult(log))
173
[('startTest', test),
174
('addError', test, error),
175
('stopTest', test)], log)
177
def test_call_is_run(self):
178
# A PlaceHolder can be called, in which case it behaves like run.
179
test = self.makePlaceHolder()
181
test.run(LoggingResult(run_log))
183
test(LoggingResult(call_log))
184
self.assertEqual(run_log, call_log)
186
def test_runs_without_result(self):
187
# A PlaceHolder can be run without a result, in which case there's no
188
# way to actually get at the result.
189
self.makePlaceHolder().run()
191
def test_debug(self):
192
# A PlaceHolder can be debugged.
193
self.makePlaceHolder().debug()
29
196
class TestEquality(TestCase):
30
197
"""Test `TestCase`'s equality implementation."""
285
451
self.assertFalse(result.wasSuccessful())
453
def test_assertEqual_nice_formatting(self):
454
message = "These things ought not be equal."
455
a = ['apple', 'banana', 'cherry']
456
b = {'Thatcher': 'One who mends roofs of straw',
457
'Major': 'A military officer, ranked below colonel',
458
'Blair': 'To shout loudly',
459
'Brown': 'The colour of healthy human faeces'}
460
expected_error = '\n'.join(
463
'a = %s' % pformat(a),
464
'b = %s' % pformat(b),
466
expected_error = '\n'.join([
467
'Match failed. Matchee: "%r"' % b,
468
'Matcher: Annotate(%r, Equals(%r))' % (message, a),
470
'reference = %s' % pformat(a),
471
'actual = %s' % pformat(b),
475
self.assertFails(expected_error, self.assertEqual, a, b, message)
476
self.assertFails(expected_error, self.assertEquals, a, b, message)
477
self.assertFails(expected_error, self.failUnlessEqual, a, b, message)
479
def test_assertEqual_formatting_no_message(self):
482
expected_error = '\n'.join([
483
'Match failed. Matchee: "dog"',
484
'Matcher: Equals(\'cat\')',
485
'Difference: \'cat\' != \'dog\'',
488
self.assertFails(expected_error, self.assertEqual, a, b)
489
self.assertFails(expected_error, self.assertEquals, a, b)
490
self.assertFails(expected_error, self.failUnlessEqual, a, b)
288
493
class TestAddCleanup(TestCase):
289
494
"""Tests for TestCase.addCleanup."""
402
607
def raiseKeyboardInterrupt():
403
608
raise KeyboardInterrupt()
404
609
self.test.addCleanup(raiseKeyboardInterrupt)
406
KeyboardInterrupt, self.test.run, self.logging_result)
610
self.assertThat(lambda:self.test.run(self.logging_result),
611
Raises(MatchesException(KeyboardInterrupt)))
613
def test_all_errors_from_MultipleExceptions_reported(self):
614
# When a MultipleExceptions exception is caught, all the errors are
620
exc_info1 = sys.exc_info()
624
exc_info2 = sys.exc_info()
625
raise MultipleExceptions(exc_info1, exc_info2)
626
self.test.addCleanup(raiseMany)
627
self.logging_result = ExtendedTestResult()
628
self.test.run(self.logging_result)
629
self.assertEqual(['startTest', 'addError', 'stopTest'],
630
[event[0] for event in self.logging_result._events])
631
self.assertEqual(set(['traceback', 'traceback-1']),
632
set(self.logging_result._events[1][2].keys()))
408
634
def test_multipleCleanupErrorsReported(self):
409
635
# Errors from all failing cleanups are reported as separate backtraces.
823
1061
class Case(TestCase):
824
1062
def method(self):
825
1063
self.addOnException(events.index)
826
self.assertRaises(ValueError, self.onException, an_exc_info)
1064
self.assertThat(lambda: self.onException(an_exc_info),
1065
Raises(MatchesException(ValueError)))
827
1066
case = Case("method")
829
1068
self.assertThat(events, Equals([]))
1071
class TestPatchSupport(TestCase):
1073
class Case(TestCase):
1077
def test_patch(self):
1078
# TestCase.patch masks obj.attribute with the new value.
1079
self.foo = 'original'
1080
test = self.Case('test')
1081
test.patch(self, 'foo', 'patched')
1082
self.assertEqual('patched', self.foo)
1084
def test_patch_restored_after_run(self):
1085
# TestCase.patch masks obj.attribute with the new value, but restores
1086
# the original value after the test is finished.
1087
self.foo = 'original'
1088
test = self.Case('test')
1089
test.patch(self, 'foo', 'patched')
1091
self.assertEqual('original', self.foo)
1093
def test_successive_patches_apply(self):
1094
# TestCase.patch can be called multiple times per test. Each time you
1095
# call it, it overrides the original value.
1096
self.foo = 'original'
1097
test = self.Case('test')
1098
test.patch(self, 'foo', 'patched')
1099
test.patch(self, 'foo', 'second')
1100
self.assertEqual('second', self.foo)
1102
def test_successive_patches_restored_after_run(self):
1103
# TestCase.patch restores the original value, no matter how many times
1105
self.foo = 'original'
1106
test = self.Case('test')
1107
test.patch(self, 'foo', 'patched')
1108
test.patch(self, 'foo', 'second')
1110
self.assertEqual('original', self.foo)
1112
def test_patch_nonexistent_attribute(self):
1113
# TestCase.patch can be used to patch a non-existent attribute.
1114
test = self.Case('test')
1115
test.patch(self, 'doesntexist', 'patched')
1116
self.assertEqual('patched', self.doesntexist)
1118
def test_restore_nonexistent_attribute(self):
1119
# TestCase.patch can be used to patch a non-existent attribute, after
1120
# the test run, the attribute is then removed from the object.
1121
test = self.Case('test')
1122
test.patch(self, 'doesntexist', 'patched')
1125
value = getattr(self, 'doesntexist', marker)
1126
self.assertIs(marker, value)
832
1129
def test_suite():
833
1130
from unittest import TestLoader
834
1131
return TestLoader().loadTestsFromName(__name__)