1 # Copyright (c) 2009-2011 testtools developers. 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
.testresult
.doubles
import ExtendedTestResult
14 from testtools
.tests
.helpers
import FullStackRunTest
17 class TestRunTest(TestCase
):
19 run_tests_with
= FullStackRunTest
27 def test___init___short(self
):
29 self
.assertEqual("bar", run
.case
)
30 self
.assertEqual([], run
.handlers
)
32 def test__init____handlers(self
):
33 handlers
= [("quux", "baz")]
34 run
= RunTest("bar", handlers
)
35 self
.assertEqual(handlers
, run
.handlers
)
37 def test_run_with_result(self
):
38 # test.run passes result down to _run_test_method.
41 def _run_test_method(self
, result
):
43 case
= Case('_run_test_method')
44 run
= RunTest(case
, lambda x
: log
.append(x
))
47 self
.assertEqual(1, len(log
))
48 self
.assertEqual(result
, log
[0].decorated
)
50 def test_run_no_result_manages_new_result(self
):
52 run
= RunTest(self
.make_case(), lambda x
: log
.append(x
) or x
)
54 self
.assertIsInstance(result
.decorated
, TestResult
)
56 def test__run_core_called(self
):
57 case
= self
.make_case()
59 run
= RunTest(case
, lambda x
: x
)
60 run
._run
_core
= lambda: log
.append('foo')
62 self
.assertEqual(['foo'], log
)
64 def test__run_user_does_not_catch_keyboard(self
):
65 case
= self
.make_case()
67 raise KeyboardInterrupt("yo")
68 run
= RunTest(case
, None)
69 run
.result
= ExtendedTestResult()
70 self
.assertThat(lambda: run
._run
_user
(raises
),
71 Raises(MatchesException(KeyboardInterrupt)))
72 self
.assertEqual([], run
.result
._events
)
74 def test__run_user_calls_onException(self
):
75 case
= self
.make_case()
77 def handler(exc_info
):
79 self
.assertEqual(3, len(exc_info
))
80 self
.assertIsInstance(exc_info
[1], KeyError)
81 self
.assertIs(KeyError, exc_info
[0])
82 case
.addOnException(handler
)
86 run
= RunTest(case
, [(KeyError, None)])
87 run
.result
= ExtendedTestResult()
88 status
= run
._run
_user
(raises
)
89 self
.assertEqual(run
.exception_caught
, status
)
90 self
.assertEqual([], run
.result
._events
)
91 self
.assertEqual(["got it"], log
)
93 def test__run_user_can_catch_Exception(self
):
94 case
= self
.make_case()
99 run
= RunTest(case
, [(Exception, None)])
100 run
.result
= ExtendedTestResult()
101 status
= run
._run
_user
(raises
)
102 self
.assertEqual(run
.exception_caught
, status
)
103 self
.assertEqual([], run
.result
._events
)
104 self
.assertEqual([], log
)
106 def test__run_user_uncaught_Exception_raised(self
):
107 case
= self
.make_case()
112 def log_exc(self
, result
, err
):
113 log
.append((result
, err
))
114 run
= RunTest(case
, [(ValueError, log_exc
)])
115 run
.result
= ExtendedTestResult()
116 self
.assertThat(lambda: run
._run
_user
(raises
),
117 Raises(MatchesException(KeyError)))
118 self
.assertEqual([], run
.result
._events
)
119 self
.assertEqual([], log
)
121 def test__run_user_uncaught_Exception_from_exception_handler_raised(self
):
122 case
= self
.make_case()
123 def broken_handler(exc_info
):
124 # ValueError because thats what we know how to catch - and must
126 raise ValueError('boo')
127 case
.addOnException(broken_handler
)
132 def log_exc(self
, result
, err
):
133 log
.append((result
, err
))
134 run
= RunTest(case
, [(ValueError, log_exc
)])
135 run
.result
= ExtendedTestResult()
136 self
.assertThat(lambda: run
._run
_user
(raises
),
137 Raises(MatchesException(ValueError)))
138 self
.assertEqual([], run
.result
._events
)
139 self
.assertEqual([], log
)
141 def test__run_user_returns_result(self
):
142 case
= self
.make_case()
146 run
.result
= ExtendedTestResult()
147 self
.assertEqual(1, run
._run
_user
(returns
))
148 self
.assertEqual([], run
.result
._events
)
150 def test__run_one_decorates_result(self
):
153 def _run_prepared_result(self
, result
):
156 run
= Run(self
.make_case(), lambda x
: x
)
157 result
= run
._run
_one
('foo')
158 self
.assertEqual([result
], log
)
159 self
.assertIsInstance(log
[0], ExtendedToOriginalDecorator
)
160 self
.assertEqual('foo', result
.decorated
)
162 def test__run_prepared_result_calls_start_and_stop_test(self
):
163 result
= ExtendedTestResult()
164 case
= self
.make_case()
165 run
= RunTest(case
, lambda x
: x
)
169 ('addSuccess', case
),
173 def test__run_prepared_result_calls_stop_test_always(self
):
174 result
= ExtendedTestResult()
175 case
= self
.make_case()
177 raise Exception("foo")
178 run
= RunTest(case
, lambda x
: x
)
179 run
._run
_core
= inner
180 self
.assertThat(lambda: run
.run(result
),
181 Raises(MatchesException(Exception("foo"))))
188 class CustomRunTest(RunTest
):
192 def run(self
, result
=None):
196 class TestTestCaseSupportForRunTest(TestCase
):
198 def test_pass_custom_run_test(self
):
199 class SomeCase(TestCase
):
202 result
= TestResult()
203 case
= SomeCase('test_foo', runTest
=CustomRunTest
)
204 from_run_test
= case
.run(result
)
205 self
.assertThat(from_run_test
, Is(CustomRunTest
.marker
))
207 def test_default_is_runTest_class_variable(self
):
208 class SomeCase(TestCase
):
209 run_tests_with
= CustomRunTest
212 result
= TestResult()
213 case
= SomeCase('test_foo')
214 from_run_test
= case
.run(result
)
215 self
.assertThat(from_run_test
, Is(CustomRunTest
.marker
))
217 def test_constructor_argument_overrides_class_variable(self
):
218 # If a 'runTest' argument is passed to the test's constructor, that
219 # overrides the class variable.
221 class DifferentRunTest(RunTest
):
222 def run(self
, result
=None):
224 class SomeCase(TestCase
):
225 run_tests_with
= CustomRunTest
228 result
= TestResult()
229 case
= SomeCase('test_foo', runTest
=DifferentRunTest
)
230 from_run_test
= case
.run(result
)
231 self
.assertThat(from_run_test
, Is(marker
))
233 def test_decorator_for_run_test(self
):
234 # Individual test methods can be marked as needing a special runner.
235 class SomeCase(TestCase
):
236 @run_test_with(CustomRunTest
)
239 result
= TestResult()
240 case
= SomeCase('test_foo')
241 from_run_test
= case
.run(result
)
242 self
.assertThat(from_run_test
, Is(CustomRunTest
.marker
))
244 def test_extended_decorator_for_run_test(self
):
245 # Individual test methods can be marked as needing a special runner.
246 # Extra arguments can be passed to the decorator which will then be
247 # passed on to the RunTest object.
249 class FooRunTest(RunTest
):
250 def __init__(self
, case
, handlers
=None, bar
=None):
251 super(FooRunTest
, self
).__init
__(case
, handlers
)
253 def run(self
, result
=None):
255 class SomeCase(TestCase
):
256 @run_test_with(FooRunTest
, bar
=marker
)
259 result
= TestResult()
260 case
= SomeCase('test_foo')
261 from_run_test
= case
.run(result
)
262 self
.assertThat(from_run_test
, Is(marker
))
264 def test_works_as_inner_decorator(self
):
265 # Even if run_test_with is the innermost decorator, it will be
267 def wrapped(function
):
268 """Silly, trivial decorator."""
269 def decorated(*args
, **kwargs
):
270 return function(*args
, **kwargs
)
271 decorated
.__name
__ = function
.__name
__
272 decorated
.__dict
__.update(function
.__dict
__)
274 class SomeCase(TestCase
):
276 @run_test_with(CustomRunTest
)
279 result
= TestResult()
280 case
= SomeCase('test_foo')
281 from_run_test
= case
.run(result
)
282 self
.assertThat(from_run_test
, Is(CustomRunTest
.marker
))
284 def test_constructor_overrides_decorator(self
):
285 # If a 'runTest' argument is passed to the test's constructor, that
286 # overrides the decorator.
288 class DifferentRunTest(RunTest
):
289 def run(self
, result
=None):
291 class SomeCase(TestCase
):
292 @run_test_with(CustomRunTest
)
295 result
= TestResult()
296 case
= SomeCase('test_foo', runTest
=DifferentRunTest
)
297 from_run_test
= case
.run(result
)
298 self
.assertThat(from_run_test
, Is(marker
))
302 from unittest
import TestLoader
303 return TestLoader().loadTestsFromName(__name__
)