| 1 |
import functools |
| 2 |
import sys |
| 3 |
import types |
| 4 |
|
| 5 |
import unittest |
| 6 |
|
| 7 |
class Test_TestLoader(unittest.TestCase): |
| 8 |
|
| 9 |
### Basic object tests |
| 10 |
################################################################ |
| 11 |
|
| 12 |
def test___init__(self): |
| 13 |
loader = unittest.TestLoader() |
| 14 |
self.assertEqual([], loader.errors) |
| 15 |
|
| 16 |
### Tests for TestLoader.loadTestsFromTestCase |
| 17 |
################################################################ |
| 18 |
|
| 19 |
# "Return a suite of all test cases contained in the TestCase-derived |
| 20 |
# class testCaseClass" |
| 21 |
def test_loadTestsFromTestCase(self): |
| 22 |
class Foo(unittest.TestCase): |
| 23 |
def test_1(self): pass |
| 24 |
def test_2(self): pass |
| 25 |
def foo_bar(self): pass |
| 26 |
|
| 27 |
tests = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) |
| 28 |
|
| 29 |
loader = unittest.TestLoader() |
| 30 |
self.assertEqual(loader.loadTestsFromTestCase(Foo), tests) |
| 31 |
|
| 32 |
# "Return a suite of all test cases contained in the TestCase-derived |
| 33 |
# class testCaseClass" |
| 34 |
# |
| 35 |
# Make sure it does the right thing even if no tests were found |
| 36 |
def test_loadTestsFromTestCase__no_matches(self): |
| 37 |
class Foo(unittest.TestCase): |
| 38 |
def foo_bar(self): pass |
| 39 |
|
| 40 |
empty_suite = unittest.TestSuite() |
| 41 |
|
| 42 |
loader = unittest.TestLoader() |
| 43 |
self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite) |
| 44 |
|
| 45 |
# "Return a suite of all test cases contained in the TestCase-derived |
| 46 |
# class testCaseClass" |
| 47 |
# |
| 48 |
# What happens if loadTestsFromTestCase() is given an object |
| 49 |
# that isn't a subclass of TestCase? Specifically, what happens |
| 50 |
# if testCaseClass is a subclass of TestSuite? |
| 51 |
# |
| 52 |
# This is checked for specifically in the code, so we better add a |
| 53 |
# test for it. |
| 54 |
def test_loadTestsFromTestCase__TestSuite_subclass(self): |
| 55 |
class NotATestCase(unittest.TestSuite): |
| 56 |
pass |
| 57 |
|
| 58 |
loader = unittest.TestLoader() |
| 59 |
try: |
| 60 |
loader.loadTestsFromTestCase(NotATestCase) |
| 61 |
except TypeError: |
| 62 |
pass |
| 63 |
else: |
| 64 |
self.fail('Should raise TypeError') |
| 65 |
|
| 66 |
# "Return a suite of all test cases contained in the TestCase-derived |
| 67 |
# class testCaseClass" |
| 68 |
# |
| 69 |
# Make sure loadTestsFromTestCase() picks up the default test method |
| 70 |
# name (as specified by TestCase), even though the method name does |
| 71 |
# not match the default TestLoader.testMethodPrefix string |
| 72 |
def test_loadTestsFromTestCase__default_method_name(self): |
| 73 |
class Foo(unittest.TestCase): |
| 74 |
def runTest(self): |
| 75 |
pass |
| 76 |
|
| 77 |
loader = unittest.TestLoader() |
| 78 |
# This has to be false for the test to succeed |
| 79 |
self.assertNotStartsWith('runTest', loader.testMethodPrefix) |
| 80 |
|
| 81 |
suite = loader.loadTestsFromTestCase(Foo) |
| 82 |
self.assertIsInstance(suite, loader.suiteClass) |
| 83 |
self.assertEqual(list(suite), [Foo('runTest')]) |
| 84 |
|
| 85 |
# "Do not load any tests from `TestCase` class itself." |
| 86 |
def test_loadTestsFromTestCase__from_TestCase(self): |
| 87 |
loader = unittest.TestLoader() |
| 88 |
|
| 89 |
suite = loader.loadTestsFromTestCase(unittest.TestCase) |
| 90 |
self.assertIsInstance(suite, loader.suiteClass) |
| 91 |
self.assertEqual(list(suite), []) |
| 92 |
|
| 93 |
# "Do not load any tests from `FunctionTestCase` class." |
| 94 |
def test_loadTestsFromTestCase__from_FunctionTestCase(self): |
| 95 |
loader = unittest.TestLoader() |
| 96 |
|
| 97 |
suite = loader.loadTestsFromTestCase(unittest.FunctionTestCase) |
| 98 |
self.assertIsInstance(suite, loader.suiteClass) |
| 99 |
self.assertEqual(list(suite), []) |
| 100 |
|
| 101 |
################################################################ |
| 102 |
### /Tests for TestLoader.loadTestsFromTestCase |
| 103 |
|
| 104 |
### Tests for TestLoader.loadTestsFromModule |
| 105 |
################################################################ |
| 106 |
|
| 107 |
# "This method searches `module` for classes derived from TestCase" |
| 108 |
def test_loadTestsFromModule__TestCase_subclass(self): |
| 109 |
m = types.ModuleType('m') |
| 110 |
class MyTestCase(unittest.TestCase): |
| 111 |
def test(self): |
| 112 |
pass |
| 113 |
m.testcase_1 = MyTestCase |
| 114 |
|
| 115 |
loader = unittest.TestLoader() |
| 116 |
suite = loader.loadTestsFromModule(m) |
| 117 |
self.assertIsInstance(suite, loader.suiteClass) |
| 118 |
|
| 119 |
expected = [loader.suiteClass([MyTestCase('test')])] |
| 120 |
self.assertEqual(list(suite), expected) |
| 121 |
|
| 122 |
# "This test ensures that internal `TestCase` subclasses are not loaded" |
| 123 |
def test_loadTestsFromModule__TestCase_subclass_internals(self): |
| 124 |
# See https://github.com/python/cpython/issues/84867 |
| 125 |
m = types.ModuleType('m') |
| 126 |
# Simulate imported names: |
| 127 |
m.TestCase = unittest.TestCase |
| 128 |
m.FunctionTestCase = unittest.FunctionTestCase |
| 129 |
|
| 130 |
loader = unittest.TestLoader() |
| 131 |
suite = loader.loadTestsFromModule(m) |
| 132 |
self.assertIsInstance(suite, loader.suiteClass) |
| 133 |
self.assertEqual(list(suite), []) |
| 134 |
|
| 135 |
# "This method searches `module` for classes derived from TestCase" |
| 136 |
# |
| 137 |
# What happens if no tests are found (no TestCase instances)? |
| 138 |
def test_loadTestsFromModule__no_TestCase_instances(self): |
| 139 |
m = types.ModuleType('m') |
| 140 |
|
| 141 |
loader = unittest.TestLoader() |
| 142 |
suite = loader.loadTestsFromModule(m) |
| 143 |
self.assertIsInstance(suite, loader.suiteClass) |
| 144 |
self.assertEqual(list(suite), []) |
| 145 |
|
| 146 |
# "This method searches `module` for classes derived from TestCase" |
| 147 |
# |
| 148 |
# What happens if no tests are found (TestCases instances, but no tests)? |
| 149 |
def test_loadTestsFromModule__no_TestCase_tests(self): |
| 150 |
m = types.ModuleType('m') |
| 151 |
class MyTestCase(unittest.TestCase): |
| 152 |
pass |
| 153 |
m.testcase_1 = MyTestCase |
| 154 |
|
| 155 |
loader = unittest.TestLoader() |
| 156 |
suite = loader.loadTestsFromModule(m) |
| 157 |
self.assertIsInstance(suite, loader.suiteClass) |
| 158 |
|
| 159 |
self.assertEqual(list(suite), [loader.suiteClass()]) |
| 160 |
|
| 161 |
# "This method searches `module` for classes derived from TestCase"s |
| 162 |
# |
| 163 |
# What happens if loadTestsFromModule() is given something other |
| 164 |
# than a module? |
| 165 |
# |
| 166 |
# XXX Currently, it succeeds anyway. This flexibility |
| 167 |
# should either be documented or loadTestsFromModule() should |
| 168 |
# raise a TypeError |
| 169 |
# |
| 170 |
# XXX Certain people are using this behaviour. We'll add a test for it |
| 171 |
def test_loadTestsFromModule__not_a_module(self): |
| 172 |
class MyTestCase(unittest.TestCase): |
| 173 |
def test(self): |
| 174 |
pass |
| 175 |
|
| 176 |
class NotAModule(object): |
| 177 |
test_2 = MyTestCase |
| 178 |
|
| 179 |
loader = unittest.TestLoader() |
| 180 |
suite = loader.loadTestsFromModule(NotAModule) |
| 181 |
|
| 182 |
reference = [unittest.TestSuite([MyTestCase('test')])] |
| 183 |
self.assertEqual(list(suite), reference) |
| 184 |
|
| 185 |
|
| 186 |
# Check that loadTestsFromModule honors a module |
| 187 |
# with a load_tests function. |
| 188 |
def test_loadTestsFromModule__load_tests(self): |
| 189 |
m = types.ModuleType('m') |
| 190 |
class MyTestCase(unittest.TestCase): |
| 191 |
def test(self): |
| 192 |
pass |
| 193 |
m.testcase_1 = MyTestCase |
| 194 |
|
| 195 |
load_tests_args = [] |
| 196 |
def load_tests(loader, tests, pattern): |
| 197 |
self.assertIsInstance(tests, unittest.TestSuite) |
| 198 |
load_tests_args.extend((loader, tests, pattern)) |
| 199 |
return tests |
| 200 |
m.load_tests = load_tests |
| 201 |
|
| 202 |
loader = unittest.TestLoader() |
| 203 |
suite = loader.loadTestsFromModule(m) |
| 204 |
self.assertIsInstance(suite, unittest.TestSuite) |
| 205 |
self.assertEqual(load_tests_args, [loader, suite, None]) |
| 206 |
|
| 207 |
# In Python 3.12, the undocumented and unofficial use_load_tests has |
| 208 |
# been removed. |
| 209 |
with self.assertRaises(TypeError): |
| 210 |
loader.loadTestsFromModule(m, False) |
| 211 |
with self.assertRaises(TypeError): |
| 212 |
loader.loadTestsFromModule(m, use_load_tests=False) |
| 213 |
|
| 214 |
def test_loadTestsFromModule__pattern(self): |
| 215 |
m = types.ModuleType('m') |
| 216 |
class MyTestCase(unittest.TestCase): |
| 217 |
def test(self): |
| 218 |
pass |
| 219 |
m.testcase_1 = MyTestCase |
| 220 |
|
| 221 |
load_tests_args = [] |
| 222 |
def load_tests(loader, tests, pattern): |
| 223 |
self.assertIsInstance(tests, unittest.TestSuite) |
| 224 |
load_tests_args.extend((loader, tests, pattern)) |
| 225 |
return tests |
| 226 |
m.load_tests = load_tests |
| 227 |
|
| 228 |
loader = unittest.TestLoader() |
| 229 |
suite = loader.loadTestsFromModule(m, pattern='testme.*') |
| 230 |
self.assertIsInstance(suite, unittest.TestSuite) |
| 231 |
self.assertEqual(load_tests_args, [loader, suite, 'testme.*']) |
| 232 |
|
| 233 |
def test_loadTestsFromModule__faulty_load_tests(self): |
| 234 |
m = types.ModuleType('m') |
| 235 |
|
| 236 |
def load_tests(loader, tests, pattern): |
| 237 |
raise TypeError('some failure') |
| 238 |
m.load_tests = load_tests |
| 239 |
|
| 240 |
loader = unittest.TestLoader() |
| 241 |
suite = loader.loadTestsFromModule(m) |
| 242 |
self.assertIsInstance(suite, unittest.TestSuite) |
| 243 |
self.assertEqual(suite.countTestCases(), 1) |
| 244 |
# Errors loading the suite are also captured for introspection. |
| 245 |
self.assertNotEqual([], loader.errors) |
| 246 |
self.assertEqual(1, len(loader.errors)) |
| 247 |
error = loader.errors[0] |
| 248 |
self.assertTrue( |
| 249 |
'Failed to call load_tests:' in error, |
| 250 |
'missing error string in %r' % error) |
| 251 |
test = list(suite)[0] |
| 252 |
|
| 253 |
self.assertRaisesRegex(TypeError, "some failure", test.m) |
| 254 |
|
| 255 |
################################################################ |
| 256 |
### /Tests for TestLoader.loadTestsFromModule() |
| 257 |
|
| 258 |
### Tests for TestLoader.loadTestsFromName() |
| 259 |
################################################################ |
| 260 |
|
| 261 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 262 |
# a module, a test case class, a TestSuite instance, a test method |
| 263 |
# within a test case class, or a callable object which returns a |
| 264 |
# TestCase or TestSuite instance." |
| 265 |
# |
| 266 |
# Is ValueError raised in response to an empty name? |
| 267 |
def test_loadTestsFromName__empty_name(self): |
| 268 |
loader = unittest.TestLoader() |
| 269 |
|
| 270 |
try: |
| 271 |
loader.loadTestsFromName('') |
| 272 |
except ValueError as e: |
| 273 |
self.assertEqual(str(e), "Empty module name") |
| 274 |
else: |
| 275 |
self.fail("TestLoader.loadTestsFromName failed to raise ValueError") |
| 276 |
|
| 277 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 278 |
# a module, a test case class, a TestSuite instance, a test method |
| 279 |
# within a test case class, or a callable object which returns a |
| 280 |
# TestCase or TestSuite instance." |
| 281 |
# |
| 282 |
# What happens when the name contains invalid characters? |
| 283 |
def test_loadTestsFromName__malformed_name(self): |
| 284 |
loader = unittest.TestLoader() |
| 285 |
|
| 286 |
suite = loader.loadTestsFromName('abc () //') |
| 287 |
error, test = self.check_deferred_error(loader, suite) |
| 288 |
expected = "Failed to import test module: abc () //" |
| 289 |
expected_regex = r"Failed to import test module: abc \(\) //" |
| 290 |
self.assertIn( |
| 291 |
expected, error, |
| 292 |
'missing error string in %r' % error) |
| 293 |
self.assertRaisesRegex( |
| 294 |
ImportError, expected_regex, getattr(test, 'abc () //')) |
| 295 |
|
| 296 |
# "The specifier name is a ``dotted name'' that may resolve ... to a |
| 297 |
# module" |
| 298 |
# |
| 299 |
# What happens when a module by that name can't be found? |
| 300 |
def test_loadTestsFromName__unknown_module_name(self): |
| 301 |
loader = unittest.TestLoader() |
| 302 |
|
| 303 |
suite = loader.loadTestsFromName('sdasfasfasdf') |
| 304 |
expected = "No module named 'sdasfasfasdf'" |
| 305 |
error, test = self.check_deferred_error(loader, suite) |
| 306 |
self.assertIn( |
| 307 |
expected, error, |
| 308 |
'missing error string in %r' % error) |
| 309 |
self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf) |
| 310 |
|
| 311 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 312 |
# a module, a test case class, a TestSuite instance, a test method |
| 313 |
# within a test case class, or a callable object which returns a |
| 314 |
# TestCase or TestSuite instance." |
| 315 |
# |
| 316 |
# What happens when the module is found, but the attribute isn't? |
| 317 |
def test_loadTestsFromName__unknown_attr_name_on_module(self): |
| 318 |
loader = unittest.TestLoader() |
| 319 |
|
| 320 |
suite = loader.loadTestsFromName('unittest.loader.sdasfasfasdf') |
| 321 |
expected = "module 'unittest.loader' has no attribute 'sdasfasfasdf'" |
| 322 |
error, test = self.check_deferred_error(loader, suite) |
| 323 |
self.assertIn( |
| 324 |
expected, error, |
| 325 |
'missing error string in %r' % error) |
| 326 |
self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf) |
| 327 |
|
| 328 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 329 |
# a module, a test case class, a TestSuite instance, a test method |
| 330 |
# within a test case class, or a callable object which returns a |
| 331 |
# TestCase or TestSuite instance." |
| 332 |
# |
| 333 |
# What happens when the module is found, but the attribute isn't? |
| 334 |
def test_loadTestsFromName__unknown_attr_name_on_package(self): |
| 335 |
loader = unittest.TestLoader() |
| 336 |
|
| 337 |
suite = loader.loadTestsFromName('unittest.sdasfasfasdf') |
| 338 |
expected = "No module named 'unittest.sdasfasfasdf'" |
| 339 |
error, test = self.check_deferred_error(loader, suite) |
| 340 |
self.assertIn( |
| 341 |
expected, error, |
| 342 |
'missing error string in %r' % error) |
| 343 |
self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf) |
| 344 |
|
| 345 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 346 |
# a module, a test case class, a TestSuite instance, a test method |
| 347 |
# within a test case class, or a callable object which returns a |
| 348 |
# TestCase or TestSuite instance." |
| 349 |
# |
| 350 |
# What happens when we provide the module, but the attribute can't be |
| 351 |
# found? |
| 352 |
def test_loadTestsFromName__relative_unknown_name(self): |
| 353 |
loader = unittest.TestLoader() |
| 354 |
|
| 355 |
suite = loader.loadTestsFromName('sdasfasfasdf', unittest) |
| 356 |
expected = "module 'unittest' has no attribute 'sdasfasfasdf'" |
| 357 |
error, test = self.check_deferred_error(loader, suite) |
| 358 |
self.assertIn( |
| 359 |
expected, error, |
| 360 |
'missing error string in %r' % error) |
| 361 |
self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf) |
| 362 |
|
| 363 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 364 |
# a module, a test case class, a TestSuite instance, a test method |
| 365 |
# within a test case class, or a callable object which returns a |
| 366 |
# TestCase or TestSuite instance." |
| 367 |
# ... |
| 368 |
# "The method optionally resolves name relative to the given module" |
| 369 |
# |
| 370 |
# Does loadTestsFromName raise ValueError when passed an empty |
| 371 |
# name relative to a provided module? |
| 372 |
# |
| 373 |
# XXX Should probably raise a ValueError instead of an AttributeError |
| 374 |
def test_loadTestsFromName__relative_empty_name(self): |
| 375 |
loader = unittest.TestLoader() |
| 376 |
|
| 377 |
suite = loader.loadTestsFromName('', unittest) |
| 378 |
error, test = self.check_deferred_error(loader, suite) |
| 379 |
expected = "has no attribute ''" |
| 380 |
self.assertIn( |
| 381 |
expected, error, |
| 382 |
'missing error string in %r' % error) |
| 383 |
self.assertRaisesRegex(AttributeError, expected, getattr(test, '')) |
| 384 |
|
| 385 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 386 |
# a module, a test case class, a TestSuite instance, a test method |
| 387 |
# within a test case class, or a callable object which returns a |
| 388 |
# TestCase or TestSuite instance." |
| 389 |
# ... |
| 390 |
# "The method optionally resolves name relative to the given module" |
| 391 |
# |
| 392 |
# What happens when an impossible name is given, relative to the provided |
| 393 |
# `module`? |
| 394 |
def test_loadTestsFromName__relative_malformed_name(self): |
| 395 |
loader = unittest.TestLoader() |
| 396 |
|
| 397 |
# XXX Should this raise AttributeError or ValueError? |
| 398 |
suite = loader.loadTestsFromName('abc () //', unittest) |
| 399 |
error, test = self.check_deferred_error(loader, suite) |
| 400 |
expected = "module 'unittest' has no attribute 'abc () //'" |
| 401 |
expected_regex = r"module 'unittest' has no attribute 'abc \(\) //'" |
| 402 |
self.assertIn( |
| 403 |
expected, error, |
| 404 |
'missing error string in %r' % error) |
| 405 |
self.assertRaisesRegex( |
| 406 |
AttributeError, expected_regex, getattr(test, 'abc () //')) |
| 407 |
|
| 408 |
# "The method optionally resolves name relative to the given module" |
| 409 |
# |
| 410 |
# Does loadTestsFromName raise TypeError when the `module` argument |
| 411 |
# isn't a module object? |
| 412 |
# |
| 413 |
# XXX Accepts the not-a-module object, ignoring the object's type |
| 414 |
# This should raise an exception or the method name should be changed |
| 415 |
# |
| 416 |
# XXX Some people are relying on this, so keep it for now |
| 417 |
def test_loadTestsFromName__relative_not_a_module(self): |
| 418 |
class MyTestCase(unittest.TestCase): |
| 419 |
def test(self): |
| 420 |
pass |
| 421 |
|
| 422 |
class NotAModule(object): |
| 423 |
test_2 = MyTestCase |
| 424 |
|
| 425 |
loader = unittest.TestLoader() |
| 426 |
suite = loader.loadTestsFromName('test_2', NotAModule) |
| 427 |
|
| 428 |
reference = [MyTestCase('test')] |
| 429 |
self.assertEqual(list(suite), reference) |
| 430 |
|
| 431 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 432 |
# a module, a test case class, a TestSuite instance, a test method |
| 433 |
# within a test case class, or a callable object which returns a |
| 434 |
# TestCase or TestSuite instance." |
| 435 |
# |
| 436 |
# Does it raise an exception if the name resolves to an invalid |
| 437 |
# object? |
| 438 |
def test_loadTestsFromName__relative_bad_object(self): |
| 439 |
m = types.ModuleType('m') |
| 440 |
m.testcase_1 = object() |
| 441 |
|
| 442 |
loader = unittest.TestLoader() |
| 443 |
try: |
| 444 |
loader.loadTestsFromName('testcase_1', m) |
| 445 |
except TypeError: |
| 446 |
pass |
| 447 |
else: |
| 448 |
self.fail("Should have raised TypeError") |
| 449 |
|
| 450 |
# "The specifier name is a ``dotted name'' that may |
| 451 |
# resolve either to ... a test case class" |
| 452 |
def test_loadTestsFromName__relative_TestCase_subclass(self): |
| 453 |
m = types.ModuleType('m') |
| 454 |
class MyTestCase(unittest.TestCase): |
| 455 |
def test(self): |
| 456 |
pass |
| 457 |
m.testcase_1 = MyTestCase |
| 458 |
|
| 459 |
loader = unittest.TestLoader() |
| 460 |
suite = loader.loadTestsFromName('testcase_1', m) |
| 461 |
self.assertIsInstance(suite, loader.suiteClass) |
| 462 |
self.assertEqual(list(suite), [MyTestCase('test')]) |
| 463 |
|
| 464 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 465 |
# a module, a test case class, a TestSuite instance, a test method |
| 466 |
# within a test case class, or a callable object which returns a |
| 467 |
# TestCase or TestSuite instance." |
| 468 |
def test_loadTestsFromName__relative_TestSuite(self): |
| 469 |
m = types.ModuleType('m') |
| 470 |
class MyTestCase(unittest.TestCase): |
| 471 |
def test(self): |
| 472 |
pass |
| 473 |
m.testsuite = unittest.TestSuite([MyTestCase('test')]) |
| 474 |
|
| 475 |
loader = unittest.TestLoader() |
| 476 |
suite = loader.loadTestsFromName('testsuite', m) |
| 477 |
self.assertIsInstance(suite, loader.suiteClass) |
| 478 |
|
| 479 |
self.assertEqual(list(suite), [MyTestCase('test')]) |
| 480 |
|
| 481 |
# "The specifier name is a ``dotted name'' that may resolve ... to |
| 482 |
# ... a test method within a test case class" |
| 483 |
def test_loadTestsFromName__relative_testmethod(self): |
| 484 |
m = types.ModuleType('m') |
| 485 |
class MyTestCase(unittest.TestCase): |
| 486 |
def test(self): |
| 487 |
pass |
| 488 |
m.testcase_1 = MyTestCase |
| 489 |
|
| 490 |
loader = unittest.TestLoader() |
| 491 |
suite = loader.loadTestsFromName('testcase_1.test', m) |
| 492 |
self.assertIsInstance(suite, loader.suiteClass) |
| 493 |
|
| 494 |
self.assertEqual(list(suite), [MyTestCase('test')]) |
| 495 |
|
| 496 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 497 |
# a module, a test case class, a TestSuite instance, a test method |
| 498 |
# within a test case class, or a callable object which returns a |
| 499 |
# TestCase or TestSuite instance." |
| 500 |
# |
| 501 |
# Does loadTestsFromName() raise the proper exception when trying to |
| 502 |
# resolve "a test method within a test case class" that doesn't exist |
| 503 |
# for the given name (relative to a provided module)? |
| 504 |
def test_loadTestsFromName__relative_invalid_testmethod(self): |
| 505 |
m = types.ModuleType('m') |
| 506 |
class MyTestCase(unittest.TestCase): |
| 507 |
def test(self): |
| 508 |
pass |
| 509 |
m.testcase_1 = MyTestCase |
| 510 |
|
| 511 |
loader = unittest.TestLoader() |
| 512 |
suite = loader.loadTestsFromName('testcase_1.testfoo', m) |
| 513 |
expected = "type object 'MyTestCase' has no attribute 'testfoo'" |
| 514 |
error, test = self.check_deferred_error(loader, suite) |
| 515 |
self.assertIn( |
| 516 |
expected, error, |
| 517 |
'missing error string in %r' % error) |
| 518 |
self.assertRaisesRegex(AttributeError, expected, test.testfoo) |
| 519 |
|
| 520 |
# "The specifier name is a ``dotted name'' that may resolve ... to |
| 521 |
# ... a callable object which returns a ... TestSuite instance" |
| 522 |
def test_loadTestsFromName__callable__TestSuite(self): |
| 523 |
m = types.ModuleType('m') |
| 524 |
testcase_1 = unittest.FunctionTestCase(lambda: None) |
| 525 |
testcase_2 = unittest.FunctionTestCase(lambda: None) |
| 526 |
def return_TestSuite(): |
| 527 |
return unittest.TestSuite([testcase_1, testcase_2]) |
| 528 |
m.return_TestSuite = return_TestSuite |
| 529 |
|
| 530 |
loader = unittest.TestLoader() |
| 531 |
suite = loader.loadTestsFromName('return_TestSuite', m) |
| 532 |
self.assertIsInstance(suite, loader.suiteClass) |
| 533 |
self.assertEqual(list(suite), [testcase_1, testcase_2]) |
| 534 |
|
| 535 |
# "The specifier name is a ``dotted name'' that may resolve ... to |
| 536 |
# ... a callable object which returns a TestCase ... instance" |
| 537 |
def test_loadTestsFromName__callable__TestCase_instance(self): |
| 538 |
m = types.ModuleType('m') |
| 539 |
testcase_1 = unittest.FunctionTestCase(lambda: None) |
| 540 |
def return_TestCase(): |
| 541 |
return testcase_1 |
| 542 |
m.return_TestCase = return_TestCase |
| 543 |
|
| 544 |
loader = unittest.TestLoader() |
| 545 |
suite = loader.loadTestsFromName('return_TestCase', m) |
| 546 |
self.assertIsInstance(suite, loader.suiteClass) |
| 547 |
self.assertEqual(list(suite), [testcase_1]) |
| 548 |
|
| 549 |
# "The specifier name is a ``dotted name'' that may resolve ... to |
| 550 |
# ... a callable object which returns a TestCase ... instance" |
| 551 |
#***************************************************************** |
| 552 |
#Override the suiteClass attribute to ensure that the suiteClass |
| 553 |
#attribute is used |
| 554 |
def test_loadTestsFromName__callable__TestCase_instance_ProperSuiteClass(self): |
| 555 |
class SubTestSuite(unittest.TestSuite): |
| 556 |
pass |
| 557 |
m = types.ModuleType('m') |
| 558 |
testcase_1 = unittest.FunctionTestCase(lambda: None) |
| 559 |
def return_TestCase(): |
| 560 |
return testcase_1 |
| 561 |
m.return_TestCase = return_TestCase |
| 562 |
|
| 563 |
loader = unittest.TestLoader() |
| 564 |
loader.suiteClass = SubTestSuite |
| 565 |
suite = loader.loadTestsFromName('return_TestCase', m) |
| 566 |
self.assertIsInstance(suite, loader.suiteClass) |
| 567 |
self.assertEqual(list(suite), [testcase_1]) |
| 568 |
|
| 569 |
# "The specifier name is a ``dotted name'' that may resolve ... to |
| 570 |
# ... a test method within a test case class" |
| 571 |
#***************************************************************** |
| 572 |
#Override the suiteClass attribute to ensure that the suiteClass |
| 573 |
#attribute is used |
| 574 |
def test_loadTestsFromName__relative_testmethod_ProperSuiteClass(self): |
| 575 |
class SubTestSuite(unittest.TestSuite): |
| 576 |
pass |
| 577 |
m = types.ModuleType('m') |
| 578 |
class MyTestCase(unittest.TestCase): |
| 579 |
def test(self): |
| 580 |
pass |
| 581 |
m.testcase_1 = MyTestCase |
| 582 |
|
| 583 |
loader = unittest.TestLoader() |
| 584 |
loader.suiteClass=SubTestSuite |
| 585 |
suite = loader.loadTestsFromName('testcase_1.test', m) |
| 586 |
self.assertIsInstance(suite, loader.suiteClass) |
| 587 |
|
| 588 |
self.assertEqual(list(suite), [MyTestCase('test')]) |
| 589 |
|
| 590 |
# "The specifier name is a ``dotted name'' that may resolve ... to |
| 591 |
# ... a callable object which returns a TestCase or TestSuite instance" |
| 592 |
# |
| 593 |
# What happens if the callable returns something else? |
| 594 |
def test_loadTestsFromName__callable__wrong_type(self): |
| 595 |
m = types.ModuleType('m') |
| 596 |
def return_wrong(): |
| 597 |
return 6 |
| 598 |
m.return_wrong = return_wrong |
| 599 |
|
| 600 |
loader = unittest.TestLoader() |
| 601 |
try: |
| 602 |
suite = loader.loadTestsFromName('return_wrong', m) |
| 603 |
except TypeError: |
| 604 |
pass |
| 605 |
else: |
| 606 |
self.fail("TestLoader.loadTestsFromName failed to raise TypeError") |
| 607 |
|
| 608 |
# "The specifier can refer to modules and packages which have not been |
| 609 |
# imported; they will be imported as a side-effect" |
| 610 |
def test_loadTestsFromName__module_not_loaded(self): |
| 611 |
# We're going to try to load this module as a side-effect, so it |
| 612 |
# better not be loaded before we try. |
| 613 |
# |
| 614 |
module_name = 'test.test_unittest.dummy' |
| 615 |
sys.modules.pop(module_name, None) |
| 616 |
|
| 617 |
loader = unittest.TestLoader() |
| 618 |
try: |
| 619 |
suite = loader.loadTestsFromName(module_name) |
| 620 |
|
| 621 |
self.assertIsInstance(suite, loader.suiteClass) |
| 622 |
self.assertEqual(list(suite), []) |
| 623 |
|
| 624 |
# module should now be loaded, thanks to loadTestsFromName() |
| 625 |
self.assertIn(module_name, sys.modules) |
| 626 |
finally: |
| 627 |
if module_name in sys.modules: |
| 628 |
del sys.modules[module_name] |
| 629 |
|
| 630 |
################################################################ |
| 631 |
### Tests for TestLoader.loadTestsFromName() |
| 632 |
|
| 633 |
### Tests for TestLoader.loadTestsFromNames() |
| 634 |
################################################################ |
| 635 |
|
| 636 |
def check_deferred_error(self, loader, suite): |
| 637 |
"""Helper function for checking that errors in loading are reported. |
| 638 |
|
| 639 |
:param loader: A loader with some errors. |
| 640 |
:param suite: A suite that should have a late bound error. |
| 641 |
:return: The first error message from the loader and the test object |
| 642 |
from the suite. |
| 643 |
""" |
| 644 |
self.assertIsInstance(suite, unittest.TestSuite) |
| 645 |
self.assertEqual(suite.countTestCases(), 1) |
| 646 |
# Errors loading the suite are also captured for introspection. |
| 647 |
self.assertNotEqual([], loader.errors) |
| 648 |
self.assertEqual(1, len(loader.errors)) |
| 649 |
error = loader.errors[0] |
| 650 |
test = list(suite)[0] |
| 651 |
return error, test |
| 652 |
|
| 653 |
# "Similar to loadTestsFromName(), but takes a sequence of names rather |
| 654 |
# than a single name." |
| 655 |
# |
| 656 |
# What happens if that sequence of names is empty? |
| 657 |
def test_loadTestsFromNames__empty_name_list(self): |
| 658 |
loader = unittest.TestLoader() |
| 659 |
|
| 660 |
suite = loader.loadTestsFromNames([]) |
| 661 |
self.assertIsInstance(suite, loader.suiteClass) |
| 662 |
self.assertEqual(list(suite), []) |
| 663 |
|
| 664 |
# "Similar to loadTestsFromName(), but takes a sequence of names rather |
| 665 |
# than a single name." |
| 666 |
# ... |
| 667 |
# "The method optionally resolves name relative to the given module" |
| 668 |
# |
| 669 |
# What happens if that sequence of names is empty? |
| 670 |
# |
| 671 |
# XXX Should this raise a ValueError or just return an empty TestSuite? |
| 672 |
def test_loadTestsFromNames__relative_empty_name_list(self): |
| 673 |
loader = unittest.TestLoader() |
| 674 |
|
| 675 |
suite = loader.loadTestsFromNames([], unittest) |
| 676 |
self.assertIsInstance(suite, loader.suiteClass) |
| 677 |
self.assertEqual(list(suite), []) |
| 678 |
|
| 679 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 680 |
# a module, a test case class, a TestSuite instance, a test method |
| 681 |
# within a test case class, or a callable object which returns a |
| 682 |
# TestCase or TestSuite instance." |
| 683 |
# |
| 684 |
# Is ValueError raised in response to an empty name? |
| 685 |
def test_loadTestsFromNames__empty_name(self): |
| 686 |
loader = unittest.TestLoader() |
| 687 |
|
| 688 |
try: |
| 689 |
loader.loadTestsFromNames(['']) |
| 690 |
except ValueError as e: |
| 691 |
self.assertEqual(str(e), "Empty module name") |
| 692 |
else: |
| 693 |
self.fail("TestLoader.loadTestsFromNames failed to raise ValueError") |
| 694 |
|
| 695 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 696 |
# a module, a test case class, a TestSuite instance, a test method |
| 697 |
# within a test case class, or a callable object which returns a |
| 698 |
# TestCase or TestSuite instance." |
| 699 |
# |
| 700 |
# What happens when presented with an impossible module name? |
| 701 |
def test_loadTestsFromNames__malformed_name(self): |
| 702 |
loader = unittest.TestLoader() |
| 703 |
|
| 704 |
# XXX Should this raise ValueError or ImportError? |
| 705 |
suite = loader.loadTestsFromNames(['abc () //']) |
| 706 |
error, test = self.check_deferred_error(loader, list(suite)[0]) |
| 707 |
expected = "Failed to import test module: abc () //" |
| 708 |
expected_regex = r"Failed to import test module: abc \(\) //" |
| 709 |
self.assertIn( |
| 710 |
expected, error, |
| 711 |
'missing error string in %r' % error) |
| 712 |
self.assertRaisesRegex( |
| 713 |
ImportError, expected_regex, getattr(test, 'abc () //')) |
| 714 |
|
| 715 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 716 |
# a module, a test case class, a TestSuite instance, a test method |
| 717 |
# within a test case class, or a callable object which returns a |
| 718 |
# TestCase or TestSuite instance." |
| 719 |
# |
| 720 |
# What happens when no module can be found for the given name? |
| 721 |
def test_loadTestsFromNames__unknown_module_name(self): |
| 722 |
loader = unittest.TestLoader() |
| 723 |
|
| 724 |
suite = loader.loadTestsFromNames(['sdasfasfasdf']) |
| 725 |
error, test = self.check_deferred_error(loader, list(suite)[0]) |
| 726 |
expected = "Failed to import test module: sdasfasfasdf" |
| 727 |
self.assertIn( |
| 728 |
expected, error, |
| 729 |
'missing error string in %r' % error) |
| 730 |
self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf) |
| 731 |
|
| 732 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 733 |
# a module, a test case class, a TestSuite instance, a test method |
| 734 |
# within a test case class, or a callable object which returns a |
| 735 |
# TestCase or TestSuite instance." |
| 736 |
# |
| 737 |
# What happens when the module can be found, but not the attribute? |
| 738 |
def test_loadTestsFromNames__unknown_attr_name(self): |
| 739 |
loader = unittest.TestLoader() |
| 740 |
|
| 741 |
suite = loader.loadTestsFromNames( |
| 742 |
['unittest.loader.sdasfasfasdf', 'test.test_unittest.dummy']) |
| 743 |
error, test = self.check_deferred_error(loader, list(suite)[0]) |
| 744 |
expected = "module 'unittest.loader' has no attribute 'sdasfasfasdf'" |
| 745 |
self.assertIn( |
| 746 |
expected, error, |
| 747 |
'missing error string in %r' % error) |
| 748 |
self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf) |
| 749 |
|
| 750 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 751 |
# a module, a test case class, a TestSuite instance, a test method |
| 752 |
# within a test case class, or a callable object which returns a |
| 753 |
# TestCase or TestSuite instance." |
| 754 |
# ... |
| 755 |
# "The method optionally resolves name relative to the given module" |
| 756 |
# |
| 757 |
# What happens when given an unknown attribute on a specified `module` |
| 758 |
# argument? |
| 759 |
def test_loadTestsFromNames__unknown_name_relative_1(self): |
| 760 |
loader = unittest.TestLoader() |
| 761 |
|
| 762 |
suite = loader.loadTestsFromNames(['sdasfasfasdf'], unittest) |
| 763 |
error, test = self.check_deferred_error(loader, list(suite)[0]) |
| 764 |
expected = "module 'unittest' has no attribute 'sdasfasfasdf'" |
| 765 |
self.assertIn( |
| 766 |
expected, error, |
| 767 |
'missing error string in %r' % error) |
| 768 |
self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf) |
| 769 |
|
| 770 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 771 |
# a module, a test case class, a TestSuite instance, a test method |
| 772 |
# within a test case class, or a callable object which returns a |
| 773 |
# TestCase or TestSuite instance." |
| 774 |
# ... |
| 775 |
# "The method optionally resolves name relative to the given module" |
| 776 |
# |
| 777 |
# Do unknown attributes (relative to a provided module) still raise an |
| 778 |
# exception even in the presence of valid attribute names? |
| 779 |
def test_loadTestsFromNames__unknown_name_relative_2(self): |
| 780 |
loader = unittest.TestLoader() |
| 781 |
|
| 782 |
suite = loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest) |
| 783 |
error, test = self.check_deferred_error(loader, list(suite)[1]) |
| 784 |
expected = "module 'unittest' has no attribute 'sdasfasfasdf'" |
| 785 |
self.assertIn( |
| 786 |
expected, error, |
| 787 |
'missing error string in %r' % error) |
| 788 |
self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf) |
| 789 |
|
| 790 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 791 |
# a module, a test case class, a TestSuite instance, a test method |
| 792 |
# within a test case class, or a callable object which returns a |
| 793 |
# TestCase or TestSuite instance." |
| 794 |
# ... |
| 795 |
# "The method optionally resolves name relative to the given module" |
| 796 |
# |
| 797 |
# What happens when faced with the empty string? |
| 798 |
# |
| 799 |
# XXX This currently raises AttributeError, though ValueError is probably |
| 800 |
# more appropriate |
| 801 |
def test_loadTestsFromNames__relative_empty_name(self): |
| 802 |
loader = unittest.TestLoader() |
| 803 |
|
| 804 |
suite = loader.loadTestsFromNames([''], unittest) |
| 805 |
error, test = self.check_deferred_error(loader, list(suite)[0]) |
| 806 |
expected = "has no attribute ''" |
| 807 |
self.assertIn( |
| 808 |
expected, error, |
| 809 |
'missing error string in %r' % error) |
| 810 |
self.assertRaisesRegex(AttributeError, expected, getattr(test, '')) |
| 811 |
|
| 812 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 813 |
# a module, a test case class, a TestSuite instance, a test method |
| 814 |
# within a test case class, or a callable object which returns a |
| 815 |
# TestCase or TestSuite instance." |
| 816 |
# ... |
| 817 |
# "The method optionally resolves name relative to the given module" |
| 818 |
# |
| 819 |
# What happens when presented with an impossible attribute name? |
| 820 |
def test_loadTestsFromNames__relative_malformed_name(self): |
| 821 |
loader = unittest.TestLoader() |
| 822 |
|
| 823 |
# XXX Should this raise AttributeError or ValueError? |
| 824 |
suite = loader.loadTestsFromNames(['abc () //'], unittest) |
| 825 |
error, test = self.check_deferred_error(loader, list(suite)[0]) |
| 826 |
expected = "module 'unittest' has no attribute 'abc () //'" |
| 827 |
expected_regex = r"module 'unittest' has no attribute 'abc \(\) //'" |
| 828 |
self.assertIn( |
| 829 |
expected, error, |
| 830 |
'missing error string in %r' % error) |
| 831 |
self.assertRaisesRegex( |
| 832 |
AttributeError, expected_regex, getattr(test, 'abc () //')) |
| 833 |
|
| 834 |
# "The method optionally resolves name relative to the given module" |
| 835 |
# |
| 836 |
# Does loadTestsFromNames() make sure the provided `module` is in fact |
| 837 |
# a module? |
| 838 |
# |
| 839 |
# XXX This validation is currently not done. This flexibility should |
| 840 |
# either be documented or a TypeError should be raised. |
| 841 |
def test_loadTestsFromNames__relative_not_a_module(self): |
| 842 |
class MyTestCase(unittest.TestCase): |
| 843 |
def test(self): |
| 844 |
pass |
| 845 |
|
| 846 |
class NotAModule(object): |
| 847 |
test_2 = MyTestCase |
| 848 |
|
| 849 |
loader = unittest.TestLoader() |
| 850 |
suite = loader.loadTestsFromNames(['test_2'], NotAModule) |
| 851 |
|
| 852 |
reference = [unittest.TestSuite([MyTestCase('test')])] |
| 853 |
self.assertEqual(list(suite), reference) |
| 854 |
|
| 855 |
# "The specifier name is a ``dotted name'' that may resolve either to |
| 856 |
# a module, a test case class, a TestSuite instance, a test method |
| 857 |
# within a test case class, or a callable object which returns a |
| 858 |
# TestCase or TestSuite instance." |
| 859 |
# |
| 860 |
# Does it raise an exception if the name resolves to an invalid |
| 861 |
# object? |
| 862 |
def test_loadTestsFromNames__relative_bad_object(self): |
| 863 |
m = types.ModuleType('m') |
| 864 |
m.testcase_1 = object() |
| 865 |
|
| 866 |
loader = unittest.TestLoader() |
| 867 |
try: |
| 868 |
loader.loadTestsFromNames(['testcase_1'], m) |
| 869 |
except TypeError: |
| 870 |
pass |
| 871 |
else: |
| 872 |
self.fail("Should have raised TypeError") |
| 873 |
|
| 874 |
# "The specifier name is a ``dotted name'' that may resolve ... to |
| 875 |
# ... a test case class" |
| 876 |
def test_loadTestsFromNames__relative_TestCase_subclass(self): |
| 877 |
m = types.ModuleType('m') |
| 878 |
class MyTestCase(unittest.TestCase): |
| 879 |
def test(self): |
| 880 |
pass |
| 881 |
m.testcase_1 = MyTestCase |
| 882 |
|
| 883 |
loader = unittest.TestLoader() |
| 884 |
suite = loader.loadTestsFromNames(['testcase_1'], m) |
| 885 |
self.assertIsInstance(suite, loader.suiteClass) |
| 886 |
|
| 887 |
expected = loader.suiteClass([MyTestCase('test')]) |
| 888 |
self.assertEqual(list(suite), [expected]) |
| 889 |
|
| 890 |
# "The specifier name is a ``dotted name'' that may resolve ... to |
| 891 |
# ... a TestSuite instance" |
| 892 |
def test_loadTestsFromNames__relative_TestSuite(self): |
| 893 |
m = types.ModuleType('m') |
| 894 |
class MyTestCase(unittest.TestCase): |
| 895 |
def test(self): |
| 896 |
pass |
| 897 |
m.testsuite = unittest.TestSuite([MyTestCase('test')]) |
| 898 |
|
| 899 |
loader = unittest.TestLoader() |
| 900 |
suite = loader.loadTestsFromNames(['testsuite'], m) |
| 901 |
self.assertIsInstance(suite, loader.suiteClass) |
| 902 |
|
| 903 |
self.assertEqual(list(suite), [m.testsuite]) |
| 904 |
|
| 905 |
# "The specifier name is a ``dotted name'' that may resolve ... to ... a |
| 906 |
# test method within a test case class" |
| 907 |
def test_loadTestsFromNames__relative_testmethod(self): |
| 908 |
m = types.ModuleType('m') |
| 909 |
class MyTestCase(unittest.TestCase): |
| 910 |
def test(self): |
| 911 |
pass |
| 912 |
m.testcase_1 = MyTestCase |
| 913 |
|
| 914 |
loader = unittest.TestLoader() |
| 915 |
suite = loader.loadTestsFromNames(['testcase_1.test'], m) |
| 916 |
self.assertIsInstance(suite, loader.suiteClass) |
| 917 |
|
| 918 |
ref_suite = unittest.TestSuite([MyTestCase('test')]) |
| 919 |
self.assertEqual(list(suite), [ref_suite]) |
| 920 |
|
| 921 |
# #14971: Make sure the dotted name resolution works even if the actual |
| 922 |
# function doesn't have the same name as is used to find it. |
| 923 |
def test_loadTestsFromName__function_with_different_name_than_method(self): |
| 924 |
# lambdas have the name '<lambda>'. |
| 925 |
m = types.ModuleType('m') |
| 926 |
class MyTestCase(unittest.TestCase): |
| 927 |
test = lambda: 1 |
| 928 |
m.testcase_1 = MyTestCase |
| 929 |
|
| 930 |
loader = unittest.TestLoader() |
| 931 |
suite = loader.loadTestsFromNames(['testcase_1.test'], m) |
| 932 |
self.assertIsInstance(suite, loader.suiteClass) |
| 933 |
|
| 934 |
ref_suite = unittest.TestSuite([MyTestCase('test')]) |
| 935 |
self.assertEqual(list(suite), [ref_suite]) |
| 936 |
|
| 937 |
# "The specifier name is a ``dotted name'' that may resolve ... to ... a |
| 938 |
# test method within a test case class" |
| 939 |
# |
| 940 |
# Does the method gracefully handle names that initially look like they |
| 941 |
# resolve to "a test method within a test case class" but don't? |
| 942 |
def test_loadTestsFromNames__relative_invalid_testmethod(self): |
| 943 |
m = types.ModuleType('m') |
| 944 |
class MyTestCase(unittest.TestCase): |
| 945 |
def test(self): |
| 946 |
pass |
| 947 |
m.testcase_1 = MyTestCase |
| 948 |
|
| 949 |
loader = unittest.TestLoader() |
| 950 |
suite = loader.loadTestsFromNames(['testcase_1.testfoo'], m) |
| 951 |
error, test = self.check_deferred_error(loader, list(suite)[0]) |
| 952 |
expected = "type object 'MyTestCase' has no attribute 'testfoo'" |
| 953 |
self.assertIn( |
| 954 |
expected, error, |
| 955 |
'missing error string in %r' % error) |
| 956 |
self.assertRaisesRegex(AttributeError, expected, test.testfoo) |
| 957 |
|
| 958 |
# "The specifier name is a ``dotted name'' that may resolve ... to |
| 959 |
# ... a callable object which returns a ... TestSuite instance" |
| 960 |
def test_loadTestsFromNames__callable__TestSuite(self): |
| 961 |
m = types.ModuleType('m') |
| 962 |
testcase_1 = unittest.FunctionTestCase(lambda: None) |
| 963 |
testcase_2 = unittest.FunctionTestCase(lambda: None) |
| 964 |
def return_TestSuite(): |
| 965 |
return unittest.TestSuite([testcase_1, testcase_2]) |
| 966 |
m.return_TestSuite = return_TestSuite |
| 967 |
|
| 968 |
loader = unittest.TestLoader() |
| 969 |
suite = loader.loadTestsFromNames(['return_TestSuite'], m) |
| 970 |
self.assertIsInstance(suite, loader.suiteClass) |
| 971 |
|
| 972 |
expected = unittest.TestSuite([testcase_1, testcase_2]) |
| 973 |
self.assertEqual(list(suite), [expected]) |
| 974 |
|
| 975 |
# "The specifier name is a ``dotted name'' that may resolve ... to |
| 976 |
# ... a callable object which returns a TestCase ... instance" |
| 977 |
def test_loadTestsFromNames__callable__TestCase_instance(self): |
| 978 |
m = types.ModuleType('m') |
| 979 |
testcase_1 = unittest.FunctionTestCase(lambda: None) |
| 980 |
def return_TestCase(): |
| 981 |
return testcase_1 |
| 982 |
m.return_TestCase = return_TestCase |
| 983 |
|
| 984 |
loader = unittest.TestLoader() |
| 985 |
suite = loader.loadTestsFromNames(['return_TestCase'], m) |
| 986 |
self.assertIsInstance(suite, loader.suiteClass) |
| 987 |
|
| 988 |
ref_suite = unittest.TestSuite([testcase_1]) |
| 989 |
self.assertEqual(list(suite), [ref_suite]) |
| 990 |
|
| 991 |
# "The specifier name is a ``dotted name'' that may resolve ... to |
| 992 |
# ... a callable object which returns a TestCase or TestSuite instance" |
| 993 |
# |
| 994 |
# Are staticmethods handled correctly? |
| 995 |
def test_loadTestsFromNames__callable__call_staticmethod(self): |
| 996 |
m = types.ModuleType('m') |
| 997 |
class Test1(unittest.TestCase): |
| 998 |
def test(self): |
| 999 |
pass |
| 1000 |
|
| 1001 |
testcase_1 = Test1('test') |
| 1002 |
class Foo(unittest.TestCase): |
| 1003 |
@staticmethod |
| 1004 |
def foo(): |
| 1005 |
return testcase_1 |
| 1006 |
m.Foo = Foo |
| 1007 |
|
| 1008 |
loader = unittest.TestLoader() |
| 1009 |
suite = loader.loadTestsFromNames(['Foo.foo'], m) |
| 1010 |
self.assertIsInstance(suite, loader.suiteClass) |
| 1011 |
|
| 1012 |
ref_suite = unittest.TestSuite([testcase_1]) |
| 1013 |
self.assertEqual(list(suite), [ref_suite]) |
| 1014 |
|
| 1015 |
# "The specifier name is a ``dotted name'' that may resolve ... to |
| 1016 |
# ... a callable object which returns a TestCase or TestSuite instance" |
| 1017 |
# |
| 1018 |
# What happens when the callable returns something else? |
| 1019 |
def test_loadTestsFromNames__callable__wrong_type(self): |
| 1020 |
m = types.ModuleType('m') |
| 1021 |
def return_wrong(): |
| 1022 |
return 6 |
| 1023 |
m.return_wrong = return_wrong |
| 1024 |
|
| 1025 |
loader = unittest.TestLoader() |
| 1026 |
try: |
| 1027 |
suite = loader.loadTestsFromNames(['return_wrong'], m) |
| 1028 |
except TypeError: |
| 1029 |
pass |
| 1030 |
else: |
| 1031 |
self.fail("TestLoader.loadTestsFromNames failed to raise TypeError") |
| 1032 |
|
| 1033 |
# "The specifier can refer to modules and packages which have not been |
| 1034 |
# imported; they will be imported as a side-effect" |
| 1035 |
def test_loadTestsFromNames__module_not_loaded(self): |
| 1036 |
# We're going to try to load this module as a side-effect, so it |
| 1037 |
# better not be loaded before we try. |
| 1038 |
# |
| 1039 |
module_name = 'test.test_unittest.dummy' |
| 1040 |
sys.modules.pop(module_name, None) |
| 1041 |
|
| 1042 |
loader = unittest.TestLoader() |
| 1043 |
try: |
| 1044 |
suite = loader.loadTestsFromNames([module_name]) |
| 1045 |
|
| 1046 |
self.assertIsInstance(suite, loader.suiteClass) |
| 1047 |
self.assertEqual(list(suite), [unittest.TestSuite()]) |
| 1048 |
|
| 1049 |
# module should now be loaded, thanks to loadTestsFromName() |
| 1050 |
self.assertIn(module_name, sys.modules) |
| 1051 |
finally: |
| 1052 |
if module_name in sys.modules: |
| 1053 |
del sys.modules[module_name] |
| 1054 |
|
| 1055 |
################################################################ |
| 1056 |
### /Tests for TestLoader.loadTestsFromNames() |
| 1057 |
|
| 1058 |
### Tests for TestLoader.getTestCaseNames() |
| 1059 |
################################################################ |
| 1060 |
|
| 1061 |
# "Return a sorted sequence of method names found within testCaseClass" |
| 1062 |
# |
| 1063 |
# Test.foobar is defined to make sure getTestCaseNames() respects |
| 1064 |
# loader.testMethodPrefix |
| 1065 |
def test_getTestCaseNames(self): |
| 1066 |
class Test(unittest.TestCase): |
| 1067 |
def test_1(self): pass |
| 1068 |
def test_2(self): pass |
| 1069 |
def foobar(self): pass |
| 1070 |
|
| 1071 |
loader = unittest.TestLoader() |
| 1072 |
|
| 1073 |
self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2']) |
| 1074 |
|
| 1075 |
# "Return a sorted sequence of method names found within testCaseClass" |
| 1076 |
# |
| 1077 |
# Does getTestCaseNames() behave appropriately if no tests are found? |
| 1078 |
def test_getTestCaseNames__no_tests(self): |
| 1079 |
class Test(unittest.TestCase): |
| 1080 |
def foobar(self): pass |
| 1081 |
|
| 1082 |
loader = unittest.TestLoader() |
| 1083 |
|
| 1084 |
self.assertEqual(loader.getTestCaseNames(Test), []) |
| 1085 |
|
| 1086 |
# "Return a sorted sequence of method names found within testCaseClass" |
| 1087 |
# |
| 1088 |
# Are not-TestCases handled gracefully? |
| 1089 |
# |
| 1090 |
# XXX This should raise a TypeError, not return a list |
| 1091 |
# |
| 1092 |
# XXX It's too late in the 2.5 release cycle to fix this, but it should |
| 1093 |
# probably be revisited for 2.6 |
| 1094 |
def test_getTestCaseNames__not_a_TestCase(self): |
| 1095 |
class BadCase(int): |
| 1096 |
def test_foo(self): |
| 1097 |
pass |
| 1098 |
|
| 1099 |
loader = unittest.TestLoader() |
| 1100 |
names = loader.getTestCaseNames(BadCase) |
| 1101 |
|
| 1102 |
self.assertEqual(names, ['test_foo']) |
| 1103 |
|
| 1104 |
# "Return a sorted sequence of method names found within testCaseClass" |
| 1105 |
# |
| 1106 |
# Make sure inherited names are handled. |
| 1107 |
# |
| 1108 |
# TestP.foobar is defined to make sure getTestCaseNames() respects |
| 1109 |
# loader.testMethodPrefix |
| 1110 |
def test_getTestCaseNames__inheritance(self): |
| 1111 |
class TestP(unittest.TestCase): |
| 1112 |
def test_1(self): pass |
| 1113 |
def test_2(self): pass |
| 1114 |
def foobar(self): pass |
| 1115 |
|
| 1116 |
class TestC(TestP): |
| 1117 |
def test_1(self): pass |
| 1118 |
def test_3(self): pass |
| 1119 |
|
| 1120 |
loader = unittest.TestLoader() |
| 1121 |
|
| 1122 |
names = ['test_1', 'test_2', 'test_3'] |
| 1123 |
self.assertEqual(loader.getTestCaseNames(TestC), names) |
| 1124 |
|
| 1125 |
# "Return a sorted sequence of method names found within testCaseClass" |
| 1126 |
# |
| 1127 |
# If TestLoader.testNamePatterns is set, only tests that match one of these |
| 1128 |
# patterns should be included. |
| 1129 |
def test_getTestCaseNames__testNamePatterns(self): |
| 1130 |
class MyTest(unittest.TestCase): |
| 1131 |
def test_1(self): pass |
| 1132 |
def test_2(self): pass |
| 1133 |
def foobar(self): pass |
| 1134 |
|
| 1135 |
loader = unittest.TestLoader() |
| 1136 |
|
| 1137 |
loader.testNamePatterns = [] |
| 1138 |
self.assertEqual(loader.getTestCaseNames(MyTest), []) |
| 1139 |
|
| 1140 |
loader.testNamePatterns = ['*1'] |
| 1141 |
self.assertEqual(loader.getTestCaseNames(MyTest), ['test_1']) |
| 1142 |
|
| 1143 |
loader.testNamePatterns = ['*1', '*2'] |
| 1144 |
self.assertEqual(loader.getTestCaseNames(MyTest), ['test_1', 'test_2']) |
| 1145 |
|
| 1146 |
loader.testNamePatterns = ['*My*'] |
| 1147 |
self.assertEqual(loader.getTestCaseNames(MyTest), ['test_1', 'test_2']) |
| 1148 |
|
| 1149 |
loader.testNamePatterns = ['*my*'] |
| 1150 |
self.assertEqual(loader.getTestCaseNames(MyTest), []) |
| 1151 |
|
| 1152 |
# "Return a sorted sequence of method names found within testCaseClass" |
| 1153 |
# |
| 1154 |
# If TestLoader.testNamePatterns is set, only tests that match one of these |
| 1155 |
# patterns should be included. |
| 1156 |
# |
| 1157 |
# For backwards compatibility reasons (see bpo-32071), the check may only |
| 1158 |
# touch a TestCase's attribute if it starts with the test method prefix. |
| 1159 |
def test_getTestCaseNames__testNamePatterns__attribute_access_regression(self): |
| 1160 |
class Trap: |
| 1161 |
def __get__(*ignored): |
| 1162 |
self.fail('Non-test attribute accessed') |
| 1163 |
|
| 1164 |
class MyTest(unittest.TestCase): |
| 1165 |
def test_1(self): pass |
| 1166 |
foobar = Trap() |
| 1167 |
|
| 1168 |
loader = unittest.TestLoader() |
| 1169 |
self.assertEqual(loader.getTestCaseNames(MyTest), ['test_1']) |
| 1170 |
|
| 1171 |
loader = unittest.TestLoader() |
| 1172 |
loader.testNamePatterns = [] |
| 1173 |
self.assertEqual(loader.getTestCaseNames(MyTest), []) |
| 1174 |
|
| 1175 |
################################################################ |
| 1176 |
### /Tests for TestLoader.getTestCaseNames() |
| 1177 |
|
| 1178 |
### Tests for TestLoader.testMethodPrefix |
| 1179 |
################################################################ |
| 1180 |
|
| 1181 |
# "String giving the prefix of method names which will be interpreted as |
| 1182 |
# test methods" |
| 1183 |
# |
| 1184 |
# Implicit in the documentation is that testMethodPrefix is respected by |
| 1185 |
# all loadTestsFrom* methods. |
| 1186 |
def test_testMethodPrefix__loadTestsFromTestCase(self): |
| 1187 |
class Foo(unittest.TestCase): |
| 1188 |
def test_1(self): pass |
| 1189 |
def test_2(self): pass |
| 1190 |
def foo_bar(self): pass |
| 1191 |
|
| 1192 |
tests_1 = unittest.TestSuite([Foo('foo_bar')]) |
| 1193 |
tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) |
| 1194 |
|
| 1195 |
loader = unittest.TestLoader() |
| 1196 |
loader.testMethodPrefix = 'foo' |
| 1197 |
self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1) |
| 1198 |
|
| 1199 |
loader.testMethodPrefix = 'test' |
| 1200 |
self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2) |
| 1201 |
|
| 1202 |
# "String giving the prefix of method names which will be interpreted as |
| 1203 |
# test methods" |
| 1204 |
# |
| 1205 |
# Implicit in the documentation is that testMethodPrefix is respected by |
| 1206 |
# all loadTestsFrom* methods. |
| 1207 |
def test_testMethodPrefix__loadTestsFromModule(self): |
| 1208 |
m = types.ModuleType('m') |
| 1209 |
class Foo(unittest.TestCase): |
| 1210 |
def test_1(self): pass |
| 1211 |
def test_2(self): pass |
| 1212 |
def foo_bar(self): pass |
| 1213 |
m.Foo = Foo |
| 1214 |
|
| 1215 |
tests_1 = [unittest.TestSuite([Foo('foo_bar')])] |
| 1216 |
tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])] |
| 1217 |
|
| 1218 |
loader = unittest.TestLoader() |
| 1219 |
loader.testMethodPrefix = 'foo' |
| 1220 |
self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1) |
| 1221 |
|
| 1222 |
loader.testMethodPrefix = 'test' |
| 1223 |
self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2) |
| 1224 |
|
| 1225 |
# "String giving the prefix of method names which will be interpreted as |
| 1226 |
# test methods" |
| 1227 |
# |
| 1228 |
# Implicit in the documentation is that testMethodPrefix is respected by |
| 1229 |
# all loadTestsFrom* methods. |
| 1230 |
def test_testMethodPrefix__loadTestsFromName(self): |
| 1231 |
m = types.ModuleType('m') |
| 1232 |
class Foo(unittest.TestCase): |
| 1233 |
def test_1(self): pass |
| 1234 |
def test_2(self): pass |
| 1235 |
def foo_bar(self): pass |
| 1236 |
m.Foo = Foo |
| 1237 |
|
| 1238 |
tests_1 = unittest.TestSuite([Foo('foo_bar')]) |
| 1239 |
tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) |
| 1240 |
|
| 1241 |
loader = unittest.TestLoader() |
| 1242 |
loader.testMethodPrefix = 'foo' |
| 1243 |
self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1) |
| 1244 |
|
| 1245 |
loader.testMethodPrefix = 'test' |
| 1246 |
self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2) |
| 1247 |
|
| 1248 |
# "String giving the prefix of method names which will be interpreted as |
| 1249 |
# test methods" |
| 1250 |
# |
| 1251 |
# Implicit in the documentation is that testMethodPrefix is respected by |
| 1252 |
# all loadTestsFrom* methods. |
| 1253 |
def test_testMethodPrefix__loadTestsFromNames(self): |
| 1254 |
m = types.ModuleType('m') |
| 1255 |
class Foo(unittest.TestCase): |
| 1256 |
def test_1(self): pass |
| 1257 |
def test_2(self): pass |
| 1258 |
def foo_bar(self): pass |
| 1259 |
m.Foo = Foo |
| 1260 |
|
| 1261 |
tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])]) |
| 1262 |
tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) |
| 1263 |
tests_2 = unittest.TestSuite([tests_2]) |
| 1264 |
|
| 1265 |
loader = unittest.TestLoader() |
| 1266 |
loader.testMethodPrefix = 'foo' |
| 1267 |
self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1) |
| 1268 |
|
| 1269 |
loader.testMethodPrefix = 'test' |
| 1270 |
self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2) |
| 1271 |
|
| 1272 |
# "The default value is 'test'" |
| 1273 |
def test_testMethodPrefix__default_value(self): |
| 1274 |
loader = unittest.TestLoader() |
| 1275 |
self.assertEqual(loader.testMethodPrefix, 'test') |
| 1276 |
|
| 1277 |
################################################################ |
| 1278 |
### /Tests for TestLoader.testMethodPrefix |
| 1279 |
|
| 1280 |
### Tests for TestLoader.sortTestMethodsUsing |
| 1281 |
################################################################ |
| 1282 |
|
| 1283 |
# "Function to be used to compare method names when sorting them in |
| 1284 |
# getTestCaseNames() and all the loadTestsFromX() methods" |
| 1285 |
def test_sortTestMethodsUsing__loadTestsFromTestCase(self): |
| 1286 |
def reversed_cmp(x, y): |
| 1287 |
return -((x > y) - (x < y)) |
| 1288 |
|
| 1289 |
class Foo(unittest.TestCase): |
| 1290 |
def test_1(self): pass |
| 1291 |
def test_2(self): pass |
| 1292 |
|
| 1293 |
loader = unittest.TestLoader() |
| 1294 |
loader.sortTestMethodsUsing = reversed_cmp |
| 1295 |
|
| 1296 |
tests = loader.suiteClass([Foo('test_2'), Foo('test_1')]) |
| 1297 |
self.assertEqual(loader.loadTestsFromTestCase(Foo), tests) |
| 1298 |
|
| 1299 |
# "Function to be used to compare method names when sorting them in |
| 1300 |
# getTestCaseNames() and all the loadTestsFromX() methods" |
| 1301 |
def test_sortTestMethodsUsing__loadTestsFromModule(self): |
| 1302 |
def reversed_cmp(x, y): |
| 1303 |
return -((x > y) - (x < y)) |
| 1304 |
|
| 1305 |
m = types.ModuleType('m') |
| 1306 |
class Foo(unittest.TestCase): |
| 1307 |
def test_1(self): pass |
| 1308 |
def test_2(self): pass |
| 1309 |
m.Foo = Foo |
| 1310 |
|
| 1311 |
loader = unittest.TestLoader() |
| 1312 |
loader.sortTestMethodsUsing = reversed_cmp |
| 1313 |
|
| 1314 |
tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])] |
| 1315 |
self.assertEqual(list(loader.loadTestsFromModule(m)), tests) |
| 1316 |
|
| 1317 |
# "Function to be used to compare method names when sorting them in |
| 1318 |
# getTestCaseNames() and all the loadTestsFromX() methods" |
| 1319 |
def test_sortTestMethodsUsing__loadTestsFromName(self): |
| 1320 |
def reversed_cmp(x, y): |
| 1321 |
return -((x > y) - (x < y)) |
| 1322 |
|
| 1323 |
m = types.ModuleType('m') |
| 1324 |
class Foo(unittest.TestCase): |
| 1325 |
def test_1(self): pass |
| 1326 |
def test_2(self): pass |
| 1327 |
m.Foo = Foo |
| 1328 |
|
| 1329 |
loader = unittest.TestLoader() |
| 1330 |
loader.sortTestMethodsUsing = reversed_cmp |
| 1331 |
|
| 1332 |
tests = loader.suiteClass([Foo('test_2'), Foo('test_1')]) |
| 1333 |
self.assertEqual(loader.loadTestsFromName('Foo', m), tests) |
| 1334 |
|
| 1335 |
# "Function to be used to compare method names when sorting them in |
| 1336 |
# getTestCaseNames() and all the loadTestsFromX() methods" |
| 1337 |
def test_sortTestMethodsUsing__loadTestsFromNames(self): |
| 1338 |
def reversed_cmp(x, y): |
| 1339 |
return -((x > y) - (x < y)) |
| 1340 |
|
| 1341 |
m = types.ModuleType('m') |
| 1342 |
class Foo(unittest.TestCase): |
| 1343 |
def test_1(self): pass |
| 1344 |
def test_2(self): pass |
| 1345 |
m.Foo = Foo |
| 1346 |
|
| 1347 |
loader = unittest.TestLoader() |
| 1348 |
loader.sortTestMethodsUsing = reversed_cmp |
| 1349 |
|
| 1350 |
tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])] |
| 1351 |
self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests) |
| 1352 |
|
| 1353 |
# "Function to be used to compare method names when sorting them in |
| 1354 |
# getTestCaseNames()" |
| 1355 |
# |
| 1356 |
# Does it actually affect getTestCaseNames()? |
| 1357 |
def test_sortTestMethodsUsing__getTestCaseNames(self): |
| 1358 |
def reversed_cmp(x, y): |
| 1359 |
return -((x > y) - (x < y)) |
| 1360 |
|
| 1361 |
class Foo(unittest.TestCase): |
| 1362 |
def test_1(self): pass |
| 1363 |
def test_2(self): pass |
| 1364 |
|
| 1365 |
loader = unittest.TestLoader() |
| 1366 |
loader.sortTestMethodsUsing = reversed_cmp |
| 1367 |
|
| 1368 |
test_names = ['test_2', 'test_1'] |
| 1369 |
self.assertEqual(loader.getTestCaseNames(Foo), test_names) |
| 1370 |
|
| 1371 |
# "The default value is the built-in cmp() function" |
| 1372 |
# Since cmp is now defunct, we simply verify that the results |
| 1373 |
# occur in the same order as they would with the default sort. |
| 1374 |
def test_sortTestMethodsUsing__default_value(self): |
| 1375 |
loader = unittest.TestLoader() |
| 1376 |
|
| 1377 |
class Foo(unittest.TestCase): |
| 1378 |
def test_2(self): pass |
| 1379 |
def test_3(self): pass |
| 1380 |
def test_1(self): pass |
| 1381 |
|
| 1382 |
test_names = ['test_2', 'test_3', 'test_1'] |
| 1383 |
self.assertEqual(loader.getTestCaseNames(Foo), sorted(test_names)) |
| 1384 |
|
| 1385 |
|
| 1386 |
# "it can be set to None to disable the sort." |
| 1387 |
# |
| 1388 |
# XXX How is this different from reassigning cmp? Are the tests returned |
| 1389 |
# in a random order or something? This behaviour should die |
| 1390 |
def test_sortTestMethodsUsing__None(self): |
| 1391 |
class Foo(unittest.TestCase): |
| 1392 |
def test_1(self): pass |
| 1393 |
def test_2(self): pass |
| 1394 |
|
| 1395 |
loader = unittest.TestLoader() |
| 1396 |
loader.sortTestMethodsUsing = None |
| 1397 |
|
| 1398 |
test_names = ['test_2', 'test_1'] |
| 1399 |
self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names)) |
| 1400 |
|
| 1401 |
################################################################ |
| 1402 |
### /Tests for TestLoader.sortTestMethodsUsing |
| 1403 |
|
| 1404 |
### Tests for TestLoader.suiteClass |
| 1405 |
################################################################ |
| 1406 |
|
| 1407 |
# "Callable object that constructs a test suite from a list of tests." |
| 1408 |
def test_suiteClass__loadTestsFromTestCase(self): |
| 1409 |
class Foo(unittest.TestCase): |
| 1410 |
def test_1(self): pass |
| 1411 |
def test_2(self): pass |
| 1412 |
def foo_bar(self): pass |
| 1413 |
|
| 1414 |
tests = [Foo('test_1'), Foo('test_2')] |
| 1415 |
|
| 1416 |
loader = unittest.TestLoader() |
| 1417 |
loader.suiteClass = list |
| 1418 |
self.assertEqual(loader.loadTestsFromTestCase(Foo), tests) |
| 1419 |
|
| 1420 |
# It is implicit in the documentation for TestLoader.suiteClass that |
| 1421 |
# all TestLoader.loadTestsFrom* methods respect it. Let's make sure |
| 1422 |
def test_suiteClass__loadTestsFromModule(self): |
| 1423 |
m = types.ModuleType('m') |
| 1424 |
class Foo(unittest.TestCase): |
| 1425 |
def test_1(self): pass |
| 1426 |
def test_2(self): pass |
| 1427 |
def foo_bar(self): pass |
| 1428 |
m.Foo = Foo |
| 1429 |
|
| 1430 |
tests = [[Foo('test_1'), Foo('test_2')]] |
| 1431 |
|
| 1432 |
loader = unittest.TestLoader() |
| 1433 |
loader.suiteClass = list |
| 1434 |
self.assertEqual(loader.loadTestsFromModule(m), tests) |
| 1435 |
|
| 1436 |
# It is implicit in the documentation for TestLoader.suiteClass that |
| 1437 |
# all TestLoader.loadTestsFrom* methods respect it. Let's make sure |
| 1438 |
def test_suiteClass__loadTestsFromName(self): |
| 1439 |
m = types.ModuleType('m') |
| 1440 |
class Foo(unittest.TestCase): |
| 1441 |
def test_1(self): pass |
| 1442 |
def test_2(self): pass |
| 1443 |
def foo_bar(self): pass |
| 1444 |
m.Foo = Foo |
| 1445 |
|
| 1446 |
tests = [Foo('test_1'), Foo('test_2')] |
| 1447 |
|
| 1448 |
loader = unittest.TestLoader() |
| 1449 |
loader.suiteClass = list |
| 1450 |
self.assertEqual(loader.loadTestsFromName('Foo', m), tests) |
| 1451 |
|
| 1452 |
# It is implicit in the documentation for TestLoader.suiteClass that |
| 1453 |
# all TestLoader.loadTestsFrom* methods respect it. Let's make sure |
| 1454 |
def test_suiteClass__loadTestsFromNames(self): |
| 1455 |
m = types.ModuleType('m') |
| 1456 |
class Foo(unittest.TestCase): |
| 1457 |
def test_1(self): pass |
| 1458 |
def test_2(self): pass |
| 1459 |
def foo_bar(self): pass |
| 1460 |
m.Foo = Foo |
| 1461 |
|
| 1462 |
tests = [[Foo('test_1'), Foo('test_2')]] |
| 1463 |
|
| 1464 |
loader = unittest.TestLoader() |
| 1465 |
loader.suiteClass = list |
| 1466 |
self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests) |
| 1467 |
|
| 1468 |
# "The default value is the TestSuite class" |
| 1469 |
def test_suiteClass__default_value(self): |
| 1470 |
loader = unittest.TestLoader() |
| 1471 |
self.assertIs(loader.suiteClass, unittest.TestSuite) |
| 1472 |
|
| 1473 |
|
| 1474 |
def test_partial_functions(self): |
| 1475 |
def noop(arg): |
| 1476 |
pass |
| 1477 |
|
| 1478 |
class Foo(unittest.TestCase): |
| 1479 |
pass |
| 1480 |
|
| 1481 |
setattr(Foo, 'test_partial', functools.partial(noop, None)) |
| 1482 |
|
| 1483 |
loader = unittest.TestLoader() |
| 1484 |
|
| 1485 |
test_names = ['test_partial'] |
| 1486 |
self.assertEqual(loader.getTestCaseNames(Foo), test_names) |
| 1487 |
|
| 1488 |
|
| 1489 |
class TestObsoleteFunctions(unittest.TestCase): |
| 1490 |
class MyTestSuite(unittest.TestSuite): |
| 1491 |
pass |
| 1492 |
|
| 1493 |
class MyTestCase(unittest.TestCase): |
| 1494 |
def check_1(self): pass |
| 1495 |
def check_2(self): pass |
| 1496 |
def test(self): pass |
| 1497 |
|
| 1498 |
@staticmethod |
| 1499 |
def reverse_three_way_cmp(a, b): |
| 1500 |
return unittest.util.three_way_cmp(b, a) |
| 1501 |
|
| 1502 |
|
| 1503 |
if __name__ == "__main__": |
| 1504 |
unittest.main() |