1
# Copyright (c) 2009-2010 Jonathan M. Lange. See LICENSE for details.
3
"""Tests for the RunTest single test execution logic."""
5
from testtools import (
6
ExtendedToOriginalDecorator,
12
from testtools.matchers import MatchesException, Is, Raises
13
from testtools.tests.helpers import ExtendedTestResult
16
class TestRunTest(TestCase):
24
def test___init___short(self):
26
self.assertEqual("bar", run.case)
27
self.assertEqual([], run.handlers)
29
def test__init____handlers(self):
30
handlers = [("quux", "baz")]
31
run = RunTest("bar", handlers)
32
self.assertEqual(handlers, run.handlers)
34
def test_run_with_result(self):
35
# test.run passes result down to _run_test_method.
38
def _run_test_method(self, result):
40
case = Case('_run_test_method')
41
run = RunTest(case, lambda x: log.append(x))
44
self.assertEqual(1, len(log))
45
self.assertEqual(result, log[0].decorated)
47
def test_run_no_result_manages_new_result(self):
49
run = RunTest(self.make_case(), lambda x: log.append(x) or x)
51
self.assertIsInstance(result.decorated, TestResult)
53
def test__run_core_called(self):
54
case = self.make_case()
56
run = RunTest(case, lambda x: x)
57
run._run_core = lambda: log.append('foo')
59
self.assertEqual(['foo'], log)
61
def test__run_user_does_not_catch_keyboard(self):
62
case = self.make_case()
64
raise KeyboardInterrupt("yo")
65
run = RunTest(case, None)
66
run.result = ExtendedTestResult()
67
self.assertThat(lambda: run._run_user(raises),
68
Raises(MatchesException(KeyboardInterrupt)))
69
self.assertEqual([], run.result._events)
71
def test__run_user_calls_onException(self):
72
case = self.make_case()
74
def handler(exc_info):
76
self.assertEqual(3, len(exc_info))
77
self.assertIsInstance(exc_info[1], KeyError)
78
self.assertIs(KeyError, exc_info[0])
79
case.addOnException(handler)
83
run = RunTest(case, [(KeyError, None)])
84
run.result = ExtendedTestResult()
85
status = run._run_user(raises)
86
self.assertEqual(run.exception_caught, status)
87
self.assertEqual([], run.result._events)
88
self.assertEqual(["got it"], log)
90
def test__run_user_can_catch_Exception(self):
91
case = self.make_case()
96
run = RunTest(case, [(Exception, None)])
97
run.result = ExtendedTestResult()
98
status = run._run_user(raises)
99
self.assertEqual(run.exception_caught, status)
100
self.assertEqual([], run.result._events)
101
self.assertEqual([], log)
103
def test__run_user_uncaught_Exception_raised(self):
104
case = self.make_case()
109
def log_exc(self, result, err):
110
log.append((result, err))
111
run = RunTest(case, [(ValueError, log_exc)])
112
run.result = ExtendedTestResult()
113
self.assertThat(lambda: run._run_user(raises),
114
Raises(MatchesException(KeyError)))
115
self.assertEqual([], run.result._events)
116
self.assertEqual([], log)
118
def test__run_user_uncaught_Exception_from_exception_handler_raised(self):
119
case = self.make_case()
120
def broken_handler(exc_info):
121
# ValueError because thats what we know how to catch - and must
123
raise ValueError('boo')
124
case.addOnException(broken_handler)
129
def log_exc(self, result, err):
130
log.append((result, err))
131
run = RunTest(case, [(ValueError, log_exc)])
132
run.result = ExtendedTestResult()
133
self.assertThat(lambda: run._run_user(raises),
134
Raises(MatchesException(ValueError)))
135
self.assertEqual([], run.result._events)
136
self.assertEqual([], log)
138
def test__run_user_returns_result(self):
139
case = self.make_case()
143
run.result = ExtendedTestResult()
144
self.assertEqual(1, run._run_user(returns))
145
self.assertEqual([], run.result._events)
147
def test__run_one_decorates_result(self):
150
def _run_prepared_result(self, result):
153
run = Run(self.make_case(), lambda x: x)
154
result = run._run_one('foo')
155
self.assertEqual([result], log)
156
self.assertIsInstance(log[0], ExtendedToOriginalDecorator)
157
self.assertEqual('foo', result.decorated)
159
def test__run_prepared_result_calls_start_and_stop_test(self):
160
result = ExtendedTestResult()
161
case = self.make_case()
162
run = RunTest(case, lambda x: x)
166
('addSuccess', case),
170
def test__run_prepared_result_calls_stop_test_always(self):
171
result = ExtendedTestResult()
172
case = self.make_case()
174
raise Exception("foo")
175
run = RunTest(case, lambda x: x)
176
run._run_core = inner
177
self.assertThat(lambda: run.run(result),
178
Raises(MatchesException(Exception("foo"))))
185
class CustomRunTest(RunTest):
189
def run(self, result=None):
193
class TestTestCaseSupportForRunTest(TestCase):
195
def test_pass_custom_run_test(self):
196
class SomeCase(TestCase):
199
result = TestResult()
200
case = SomeCase('test_foo', runTest=CustomRunTest)
201
from_run_test = case.run(result)
202
self.assertThat(from_run_test, Is(CustomRunTest.marker))
204
def test_default_is_runTest_class_variable(self):
205
class SomeCase(TestCase):
206
run_tests_with = CustomRunTest
209
result = TestResult()
210
case = SomeCase('test_foo')
211
from_run_test = case.run(result)
212
self.assertThat(from_run_test, Is(CustomRunTest.marker))
214
def test_constructor_argument_overrides_class_variable(self):
215
# If a 'runTest' argument is passed to the test's constructor, that
216
# overrides the class variable.
218
class DifferentRunTest(RunTest):
219
def run(self, result=None):
221
class SomeCase(TestCase):
222
run_tests_with = CustomRunTest
225
result = TestResult()
226
case = SomeCase('test_foo', runTest=DifferentRunTest)
227
from_run_test = case.run(result)
228
self.assertThat(from_run_test, Is(marker))
230
def test_decorator_for_run_test(self):
231
# Individual test methods can be marked as needing a special runner.
232
class SomeCase(TestCase):
233
@run_test_with(CustomRunTest)
236
result = TestResult()
237
case = SomeCase('test_foo')
238
from_run_test = case.run(result)
239
self.assertThat(from_run_test, Is(CustomRunTest.marker))
241
def test_extended_decorator_for_run_test(self):
242
# Individual test methods can be marked as needing a special runner.
243
# Extra arguments can be passed to the decorator which will then be
244
# passed on to the RunTest object.
246
class FooRunTest(RunTest):
247
def __init__(self, case, handlers=None, bar=None):
248
super(FooRunTest, self).__init__(case, handlers)
250
def run(self, result=None):
252
class SomeCase(TestCase):
253
@run_test_with(FooRunTest, bar=marker)
256
result = TestResult()
257
case = SomeCase('test_foo')
258
from_run_test = case.run(result)
259
self.assertThat(from_run_test, Is(marker))
261
def test_works_as_inner_decorator(self):
262
# Even if run_test_with is the innermost decorator, it will be
264
def wrapped(function):
265
"""Silly, trivial decorator."""
266
def decorated(*args, **kwargs):
267
return function(*args, **kwargs)
268
decorated.__name__ = function.__name__
269
decorated.__dict__.update(function.__dict__)
271
class SomeCase(TestCase):
273
@run_test_with(CustomRunTest)
276
result = TestResult()
277
case = SomeCase('test_foo')
278
from_run_test = case.run(result)
279
self.assertThat(from_run_test, Is(CustomRunTest.marker))
281
def test_constructor_overrides_decorator(self):
282
# If a 'runTest' argument is passed to the test's constructor, that
283
# overrides the decorator.
285
class DifferentRunTest(RunTest):
286
def run(self, result=None):
288
class SomeCase(TestCase):
289
@run_test_with(CustomRunTest)
292
result = TestResult()
293
case = SomeCase('test_foo', runTest=DifferentRunTest)
294
from_run_test = case.run(result)
295
self.assertThat(from_run_test, Is(marker))
299
from unittest import TestLoader
300
return TestLoader().loadTestsFromName(__name__)