1
from nose.config import Config
3
from nose.suite import LazySuite, ContextSuite, ContextSuiteFactory, \
8
from mock import ResultProxyFactory, ResultProxy
11
class TestLazySuite(unittest.TestCase):
14
class TC(unittest.TestCase):
21
def test_test_generator(self):
23
tests = [TC('test_one'), TC('test_two')]
27
suite = LazySuite(gen_tests)
28
self.assertEqual(list([test for test in suite]), tests)
30
def test_lazy_and_nonlazy(self):
32
tests = [TC('test_one'), TC('test_two')]
37
nonlazy = LazySuite(tests)
38
lazy = LazySuite(gen_tests)
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)" \
57
def test_lazy_nonzero(self):
58
"""__nonzero__ works correctly for lazy suites"""
61
tests = [TC('test_one'), TC('test_two')]
66
lazy = LazySuite(gen_tests)
76
self.assertEqual(count, 2, "Expected 2 tests, got %s" % count)
79
def gen_tests_empty():
83
empty = LazySuite(gen_tests_empty)
86
assert False, "Loaded a test from empty suite: %s" % test
88
class TestContextSuite(unittest.TestCase):
91
class TC(unittest.TestCase):
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')])
103
assert isinstance(test.test, self.TC)
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])
113
tests = [t for t in suite3]
114
assert isinstance(tests[0], ContextSuite)
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)
122
assert len(cases) == len(tests)
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
129
def test_context_fixtures_called(self):
134
self.was_setup = True
137
self.was_torndown = True
140
suite = ContextSuite(
141
[self.TC('test_one'), self.TC('test_two')],
143
res = unittest.TestResult()
146
assert not res.errors, res.errors
147
assert not res.failures, res.failures
148
assert context.was_setup
149
assert context.was_torndown
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')
156
sys.modules['top'] = top
157
sys.modules['top.bot'] = top.bot
158
sys.modules['top.bot.end'] = top.bot.end
160
class TC(unittest.TestCase):
164
TC.__module__ = 'top.bot'
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))
172
assert top in csf.was_setup, "Ancestor not set up"
173
assert top.bot in csf.was_setup, "Context not set up"
176
assert top in csf.was_torndown, "Ancestor not torn down"
177
assert top.bot in csf.was_torndown, "Context not torn down"
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))
187
assert top in csf.was_setup
188
assert not top.bot in csf.was_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
195
def test_context_fixtures_setup_fails(self):
200
self.was_setup = True
201
assert False, "Setup failed"
204
self.was_torndown = True
207
suite = ContextSuite(
208
[self.TC('test_one'), self.TC('test_two')],
210
res = unittest.TestResult()
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
220
def test_context_fixtures_no_tests_no_setup(self):
225
self.was_setup = True
228
self.was_torndown = True
231
suite = ContextSuite([], context=context)
232
res = unittest.TestResult()
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
242
def test_result_proxy_used(self):
243
class TC(unittest.TestCase):
245
raise Exception("error")
247
ResultProxy.called[:] = []
248
res = unittest.TestResult()
251
suite = ContextSuite([TC()], resultProxy=ResultProxyFactory())
253
calls = [ c[0] for c in ResultProxy.called ]
254
self.assertEqual(calls, ['beforeTest', 'startTest',
255
'addError', 'stopTest', 'afterTest'])
258
class TestContextSuiteFactory(unittest.TestCase):
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')
265
sys.modules['top'] = top
266
sys.modules['top.bot'] = top.bot
267
sys.modules['top.bot.end'] = top.bot.end
272
P.__module__ = 'top.bot'
274
csf = ContextSuiteFactory()
275
P_ancestors = list([a for a in csf.ancestry(P)])
276
self.assertEqual(P_ancestors, [top.bot, top])
278
end_ancestors = list([a for a in csf.ancestry(top.bot.end)])
279
self.assertEqual(end_ancestors, [top.bot, top])
281
bot_ancestors = list([a for a in csf.ancestry(top.bot)])
282
self.assertEqual(bot_ancestors, [top])
284
top_ancestors = list([a for a in csf.ancestry(top)])
285
self.assertEqual(top_ancestors, [])
288
if __name__ == '__main__':
290
logging.basicConfig(level=logging.DEBUG)
293
# class TC(unittest.TestCase):
295
# raise Exception("error")
297
# ResultProxy.called[:] = []
298
# res = unittest.TestResult()