testtools: Fix included testtools, for systems that don't have it.
[nivanova/samba-autobuild/.git] / lib / testtools / testtools / tests / test_runtest.py
1 # Copyright (c) 2009 Jonathan M. Lange. See LICENSE for details.
2
3 """Tests for the RunTest single test execution logic."""
4
5 from testtools import (
6     ExtendedToOriginalDecorator,
7     RunTest,
8     TestCase,
9     TestResult,
10     )
11 from testtools.tests.helpers import ExtendedTestResult
12
13
14 class TestRunTest(TestCase):
15
16     def make_case(self):
17         class Case(TestCase):
18             def test(self):
19                 pass
20         return Case('test')
21
22     def test___init___short(self):
23         run = RunTest("bar")
24         self.assertEqual("bar", run.case)
25         self.assertEqual([], run.handlers)
26
27     def test__init____handlers(self):
28         handlers = [("quux", "baz")]
29         run = RunTest("bar", handlers)
30         self.assertEqual(handlers, run.handlers)
31
32     def test_run_with_result(self):
33         # test.run passes result down to _run_test_method.
34         log = []
35         class Case(TestCase):
36             def _run_test_method(self, result):
37                 log.append(result)
38         case = Case('_run_test_method')
39         run = RunTest(case, lambda x: log.append(x))
40         result = TestResult()
41         run.run(result)
42         self.assertEqual(1, len(log))
43         self.assertEqual(result, log[0].decorated)
44
45     def test_run_no_result_manages_new_result(self):
46         log = []
47         run = RunTest(self.make_case(), lambda x: log.append(x) or x)
48         result = run.run()
49         self.assertIsInstance(result.decorated, TestResult)
50
51     def test__run_core_called(self):
52         case = self.make_case()
53         log = []
54         run = RunTest(case, lambda x: x)
55         run._run_core = lambda: log.append('foo')
56         run.run()
57         self.assertEqual(['foo'], log)
58
59     def test__run_user_does_not_catch_keyboard(self):
60         case = self.make_case()
61         def raises():
62             raise KeyboardInterrupt("yo")
63         run = RunTest(case, None)
64         run.result = ExtendedTestResult()
65         self.assertRaises(KeyboardInterrupt, run._run_user, raises)
66         self.assertEqual([], run.result._events)
67
68     def test__run_user_calls_onException(self):
69         case = self.make_case()
70         log = []
71         def handler(exc_info):
72             log.append("got it")
73             self.assertEqual(3, len(exc_info))
74             self.assertIsInstance(exc_info[1], KeyError)
75             self.assertIs(KeyError, exc_info[0])
76         case.addOnException(handler)
77         e = KeyError('Yo')
78         def raises():
79             raise e
80         def log_exc(self, result, err):
81             log.append((result, err))
82         run = RunTest(case, [(KeyError, log_exc)])
83         run.result = ExtendedTestResult()
84         status = run._run_user(raises)
85         self.assertEqual(run.exception_caught, status)
86         self.assertEqual([], run.result._events)
87         self.assertEqual(["got it", (run.result, e)], log)
88
89     def test__run_user_can_catch_Exception(self):
90         case = self.make_case()
91         e = Exception('Yo')
92         def raises():
93             raise e
94         log = []
95         def log_exc(self, result, err):
96             log.append((result, err))
97         run = RunTest(case, [(Exception, log_exc)])
98         run.result = ExtendedTestResult()
99         status = run._run_user(raises)
100         self.assertEqual(run.exception_caught, status)
101         self.assertEqual([], run.result._events)
102         self.assertEqual([(run.result, e)], log)
103
104     def test__run_user_uncaught_Exception_raised(self):
105         case = self.make_case()
106         e = KeyError('Yo')
107         def raises():
108             raise e
109         log = []
110         def log_exc(self, result, err):
111             log.append((result, err))
112         run = RunTest(case, [(ValueError, log_exc)])
113         run.result = ExtendedTestResult()
114         self.assertRaises(KeyError, run._run_user, raises)
115         self.assertEqual([], run.result._events)
116         self.assertEqual([], log)
117
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
122             # not.
123             raise ValueError('boo')
124         case.addOnException(broken_handler)
125         e = KeyError('Yo')
126         def raises():
127             raise e
128         log = []
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.assertRaises(ValueError, run._run_user, raises)
134         self.assertEqual([], run.result._events)
135         self.assertEqual([], log)
136
137     def test__run_user_returns_result(self):
138         case = self.make_case()
139         def returns():
140             return 1
141         run = RunTest(case)
142         run.result = ExtendedTestResult()
143         self.assertEqual(1, run._run_user(returns))
144         self.assertEqual([], run.result._events)
145
146     def test__run_one_decorates_result(self):
147         log = []
148         class Run(RunTest):
149             def _run_prepared_result(self, result):
150                 log.append(result)
151                 return result
152         run = Run(self.make_case(), lambda x: x)
153         result = run._run_one('foo')
154         self.assertEqual([result], log)
155         self.assertIsInstance(log[0], ExtendedToOriginalDecorator)
156         self.assertEqual('foo', result.decorated)
157
158     def test__run_prepared_result_calls_start_and_stop_test(self):
159         result = ExtendedTestResult()
160         case = self.make_case()
161         run = RunTest(case, lambda x: x)
162         run.run(result)
163         self.assertEqual([
164             ('startTest', case),
165             ('addSuccess', case),
166             ('stopTest', case),
167             ], result._events)
168
169     def test__run_prepared_result_calls_stop_test_always(self):
170         result = ExtendedTestResult()
171         case = self.make_case()
172         def inner():
173             raise Exception("foo")
174         run = RunTest(case, lambda x: x)
175         run._run_core = inner
176         self.assertRaises(Exception, run.run, result)
177         self.assertEqual([
178             ('startTest', case),
179             ('stopTest', case),
180             ], result._events)
181
182
183 def test_suite():
184     from unittest import TestLoader
185     return TestLoader().loadTestsFromName(__name__)