4
from difflib import ndiff
5
from cStringIO import StringIO
7
from nose.config import Config
8
from nose.plugins.manager import PluginManager
9
from nose.plugins.skip import Skip
10
from nose import loader
11
from nose import suite
13
support = os.path.abspath(os.path.join(os.path.dirname(__file__), 'support'))
15
class TestNoseTestLoader(unittest.TestCase):
18
self._mods = sys.modules.copy()
19
suite.ContextSuiteFactory.suiteClass = TreePrintContextSuite
22
to_del = [ m for m in sys.modules.keys() if
27
sys.modules.update(self._mods)
28
suite.ContextSuiteFactory.suiteClass = suite.ContextSuite
30
def test_load_from_name_file(self):
31
res = unittest.TestResult()
32
wd = os.path.join(support, 'package1')
33
l = loader.TestLoader(workingDir=wd)
35
file_suite = l.loadTestsFromName('tests/test_example_function.py')
37
assert not res.errors, res.errors
38
assert not res.failures, res.failures
40
def test_load_from_name_dot(self):
41
res = unittest.TestResult()
42
wd = os.path.join(support, 'package1')
43
l = loader.TestLoader(workingDir=wd)
44
dir_suite = l.loadTestsFromName('.')
46
assert not res.errors, res.errors
47
assert not res.failures, res.failures
49
def test_load_from_name_file_callable(self):
50
res = unittest.TestResult()
51
wd = os.path.join(support, 'package1')
52
l = loader.TestLoader(workingDir=wd)
53
suite = l.loadTestsFromName(
54
'tests/test_example_function.py:test_times_two')
56
assert not res.errors, res.errors
57
assert not res.failures, res.failures
58
self.assertEqual(res.testsRun, 1)
60
def test_fixture_context(self):
61
res = unittest.TestResult()
62
wd = os.path.join(support, 'package2')
63
l = loader.TestLoader(workingDir=wd)
64
dir_suite = l.loadTestsFromName('.')
67
m = sys.modules['test_pak']
68
print "test pak state", m.state
70
assert not res.errors, res.errors
71
assert not res.failures, res.failures
72
self.assertEqual(res.testsRun, 5)
74
# Expected order of calls
75
expect = ['test_pak.setup',
76
'test_pak.test_mod.setup',
77
'test_pak.test_mod.test_add',
78
'test_pak.test_mod.test_minus',
79
'test_pak.test_mod.teardown',
80
'test_pak.test_sub.setup',
81
'test_pak.test_sub.test_mod.setup',
82
'test_pak.test_sub.test_mod.TestMaths.setup_class',
83
'test_pak.test_sub.test_mod.TestMaths.setup',
84
'test_pak.test_sub.test_mod.TestMaths.test_div',
85
'test_pak.test_sub.test_mod.TestMaths.teardown',
86
'test_pak.test_sub.test_mod.TestMaths.setup',
87
'test_pak.test_sub.test_mod.TestMaths.test_two_two',
88
'test_pak.test_sub.test_mod.TestMaths.teardown',
89
'test_pak.test_sub.test_mod.TestMaths.teardown_class',
90
'test_pak.test_sub.test_mod.test',
91
'test_pak.test_sub.test_mod.teardown',
92
'test_pak.test_sub.teardown',
94
self.assertEqual(len(m.state), len(expect))
96
self.assertEqual(item, expect.pop(0))
98
def test_fixture_context_name_is_module(self):
99
res = unittest.TestResult()
100
wd = os.path.join(support, 'package2')
101
l = loader.TestLoader(workingDir=wd)
102
suite = l.loadTestsFromName('test_pak.test_mod')
105
assert 'test_pak' in sys.modules, \
106
"Context did not load test_pak"
107
m = sys.modules['test_pak']
108
print "test pak state", m.state
109
expect = ['test_pak.setup',
110
'test_pak.test_mod.setup',
111
'test_pak.test_mod.test_add',
112
'test_pak.test_mod.test_minus',
113
'test_pak.test_mod.teardown',
115
self.assertEqual(len(m.state), len(expect))
117
self.assertEqual(item, expect.pop(0))
119
def test_fixture_context_name_is_test_function(self):
120
res = unittest.TestResult()
121
wd = os.path.join(support, 'package2')
122
l = loader.TestLoader(workingDir=wd)
123
suite = l.loadTestsFromName('test_pak.test_mod:test_add')
126
assert 'test_pak' in sys.modules, \
127
"Context did not load test_pak"
128
m = sys.modules['test_pak']
129
print "test pak state", m.state
130
expect = ['test_pak.setup',
131
'test_pak.test_mod.setup',
132
'test_pak.test_mod.test_add',
133
'test_pak.test_mod.teardown',
135
self.assertEqual(len(m.state), len(expect))
137
self.assertEqual(item, expect.pop(0))
139
def test_fixture_context_name_is_test_class(self):
140
res = unittest.TestResult()
141
wd = os.path.join(support, 'package2')
142
l = loader.TestLoader(workingDir=wd)
143
suite = l.loadTestsFromName(
144
'test_pak.test_sub.test_mod:TestMaths')
147
assert 'test_pak' in sys.modules, \
148
"Context did not load test_pak"
149
m = sys.modules['test_pak']
150
# print "test pak state", m.state
151
expect = ['test_pak.setup',
152
'test_pak.test_sub.setup',
153
'test_pak.test_sub.test_mod.setup',
154
'test_pak.test_sub.test_mod.TestMaths.setup_class',
155
'test_pak.test_sub.test_mod.TestMaths.setup',
156
'test_pak.test_sub.test_mod.TestMaths.test_div',
157
'test_pak.test_sub.test_mod.TestMaths.teardown',
158
'test_pak.test_sub.test_mod.TestMaths.setup',
159
'test_pak.test_sub.test_mod.TestMaths.test_two_two',
160
'test_pak.test_sub.test_mod.TestMaths.teardown',
161
'test_pak.test_sub.test_mod.TestMaths.teardown_class',
162
'test_pak.test_sub.test_mod.teardown',
163
'test_pak.test_sub.teardown',
165
self.assertEqual(m.state, expect, diff(expect, m.state))
167
def test_fixture_context_name_is_test_class_test(self):
168
res = unittest.TestResult()
169
wd = os.path.join(support, 'package2')
170
l = loader.TestLoader(workingDir=wd)
171
suite = l.loadTestsFromName(
172
'test_pak.test_sub.test_mod:TestMaths.test_div')
175
assert 'test_pak' in sys.modules, \
176
"Context not load test_pak"
177
m = sys.modules['test_pak']
178
print "test pak state", m.state
179
expect = ['test_pak.setup',
180
'test_pak.test_sub.setup',
181
'test_pak.test_sub.test_mod.setup',
182
'test_pak.test_sub.test_mod.TestMaths.setup_class',
183
'test_pak.test_sub.test_mod.TestMaths.setup',
184
'test_pak.test_sub.test_mod.TestMaths.test_div',
185
'test_pak.test_sub.test_mod.TestMaths.teardown',
186
'test_pak.test_sub.test_mod.TestMaths.teardown_class',
187
'test_pak.test_sub.test_mod.teardown',
188
'test_pak.test_sub.teardown',
190
self.assertEqual(m.state, expect, diff(expect, m.state))
192
def test_fixture_context_multiple_names(self):
193
res = unittest.TestResult()
194
wd = os.path.join(support, 'package2')
195
l = loader.TestLoader(workingDir=wd)
196
suite = l.loadTestsFromNames(
197
['test_pak.test_sub.test_mod:TestMaths.test_div',
198
'test_pak.test_sub.test_mod:TestMaths.test_two_two',
199
'test_pak.test_mod:test_add'])
202
assert not res.errors, res.errors
203
assert not res.failures, res.failures
204
assert 'test_pak' in sys.modules, \
205
"Context not load test_pak"
206
m = sys.modules['test_pak']
207
print "test pak state", m.state
208
expect = ['test_pak.setup',
209
'test_pak.test_sub.setup',
210
'test_pak.test_sub.test_mod.setup',
211
'test_pak.test_sub.test_mod.TestMaths.setup_class',
212
'test_pak.test_sub.test_mod.TestMaths.setup',
213
'test_pak.test_sub.test_mod.TestMaths.test_div',
214
'test_pak.test_sub.test_mod.TestMaths.teardown',
215
'test_pak.test_sub.test_mod.TestMaths.setup',
216
'test_pak.test_sub.test_mod.TestMaths.test_two_two',
217
'test_pak.test_sub.test_mod.TestMaths.teardown',
218
'test_pak.test_sub.test_mod.TestMaths.teardown_class',
219
'test_pak.test_sub.test_mod.teardown',
220
'test_pak.test_sub.teardown',
221
'test_pak.test_mod.setup',
222
'test_pak.test_mod.test_add',
223
'test_pak.test_mod.teardown',
225
self.assertEqual(m.state, expect, diff(expect, m.state))
227
def test_fixture_context_multiple_names_some_common_ancestors(self):
228
stream = unittest._WritelnDecorator(StringIO())
229
res = unittest._TextTestResult(stream, 0, 2)
230
wd = os.path.join(support, 'ltfn')
231
l = loader.TestLoader(workingDir=wd)
232
suite = l.loadTestsFromNames(
233
['test_pak1.test_mod',
234
'test_pak2:test_two_two',
235
'test_pak1:test_one_one'])
239
print stream.getvalue()
240
assert not res.errors, res.errors
241
assert not res.failures, res.failures
242
assert 'state' in sys.modules, \
243
"Context not load state module"
244
m = sys.modules['state']
245
print "state", m.called
247
expect = ['test_pak1.setup',
248
'test_pak1.test_mod.setup',
249
'test_pak1.test_mod.test_one_mod_one',
250
'test_pak1.test_mod.teardown',
251
'test_pak1.test_one_one',
252
'test_pak1.teardown',
254
'test_pak2.test_two_two',
255
'test_pak2.teardown']
256
self.assertEqual(m.called, expect, diff(expect, m.called))
258
def test_fixture_context_multiple_names_no_common_ancestors(self):
259
stream = unittest._WritelnDecorator(StringIO())
260
res = unittest._TextTestResult(stream, 0, 2)
261
wd = os.path.join(support, 'ltfn')
262
l = loader.TestLoader(workingDir=wd)
263
suite = l.loadTestsFromNames(
264
['test_pak1.test_mod',
265
'test_pak2:test_two_two',
270
print stream.getvalue()
271
assert not res.errors, res.errors
272
assert not res.failures, res.failures
273
assert 'state' in sys.modules, \
274
"Context not load state module"
275
m = sys.modules['state']
276
print "state", m.called
278
expect = ['test_pak1.setup',
279
'test_pak1.test_mod.setup',
280
'test_pak1.test_mod.test_one_mod_one',
281
'test_pak1.test_mod.teardown',
282
'test_pak1.teardown',
284
'test_pak2.test_two_two',
285
'test_pak2.teardown',
289
self.assertEqual(m.called, expect, diff(expect, m.called))
291
def test_mod_setup_fails_no_tests_run(self):
292
ctx = os.path.join(support, 'ctx')
293
l = loader.TestLoader(workingDir=ctx)
294
suite = l.loadTestsFromName('mod_setup_fails.py')
296
res = unittest.TestResult()
300
assert not res.failures, res.failures
301
assert res.testsRun == 0, \
302
"Expected to run 0 tests but ran %s" % res.testsRun
304
def test_mod_setup_skip_no_tests_run_no_errors(self):
305
config = Config(plugins=PluginManager(plugins=[Skip()]))
306
ctx = os.path.join(support, 'ctx')
307
l = loader.TestLoader(workingDir=ctx, config=config)
308
suite = l.loadTestsFromName('mod_setup_skip.py')
310
res = unittest.TestResult()
313
assert not suite.was_setup, "Suite setup did not fail"
314
assert not res.errors, res.errors
315
assert not res.failures, res.failures
317
assert res.testsRun == 0, \
318
"Expected to run 0 tests but ran %s" % res.testsRun
320
def test_mod_import_skip_one_test_no_errors(self):
321
config = Config(plugins=PluginManager(plugins=[Skip()]))
322
ctx = os.path.join(support, 'ctx')
323
l = loader.TestLoader(workingDir=ctx, config=config)
324
suite = l.loadTestsFromName('mod_import_skip.py')
326
res = unittest.TestResult()
329
assert not res.errors, res.errors
330
assert not res.failures, res.failures
331
assert res.testsRun == 1, \
332
"Expected to run 1 tests but ran %s" % res.testsRun
334
def test_failed_import(self):
335
ctx = os.path.join(support, 'ctx')
336
l = loader.TestLoader(workingDir=ctx)
337
suite = l.loadTestsFromName('no_such_module.py')
339
res = unittest._TextTestResult(
340
stream=unittest._WritelnDecorator(sys.stdout),
341
descriptions=0, verbosity=1)
346
assert res.errors, "Expected errors but got none"
347
assert not res.failures, res.failures
348
assert res.testsRun == 1, \
349
"Expected to run 1 tests but ran %s" % res.testsRun
351
def test_failed_import_module_name(self):
352
ctx = os.path.join(support, 'ctx')
353
l = loader.TestLoader(workingDir=ctx)
354
suite = l.loadTestsFromName('no_such_module')
356
res = unittest._TextTestResult(
357
stream=unittest._WritelnDecorator(sys.stdout),
358
descriptions=0, verbosity=1)
362
assert res.errors, "Expected errors but got none"
363
assert not res.failures, res.failures
364
err = res.errors[0][0].test.exc_class
365
assert err is ImportError, \
366
"Expected import error, got %s" % err
368
def test_load_nonsense_name(self):
369
ctx = os.path.join(support, 'ctx')
370
l = loader.TestLoader(workingDir=ctx)
371
suite = l.loadTestsFromName('fred!')
373
res = unittest._TextTestResult(
374
stream=unittest._WritelnDecorator(sys.stdout),
375
descriptions=0, verbosity=1)
378
assert res.errors, "Expected errors but got none"
379
assert not res.failures, res.failures
381
def test_generator_with_closure(self):
382
"""Test that a generator test can employ a closure
384
Issue #3. If the generator binds early, the last value
385
of the closure will be seen for each generated test and
388
gen = os.path.join(support, 'gen')
389
l = loader.TestLoader(workingDir=gen)
390
suite = l.loadTestsFromName('test')
391
res = unittest._TextTestResult(
392
stream=unittest._WritelnDecorator(sys.stdout),
393
descriptions=0, verbosity=1)
395
assert not res.errors
396
self.assertEqual(res.testsRun, 5)
399
# used for comparing lists
401
return '\n' + '\n'.join([ l for l in ndiff(a, b)
402
if not l.startswith(' ') ])
405
# used for context debugging
406
class TreePrintContextSuite(suite.ContextSuite):
410
print self, 'setup -->'
411
suite.ContextSuite.setUp(self)
412
TreePrintContextSuite.indent += ' '
415
TreePrintContextSuite.indent = TreePrintContextSuite.indent[:-2]
417
suite.ContextSuite.tearDown(self)
419
print self, 'teardown <--'
422
return '%s<%s>' % (self.indent,
423
getattr(self.context, '__name__', self.context))
427
if __name__ == '__main__':
429
#logging.basicConfig() #level=logging.DEBUG)
430
#logging.getLogger('nose.suite').setLevel(logging.DEBUG)