Imported Upstream version 1.1.2
[platform/upstream/python-nose.git] / unit_tests / test_suite.py
1 from nose.config import Config
2 from nose import case
3 from nose.suite import LazySuite, ContextSuite, ContextSuiteFactory, \
4      ContextList
5 import imp
6 import sys
7 import unittest
8 from mock import ResultProxyFactory, ResultProxy
9
10
11 class TestLazySuite(unittest.TestCase):
12
13     def setUp(self):
14         class TC(unittest.TestCase):
15             def test_one(self):
16                 pass
17             def test_two(self):
18                 pass
19         self.TC = TC
20         
21     def test_test_generator(self):
22         TC = self.TC
23         tests = [TC('test_one'), TC('test_two')]
24         def gen_tests():
25             for test in tests:
26                 yield test
27         suite = LazySuite(gen_tests)
28         self.assertEqual(list([test for test in suite]), tests)
29
30     def test_lazy_and_nonlazy(self):
31         TC = self.TC
32         tests = [TC('test_one'), TC('test_two')]
33         def gen_tests():
34             for test in tests:
35                 yield test
36
37         nonlazy = LazySuite(tests)
38         lazy = LazySuite(gen_tests)
39
40         assert lazy
41         assert nonlazy
42
43         lazytests = []
44         nonlazytests = []
45         for t in lazy:
46             print "lazy %s" % t
47             lazytests.append(t)
48         for t in nonlazy:
49             print "nonlazy %s" % t
50             nonlazytests.append(t)
51         slazy = map(str, lazytests)
52         snonlazy = map(str, nonlazytests)
53         assert slazy == snonlazy, \
54                "Lazy and Nonlazy produced different test lists (%s vs %s)" \
55                % (slazy, snonlazy)
56
57     def test_lazy_nonzero(self):
58         """__nonzero__ works correctly for lazy suites"""
59         
60         TC = self.TC
61         tests = [TC('test_one'), TC('test_two')]
62         def gen_tests():
63             for test in tests:
64                 yield test
65
66         lazy = LazySuite(gen_tests)
67         assert lazy
68         assert lazy
69         assert lazy
70
71         count = 0
72         for test in lazy:
73             print test
74             assert test
75             count += 1
76         self.assertEqual(count, 2, "Expected 2 tests, got %s" % count)
77         assert lazy
78
79         def gen_tests_empty():
80             for test in []:
81                 yield test
82             return
83         empty = LazySuite(gen_tests_empty)
84         assert not empty
85         for test in empty:
86             assert False, "Loaded a test from empty suite: %s" % test
87
88 class TestContextSuite(unittest.TestCase):
89
90     def setUp(self):
91         class TC(unittest.TestCase):
92             def test_one(self):
93                 pass
94             def test_two(self):
95                 pass
96         self.TC = TC
97
98     def test_tests_are_wrapped(self):
99         """Tests in a context suite are wrapped"""
100         suite = ContextSuite(
101             [self.TC('test_one'), self.TC('test_two')])
102         for test in suite:
103             assert isinstance(test.test, self.TC)
104
105     def test_nested_context_suites(self):
106         """Nested suites don't re-wrap"""
107         suite = ContextSuite(
108             [self.TC('test_one'), self.TC('test_two')])
109         suite2 = ContextSuite(suite)
110         suite3 = ContextSuite([suite2])
111
112         # suite3 is [suite2]
113         tests = [t for t in suite3]
114         assert isinstance(tests[0], ContextSuite)
115         # suite2 is [suite]
116         tests = [t for t in tests[0]]
117         assert isinstance(tests[0], ContextSuite)
118         # suite is full of wrapped tests
119         tests = [t for t in tests[0]]
120         cases = filter(lambda t: isinstance(t, case.Test), tests)
121         assert cases
122         assert len(cases) == len(tests)
123
124         # sub-suites knows they have a context
125         #assert suite.context is None
126         #assert suite2.context is suite
127         #assert suite3.context is suite2
128
129     def test_context_fixtures_called(self):
130         class P:
131             was_setup = False
132             was_torndown = False
133             def setup(self):
134                 self.was_setup = True
135
136             def teardown(self):
137                 self.was_torndown = True
138
139         context = P()
140         suite = ContextSuite(
141             [self.TC('test_one'), self.TC('test_two')],
142             context=context)
143         res = unittest.TestResult()
144         suite(res)
145
146         assert not res.errors, res.errors
147         assert not res.failures, res.failures
148         assert context.was_setup
149         assert context.was_torndown
150
151     def test_context_fixtures_for_ancestors(self):
152         top = imp.new_module('top')
153         top.bot = imp.new_module('top.bot')
154         top.bot.end = imp.new_module('top.bot.end')
155
156         sys.modules['top'] = top
157         sys.modules['top.bot'] = top.bot
158         sys.modules['top.bot.end'] = top.bot.end
159
160         class TC(unittest.TestCase):
161             def runTest(self):
162                 pass
163         top.bot.TC = TC
164         TC.__module__ = 'top.bot'
165
166         # suite with just TC test
167         # this suite should call top and top.bot setup
168         csf = ContextSuiteFactory()
169         suite = csf(ContextList([TC()], context=top.bot))
170
171         suite.setUp()
172         assert top in csf.was_setup, "Ancestor not set up"
173         assert top.bot in csf.was_setup, "Context not set up"
174         suite.has_run = True
175         suite.tearDown()
176         assert top in csf.was_torndown, "Ancestor not torn down"
177         assert top.bot in csf.was_torndown, "Context not torn down"
178
179         # wrapped suites
180         # the outer suite sets up its context, the inner
181         # its context only, without re-setting up the outer context
182         csf = ContextSuiteFactory()
183         inner_suite = csf(ContextList([TC()], context=top.bot)) 
184         suite = csf(ContextList(inner_suite, context=top))
185
186         suite.setUp()
187         assert top in csf.was_setup
188         assert not top.bot in csf.was_setup
189         inner_suite.setUp()
190         assert top in csf.was_setup
191         assert top.bot in csf.was_setup
192         assert csf.was_setup[top] is suite
193         assert csf.was_setup[top.bot] is inner_suite
194
195     def test_context_fixtures_setup_fails(self):
196         class P:
197             was_setup = False
198             was_torndown = False
199             def setup(self):
200                 self.was_setup = True
201                 assert False, "Setup failed"
202
203             def teardown(self):
204                 self.was_torndown = True
205
206         context = P()
207         suite = ContextSuite(
208             [self.TC('test_one'), self.TC('test_two')],
209             context=context)
210         res = unittest.TestResult()
211         suite(res)
212
213         assert not res.failures, res.failures
214         assert res.errors, res.errors
215         assert context.was_setup
216         assert not context.was_torndown
217         assert res.testsRun == 0, \
218                "Expected to run no tests but ran %s" % res.testsRun
219
220     def test_context_fixtures_no_tests_no_setup(self):
221         class P:
222             was_setup = False
223             was_torndown = False
224             def setup(self):
225                 self.was_setup = True
226
227             def teardown(self):
228                 self.was_torndown = True
229
230         context = P()
231         suite = ContextSuite([], context=context)
232         res = unittest.TestResult()
233         suite(res)
234
235         assert not res.failures, res.failures
236         assert not res.errors, res.errors
237         assert not context.was_setup
238         assert not context.was_torndown
239         assert res.testsRun == 0, \
240                "Expected to run no tests but ran %s" % res.testsRun
241
242     def test_result_proxy_used(self):
243         class TC(unittest.TestCase):
244             def runTest(self):
245                 raise Exception("error")
246             
247         ResultProxy.called[:] = []
248         res = unittest.TestResult()
249         config = Config()
250
251         suite = ContextSuite([TC()], resultProxy=ResultProxyFactory())
252         suite(res)
253         calls = [ c[0] for c in ResultProxy.called ]
254         self.assertEqual(calls, ['beforeTest', 'startTest',
255                                  'addError', 'stopTest', 'afterTest'])
256
257
258 class TestContextSuiteFactory(unittest.TestCase):
259             
260     def test_ancestry(self):
261         top = imp.new_module('top')
262         top.bot = imp.new_module('top.bot')
263         top.bot.end = imp.new_module('top.bot.end')
264         
265         sys.modules['top'] = top
266         sys.modules['top.bot'] = top.bot
267         sys.modules['top.bot.end'] = top.bot.end
268         
269         class P:
270             pass
271         top.bot.P = P
272         P.__module__ = 'top.bot'
273
274         csf = ContextSuiteFactory()
275         P_ancestors = list([a for a in csf.ancestry(P)])
276         self.assertEqual(P_ancestors, [top.bot, top])
277
278         end_ancestors = list([a for a in csf.ancestry(top.bot.end)])
279         self.assertEqual(end_ancestors, [top.bot, top])
280
281         bot_ancestors = list([a for a in csf.ancestry(top.bot)])
282         self.assertEqual(bot_ancestors, [top])
283
284         top_ancestors = list([a for a in csf.ancestry(top)])
285         self.assertEqual(top_ancestors, [])
286
287
288 if __name__ == '__main__':
289     import logging
290     logging.basicConfig(level=logging.DEBUG)
291     unittest.main()
292         
293 #     class TC(unittest.TestCase):
294 #             def runTest(self):
295 #                 raise Exception("error")
296             
297 #     ResultProxy.called[:] = []
298 #     res = unittest.TestResult()
299 #     config = Config()
300
301