1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
import os
import sys
import unittest
from difflib import ndiff
from cStringIO import StringIO
from nose.config import Config
from nose.plugins.manager import PluginManager
from nose.plugins.skip import Skip
from nose import loader
from nose import suite
from nose.result import _TextTestResult
try:
# 2.7+
from unittest.runner import _WritelnDecorator
except ImportError:
from unittest import _WritelnDecorator
support = os.path.abspath(os.path.join(os.path.dirname(__file__), 'support'))
class TestNoseTestLoader(unittest.TestCase):
def setUp(self):
self._mods = sys.modules.copy()
suite.ContextSuiteFactory.suiteClass = TreePrintContextSuite
def tearDown(self):
to_del = [ m for m in sys.modules.keys() if
m not in self._mods ]
if to_del:
for mod in to_del:
del sys.modules[mod]
sys.modules.update(self._mods)
suite.ContextSuiteFactory.suiteClass = suite.ContextSuite
def test_load_from_name_file(self):
res = unittest.TestResult()
wd = os.path.join(support, 'package1')
l = loader.TestLoader(workingDir=wd)
file_suite = l.loadTestsFromName('tests/test_example_function.py')
file_suite(res)
assert not res.errors, res.errors
assert not res.failures, res.failures
def test_load_from_name_dot(self):
res = unittest.TestResult()
wd = os.path.join(support, 'package1')
l = loader.TestLoader(workingDir=wd)
dir_suite = l.loadTestsFromName('.')
dir_suite(res)
assert not res.errors, res.errors
assert not res.failures, res.failures
def test_load_from_name_file_callable(self):
res = unittest.TestResult()
wd = os.path.join(support, 'package1')
l = loader.TestLoader(workingDir=wd)
suite = l.loadTestsFromName(
'tests/test_example_function.py:test_times_two')
suite(res)
assert not res.errors, res.errors
assert not res.failures, res.failures
self.assertEqual(res.testsRun, 1)
def test_fixture_context(self):
res = unittest.TestResult()
wd = os.path.join(support, 'package2')
l = loader.TestLoader(workingDir=wd)
dir_suite = l.loadTestsFromName('.')
dir_suite(res)
m = sys.modules['test_pak']
print "test pak state", m.state
assert not res.errors, res.errors
assert not res.failures, res.failures
self.assertEqual(res.testsRun, 6)
# Expected order of calls
expect = ['test_pak.setup',
'test_pak.test_mod.setup',
'test_pak.test_mod.test_add',
'test_pak.test_mod.test_minus',
'test_pak.test_mod.teardown',
'test_pak.test_sub.setup',
'test_pak.test_sub.test_sub_init',
'test_pak.test_sub.test_mod.setup',
'test_pak.test_sub.test_mod.TestMaths.setup_class',
'test_pak.test_sub.test_mod.TestMaths.setup',
'test_pak.test_sub.test_mod.TestMaths.test_div',
'test_pak.test_sub.test_mod.TestMaths.teardown',
'test_pak.test_sub.test_mod.TestMaths.setup',
'test_pak.test_sub.test_mod.TestMaths.test_two_two',
'test_pak.test_sub.test_mod.TestMaths.teardown',
'test_pak.test_sub.test_mod.TestMaths.teardown_class',
'test_pak.test_sub.test_mod.test',
'test_pak.test_sub.test_mod.teardown',
'test_pak.test_sub.teardown',
'test_pak.teardown']
self.assertEqual(len(m.state), len(expect))
for item in m.state:
self.assertEqual(item, expect.pop(0))
def test_fixture_context_name_is_module(self):
res = unittest.TestResult()
wd = os.path.join(support, 'package2')
l = loader.TestLoader(workingDir=wd)
suite = l.loadTestsFromName('test_pak.test_mod')
suite(res)
assert 'test_pak' in sys.modules, \
"Context did not load test_pak"
m = sys.modules['test_pak']
print "test pak state", m.state
expect = ['test_pak.setup',
'test_pak.test_mod.setup',
'test_pak.test_mod.test_add',
'test_pak.test_mod.test_minus',
'test_pak.test_mod.teardown',
'test_pak.teardown']
self.assertEqual(len(m.state), len(expect))
for item in m.state:
self.assertEqual(item, expect.pop(0))
def test_fixture_context_name_is_test_function(self):
res = unittest.TestResult()
wd = os.path.join(support, 'package2')
l = loader.TestLoader(workingDir=wd)
suite = l.loadTestsFromName('test_pak.test_mod:test_add')
suite(res)
assert 'test_pak' in sys.modules, \
"Context did not load test_pak"
m = sys.modules['test_pak']
print "test pak state", m.state
expect = ['test_pak.setup',
'test_pak.test_mod.setup',
'test_pak.test_mod.test_add',
'test_pak.test_mod.teardown',
'test_pak.teardown']
self.assertEqual(len(m.state), len(expect))
for item in m.state:
self.assertEqual(item, expect.pop(0))
def test_fixture_context_name_is_test_class(self):
res = unittest.TestResult()
wd = os.path.join(support, 'package2')
l = loader.TestLoader(workingDir=wd)
suite = l.loadTestsFromName(
'test_pak.test_sub.test_mod:TestMaths')
suite(res)
assert 'test_pak' in sys.modules, \
"Context did not load test_pak"
m = sys.modules['test_pak']
# print "test pak state", m.state
expect = ['test_pak.setup',
'test_pak.test_sub.setup',
'test_pak.test_sub.test_mod.setup',
'test_pak.test_sub.test_mod.TestMaths.setup_class',
'test_pak.test_sub.test_mod.TestMaths.setup',
'test_pak.test_sub.test_mod.TestMaths.test_div',
'test_pak.test_sub.test_mod.TestMaths.teardown',
'test_pak.test_sub.test_mod.TestMaths.setup',
'test_pak.test_sub.test_mod.TestMaths.test_two_two',
'test_pak.test_sub.test_mod.TestMaths.teardown',
'test_pak.test_sub.test_mod.TestMaths.teardown_class',
'test_pak.test_sub.test_mod.teardown',
'test_pak.test_sub.teardown',
'test_pak.teardown']
self.assertEqual(m.state, expect, diff(expect, m.state))
def test_fixture_context_name_is_test_class_test(self):
res = unittest.TestResult()
wd = os.path.join(support, 'package2')
l = loader.TestLoader(workingDir=wd)
suite = l.loadTestsFromName(
'test_pak.test_sub.test_mod:TestMaths.test_div')
suite(res)
assert 'test_pak' in sys.modules, \
"Context not load test_pak"
m = sys.modules['test_pak']
print "test pak state", m.state
expect = ['test_pak.setup',
'test_pak.test_sub.setup',
'test_pak.test_sub.test_mod.setup',
'test_pak.test_sub.test_mod.TestMaths.setup_class',
'test_pak.test_sub.test_mod.TestMaths.setup',
'test_pak.test_sub.test_mod.TestMaths.test_div',
'test_pak.test_sub.test_mod.TestMaths.teardown',
'test_pak.test_sub.test_mod.TestMaths.teardown_class',
'test_pak.test_sub.test_mod.teardown',
'test_pak.test_sub.teardown',
'test_pak.teardown']
self.assertEqual(m.state, expect, diff(expect, m.state))
def test_fixture_context_multiple_names(self):
res = unittest.TestResult()
wd = os.path.join(support, 'package2')
l = loader.TestLoader(workingDir=wd)
suite = l.loadTestsFromNames(
['test_pak.test_sub.test_mod:TestMaths.test_div',
'test_pak.test_sub.test_mod:TestMaths.test_two_two',
'test_pak.test_mod:test_add'])
print suite
suite(res)
assert not res.errors, res.errors
assert not res.failures, res.failures
assert 'test_pak' in sys.modules, \
"Context not load test_pak"
m = sys.modules['test_pak']
print "test pak state", m.state
expect = ['test_pak.setup',
'test_pak.test_sub.setup',
'test_pak.test_sub.test_mod.setup',
'test_pak.test_sub.test_mod.TestMaths.setup_class',
'test_pak.test_sub.test_mod.TestMaths.setup',
'test_pak.test_sub.test_mod.TestMaths.test_div',
'test_pak.test_sub.test_mod.TestMaths.teardown',
'test_pak.test_sub.test_mod.TestMaths.setup',
'test_pak.test_sub.test_mod.TestMaths.test_two_two',
'test_pak.test_sub.test_mod.TestMaths.teardown',
'test_pak.test_sub.test_mod.TestMaths.teardown_class',
'test_pak.test_sub.test_mod.teardown',
'test_pak.test_sub.teardown',
'test_pak.test_mod.setup',
'test_pak.test_mod.test_add',
'test_pak.test_mod.teardown',
'test_pak.teardown']
self.assertEqual(m.state, expect, diff(expect, m.state))
def test_fixture_context_multiple_names_some_common_ancestors(self):
stream = _WritelnDecorator(StringIO())
res = _TextTestResult(stream, 0, 2)
wd = os.path.join(support, 'ltfn')
l = loader.TestLoader(workingDir=wd)
suite = l.loadTestsFromNames(
['test_pak1.test_mod',
'test_pak2:test_two_two',
'test_pak1:test_one_one'])
print suite
suite(res)
res.printErrors()
print stream.getvalue()
assert not res.errors, res.errors
assert not res.failures, res.failures
assert 'state' in sys.modules, \
"Context not load state module"
m = sys.modules['state']
print "state", m.called
expect = ['test_pak1.setup',
'test_pak1.test_mod.setup',
'test_pak1.test_mod.test_one_mod_one',
'test_pak1.test_mod.teardown',
'test_pak1.test_one_one',
'test_pak1.teardown',
'test_pak2.setup',
'test_pak2.test_two_two',
'test_pak2.teardown']
self.assertEqual(m.called, expect, diff(expect, m.called))
def test_fixture_context_multiple_names_no_common_ancestors(self):
stream = _WritelnDecorator(StringIO())
res = _TextTestResult(stream, 0, 2)
wd = os.path.join(support, 'ltfn')
l = loader.TestLoader(workingDir=wd)
suite = l.loadTestsFromNames(
['test_pak1.test_mod',
'test_pak2:test_two_two',
'test_mod'])
print suite
suite(res)
res.printErrors()
print stream.getvalue()
assert not res.errors, res.errors
assert not res.failures, res.failures
assert 'state' in sys.modules, \
"Context not load state module"
m = sys.modules['state']
print "state", m.called
expect = ['test_pak1.setup',
'test_pak1.test_mod.setup',
'test_pak1.test_mod.test_one_mod_one',
'test_pak1.test_mod.teardown',
'test_pak1.teardown',
'test_pak2.setup',
'test_pak2.test_two_two',
'test_pak2.teardown',
'test_mod.setup',
'test_mod.test_mod',
'test_mod.teardown']
self.assertEqual(m.called, expect, diff(expect, m.called))
def test_mod_setup_fails_no_tests_run(self):
ctx = os.path.join(support, 'ctx')
l = loader.TestLoader(workingDir=ctx)
suite = l.loadTestsFromName('mod_setup_fails.py')
res = unittest.TestResult()
suite(res)
assert res.errors
assert not res.failures, res.failures
assert res.testsRun == 0, \
"Expected to run 0 tests but ran %s" % res.testsRun
def test_mod_setup_skip_no_tests_run_no_errors(self):
config = Config(plugins=PluginManager(plugins=[Skip()]))
ctx = os.path.join(support, 'ctx')
l = loader.TestLoader(workingDir=ctx, config=config)
suite = l.loadTestsFromName('mod_setup_skip.py')
res = unittest.TestResult()
suite(res)
assert not suite.was_setup, "Suite setup did not fail"
assert not res.errors, res.errors
assert not res.failures, res.failures
assert res.skipped
assert res.testsRun == 0, \
"Expected to run 0 tests but ran %s" % res.testsRun
def test_mod_import_skip_one_test_no_errors(self):
config = Config(plugins=PluginManager(plugins=[Skip()]))
ctx = os.path.join(support, 'ctx')
l = loader.TestLoader(workingDir=ctx, config=config)
suite = l.loadTestsFromName('mod_import_skip.py')
res = unittest.TestResult()
suite(res)
assert not res.errors, res.errors
assert not res.failures, res.failures
assert res.testsRun == 1, \
"Expected to run 1 tests but ran %s" % res.testsRun
def test_failed_import(self):
ctx = os.path.join(support, 'ctx')
l = loader.TestLoader(workingDir=ctx)
suite = l.loadTestsFromName('no_such_module.py')
res = _TextTestResult(
stream=_WritelnDecorator(sys.stdout),
descriptions=0, verbosity=1)
suite(res)
print res.errors
res.printErrors()
assert res.errors, "Expected errors but got none"
assert not res.failures, res.failures
assert res.testsRun == 1, \
"Expected to run 1 tests but ran %s" % res.testsRun
def test_failed_import_module_name(self):
ctx = os.path.join(support, 'ctx')
l = loader.TestLoader(workingDir=ctx)
suite = l.loadTestsFromName('no_such_module')
res = _TextTestResult(
stream=_WritelnDecorator(sys.stdout),
descriptions=0, verbosity=1)
suite(res)
print res.errors
res.printErrors()
assert res.errors, "Expected errors but got none"
assert not res.failures, res.failures
err = res.errors[0][0].test.exc_class
assert issubclass(err, ImportError), \
"Expected import error, got %s" % err
def test_load_nonsense_name(self):
ctx = os.path.join(support, 'ctx')
l = loader.TestLoader(workingDir=ctx)
suite = l.loadTestsFromName('fred!')
res = _TextTestResult(
stream=_WritelnDecorator(sys.stdout),
descriptions=0, verbosity=1)
suite(res)
print res.errors
assert res.errors, "Expected errors but got none"
assert not res.failures, res.failures
def test_generator_with_closure(self):
"""Test that a generator test can employ a closure
Issue #3. If the generator binds early, the last value
of the closure will be seen for each generated test and
the tests will fail.
"""
gen = os.path.join(support, 'gen')
l = loader.TestLoader(workingDir=gen)
suite = l.loadTestsFromName('test')
res = _TextTestResult(
stream=_WritelnDecorator(sys.stdout),
descriptions=0, verbosity=1)
suite(res)
assert not res.errors
self.assertEqual(res.testsRun, 5)
def test_issue_269(self):
"""Test classes that raise exceptions in __init__ do not stop test run
"""
wdir = os.path.join(support, 'issue269')
l = loader.TestLoader(workingDir=wdir)
suite = l.loadTestsFromName('test_bad_class')
res = _TextTestResult(
stream=_WritelnDecorator(sys.stdout),
descriptions=0, verbosity=1)
suite(res)
print res.errors
self.assertEqual(len(res.errors), 1)
assert 'raise Exception("pow")' in res.errors[0][1]
def test_load_from_file(self):
res = unittest.TestResult()
wd = os.path.join(support, 'package2')
l = loader.TestLoader(workingDir=wd)
suite = l.loadTestsFromName('test_pak/test_sub/__init__.py')
suite(res)
assert 'test_pak' in sys.modules, \
"Context did not load test_pak"
m = sys.modules['test_pak']
print "test pak state", m.state
expect = ['test_pak.setup',
'test_pak.test_sub.setup',
'test_pak.test_sub.test_sub_init',
'test_pak.test_sub.teardown',
'test_pak.teardown']
self.assertEqual(len(m.state), len(expect))
for item in m.state:
self.assertEqual(item, expect.pop(0))
def test_load_from_sub_package(self):
res = unittest.TestResult()
wd = os.path.join(support, 'package2')
l = loader.TestLoader(workingDir=wd)
suite = l.loadTestsFromName('test_pak.test_sub')
suite(res)
assert 'test_pak' in sys.modules, \
"Context did not load test_pak"
m = sys.modules['test_pak']
print "test pak state", m.state
expect = ['test_pak.setup',
'test_pak.test_sub.setup',
'test_pak.test_sub.test_sub_init',
'test_pak.test_sub.test_mod.setup',
'test_pak.test_sub.test_mod.TestMaths.setup_class',
'test_pak.test_sub.test_mod.TestMaths.setup',
'test_pak.test_sub.test_mod.TestMaths.test_div',
'test_pak.test_sub.test_mod.TestMaths.teardown',
'test_pak.test_sub.test_mod.TestMaths.setup',
'test_pak.test_sub.test_mod.TestMaths.test_two_two',
'test_pak.test_sub.test_mod.TestMaths.teardown',
'test_pak.test_sub.test_mod.TestMaths.teardown_class',
'test_pak.test_sub.test_mod.test',
'test_pak.test_sub.test_mod.teardown',
'test_pak.test_sub.teardown',
'test_pak.teardown']
self.assertEqual(len(m.state), len(expect))
for item in m.state:
self.assertEqual(item, expect.pop(0))
# used for comparing lists
def diff(a, b):
return '\n' + '\n'.join([ l for l in ndiff(a, b)
if not l.startswith(' ') ])
# used for context debugging
class TreePrintContextSuite(suite.ContextSuite):
indent = ''
def setUp(self):
print self, 'setup -->'
suite.ContextSuite.setUp(self)
TreePrintContextSuite.indent += ' '
def tearDown(self):
TreePrintContextSuite.indent = TreePrintContextSuite.indent[:-2]
try:
suite.ContextSuite.tearDown(self)
finally:
print self, 'teardown <--'
def __repr__(self):
return '%s<%s>' % (self.indent,
getattr(self.context, '__name__', self.context))
__str__ = __repr__
if __name__ == '__main__':
#import logging
#logging.basicConfig() #level=logging.DEBUG)
#logging.getLogger('nose.suite').setLevel(logging.DEBUG)
unittest.main()
|