test_loader.py 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. import sys
  2. import types
  3. import unittest
  4. class Test_TestLoader(unittest.TestCase):
  5. ### Tests for TestLoader.loadTestsFromTestCase
  6. ################################################################
  7. # "Return a suite of all tests cases contained in the TestCase-derived
  8. # class testCaseClass"
  9. def test_loadTestsFromTestCase(self):
  10. class Foo(unittest.TestCase):
  11. def test_1(self): pass
  12. def test_2(self): pass
  13. def foo_bar(self): pass
  14. tests = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
  15. loader = unittest.TestLoader()
  16. self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
  17. # "Return a suite of all tests cases contained in the TestCase-derived
  18. # class testCaseClass"
  19. #
  20. # Make sure it does the right thing even if no tests were found
  21. def test_loadTestsFromTestCase__no_matches(self):
  22. class Foo(unittest.TestCase):
  23. def foo_bar(self): pass
  24. empty_suite = unittest.TestSuite()
  25. loader = unittest.TestLoader()
  26. self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite)
  27. # "Return a suite of all tests cases contained in the TestCase-derived
  28. # class testCaseClass"
  29. #
  30. # What happens if loadTestsFromTestCase() is given an object
  31. # that isn't a subclass of TestCase? Specifically, what happens
  32. # if testCaseClass is a subclass of TestSuite?
  33. #
  34. # This is checked for specifically in the code, so we better add a
  35. # test for it.
  36. def test_loadTestsFromTestCase__TestSuite_subclass(self):
  37. class NotATestCase(unittest.TestSuite):
  38. pass
  39. loader = unittest.TestLoader()
  40. try:
  41. loader.loadTestsFromTestCase(NotATestCase)
  42. except TypeError:
  43. pass
  44. else:
  45. self.fail('Should raise TypeError')
  46. # "Return a suite of all tests cases contained in the TestCase-derived
  47. # class testCaseClass"
  48. #
  49. # Make sure loadTestsFromTestCase() picks up the default test method
  50. # name (as specified by TestCase), even though the method name does
  51. # not match the default TestLoader.testMethodPrefix string
  52. def test_loadTestsFromTestCase__default_method_name(self):
  53. class Foo(unittest.TestCase):
  54. def runTest(self):
  55. pass
  56. loader = unittest.TestLoader()
  57. # This has to be false for the test to succeed
  58. self.assertFalse('runTest'.startswith(loader.testMethodPrefix))
  59. suite = loader.loadTestsFromTestCase(Foo)
  60. self.assertIsInstance(suite, loader.suiteClass)
  61. self.assertEqual(list(suite), [Foo('runTest')])
  62. ################################################################
  63. ### /Tests for TestLoader.loadTestsFromTestCase
  64. ### Tests for TestLoader.loadTestsFromModule
  65. ################################################################
  66. # "This method searches `module` for classes derived from TestCase"
  67. def test_loadTestsFromModule__TestCase_subclass(self):
  68. m = types.ModuleType('m')
  69. class MyTestCase(unittest.TestCase):
  70. def test(self):
  71. pass
  72. m.testcase_1 = MyTestCase
  73. loader = unittest.TestLoader()
  74. suite = loader.loadTestsFromModule(m)
  75. self.assertIsInstance(suite, loader.suiteClass)
  76. expected = [loader.suiteClass([MyTestCase('test')])]
  77. self.assertEqual(list(suite), expected)
  78. # "This method searches `module` for classes derived from TestCase"
  79. #
  80. # What happens if no tests are found (no TestCase instances)?
  81. def test_loadTestsFromModule__no_TestCase_instances(self):
  82. m = types.ModuleType('m')
  83. loader = unittest.TestLoader()
  84. suite = loader.loadTestsFromModule(m)
  85. self.assertIsInstance(suite, loader.suiteClass)
  86. self.assertEqual(list(suite), [])
  87. # "This method searches `module` for classes derived from TestCase"
  88. #
  89. # What happens if no tests are found (TestCases instances, but no tests)?
  90. def test_loadTestsFromModule__no_TestCase_tests(self):
  91. m = types.ModuleType('m')
  92. class MyTestCase(unittest.TestCase):
  93. pass
  94. m.testcase_1 = MyTestCase
  95. loader = unittest.TestLoader()
  96. suite = loader.loadTestsFromModule(m)
  97. self.assertIsInstance(suite, loader.suiteClass)
  98. self.assertEqual(list(suite), [loader.suiteClass()])
  99. # "This method searches `module` for classes derived from TestCase"s
  100. #
  101. # What happens if loadTestsFromModule() is given something other
  102. # than a module?
  103. #
  104. # XXX Currently, it succeeds anyway. This flexibility
  105. # should either be documented or loadTestsFromModule() should
  106. # raise a TypeError
  107. #
  108. # XXX Certain people are using this behaviour. We'll add a test for it
  109. def test_loadTestsFromModule__not_a_module(self):
  110. class MyTestCase(unittest.TestCase):
  111. def test(self):
  112. pass
  113. class NotAModule(object):
  114. test_2 = MyTestCase
  115. loader = unittest.TestLoader()
  116. suite = loader.loadTestsFromModule(NotAModule)
  117. reference = [unittest.TestSuite([MyTestCase('test')])]
  118. self.assertEqual(list(suite), reference)
  119. # Check that loadTestsFromModule honors (or not) a module
  120. # with a load_tests function.
  121. def test_loadTestsFromModule__load_tests(self):
  122. m = types.ModuleType('m')
  123. class MyTestCase(unittest.TestCase):
  124. def test(self):
  125. pass
  126. m.testcase_1 = MyTestCase
  127. load_tests_args = []
  128. def load_tests(loader, tests, pattern):
  129. self.assertIsInstance(tests, unittest.TestSuite)
  130. load_tests_args.extend((loader, tests, pattern))
  131. return tests
  132. m.load_tests = load_tests
  133. loader = unittest.TestLoader()
  134. suite = loader.loadTestsFromModule(m)
  135. self.assertIsInstance(suite, unittest.TestSuite)
  136. self.assertEqual(load_tests_args, [loader, suite, None])
  137. load_tests_args = []
  138. suite = loader.loadTestsFromModule(m, use_load_tests=False)
  139. self.assertEqual(load_tests_args, [])
  140. def test_loadTestsFromModule__faulty_load_tests(self):
  141. m = types.ModuleType('m')
  142. def load_tests(loader, tests, pattern):
  143. raise TypeError('some failure')
  144. m.load_tests = load_tests
  145. loader = unittest.TestLoader()
  146. suite = loader.loadTestsFromModule(m)
  147. self.assertIsInstance(suite, unittest.TestSuite)
  148. self.assertEqual(suite.countTestCases(), 1)
  149. test = list(suite)[0]
  150. self.assertRaisesRegexp(TypeError, "some failure", test.m)
  151. ################################################################
  152. ### /Tests for TestLoader.loadTestsFromModule()
  153. ### Tests for TestLoader.loadTestsFromName()
  154. ################################################################
  155. # "The specifier name is a ``dotted name'' that may resolve either to
  156. # a module, a test case class, a TestSuite instance, a test method
  157. # within a test case class, or a callable object which returns a
  158. # TestCase or TestSuite instance."
  159. #
  160. # Is ValueError raised in response to an empty name?
  161. def test_loadTestsFromName__empty_name(self):
  162. loader = unittest.TestLoader()
  163. try:
  164. loader.loadTestsFromName('')
  165. except ValueError, e:
  166. self.assertEqual(str(e), "Empty module name")
  167. else:
  168. self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
  169. # "The specifier name is a ``dotted name'' that may resolve either to
  170. # a module, a test case class, a TestSuite instance, a test method
  171. # within a test case class, or a callable object which returns a
  172. # TestCase or TestSuite instance."
  173. #
  174. # What happens when the name contains invalid characters?
  175. def test_loadTestsFromName__malformed_name(self):
  176. loader = unittest.TestLoader()
  177. # XXX Should this raise ValueError or ImportError?
  178. try:
  179. loader.loadTestsFromName('abc () //')
  180. except ValueError:
  181. pass
  182. except ImportError:
  183. pass
  184. else:
  185. self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
  186. # "The specifier name is a ``dotted name'' that may resolve ... to a
  187. # module"
  188. #
  189. # What happens when a module by that name can't be found?
  190. def test_loadTestsFromName__unknown_module_name(self):
  191. loader = unittest.TestLoader()
  192. try:
  193. loader.loadTestsFromName('sdasfasfasdf')
  194. except ImportError, e:
  195. self.assertEqual(str(e), "No module named sdasfasfasdf")
  196. else:
  197. self.fail("TestLoader.loadTestsFromName failed to raise ImportError")
  198. # "The specifier name is a ``dotted name'' that may resolve either to
  199. # a module, a test case class, a TestSuite instance, a test method
  200. # within a test case class, or a callable object which returns a
  201. # TestCase or TestSuite instance."
  202. #
  203. # What happens when the module is found, but the attribute can't?
  204. def test_loadTestsFromName__unknown_attr_name(self):
  205. loader = unittest.TestLoader()
  206. try:
  207. loader.loadTestsFromName('unittest.sdasfasfasdf')
  208. except AttributeError, e:
  209. self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
  210. else:
  211. self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
  212. # "The specifier name is a ``dotted name'' that may resolve either to
  213. # a module, a test case class, a TestSuite instance, a test method
  214. # within a test case class, or a callable object which returns a
  215. # TestCase or TestSuite instance."
  216. #
  217. # What happens when we provide the module, but the attribute can't be
  218. # found?
  219. def test_loadTestsFromName__relative_unknown_name(self):
  220. loader = unittest.TestLoader()
  221. try:
  222. loader.loadTestsFromName('sdasfasfasdf', unittest)
  223. except AttributeError, e:
  224. self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
  225. else:
  226. self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
  227. # "The specifier name is a ``dotted name'' that may resolve either to
  228. # a module, a test case class, a TestSuite instance, a test method
  229. # within a test case class, or a callable object which returns a
  230. # TestCase or TestSuite instance."
  231. # ...
  232. # "The method optionally resolves name relative to the given module"
  233. #
  234. # Does loadTestsFromName raise ValueError when passed an empty
  235. # name relative to a provided module?
  236. #
  237. # XXX Should probably raise a ValueError instead of an AttributeError
  238. def test_loadTestsFromName__relative_empty_name(self):
  239. loader = unittest.TestLoader()
  240. try:
  241. loader.loadTestsFromName('', unittest)
  242. except AttributeError:
  243. pass
  244. else:
  245. self.fail("Failed to raise AttributeError")
  246. # "The specifier name is a ``dotted name'' that may resolve either to
  247. # a module, a test case class, a TestSuite instance, a test method
  248. # within a test case class, or a callable object which returns a
  249. # TestCase or TestSuite instance."
  250. # ...
  251. # "The method optionally resolves name relative to the given module"
  252. #
  253. # What happens when an impossible name is given, relative to the provided
  254. # `module`?
  255. def test_loadTestsFromName__relative_malformed_name(self):
  256. loader = unittest.TestLoader()
  257. # XXX Should this raise AttributeError or ValueError?
  258. try:
  259. loader.loadTestsFromName('abc () //', unittest)
  260. except ValueError:
  261. pass
  262. except AttributeError:
  263. pass
  264. else:
  265. self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
  266. # "The method optionally resolves name relative to the given module"
  267. #
  268. # Does loadTestsFromName raise TypeError when the `module` argument
  269. # isn't a module object?
  270. #
  271. # XXX Accepts the not-a-module object, ignoring the object's type
  272. # This should raise an exception or the method name should be changed
  273. #
  274. # XXX Some people are relying on this, so keep it for now
  275. def test_loadTestsFromName__relative_not_a_module(self):
  276. class MyTestCase(unittest.TestCase):
  277. def test(self):
  278. pass
  279. class NotAModule(object):
  280. test_2 = MyTestCase
  281. loader = unittest.TestLoader()
  282. suite = loader.loadTestsFromName('test_2', NotAModule)
  283. reference = [MyTestCase('test')]
  284. self.assertEqual(list(suite), reference)
  285. # "The specifier name is a ``dotted name'' that may resolve either to
  286. # a module, a test case class, a TestSuite instance, a test method
  287. # within a test case class, or a callable object which returns a
  288. # TestCase or TestSuite instance."
  289. #
  290. # Does it raise an exception if the name resolves to an invalid
  291. # object?
  292. def test_loadTestsFromName__relative_bad_object(self):
  293. m = types.ModuleType('m')
  294. m.testcase_1 = object()
  295. loader = unittest.TestLoader()
  296. try:
  297. loader.loadTestsFromName('testcase_1', m)
  298. except TypeError:
  299. pass
  300. else:
  301. self.fail("Should have raised TypeError")
  302. # "The specifier name is a ``dotted name'' that may
  303. # resolve either to ... a test case class"
  304. def test_loadTestsFromName__relative_TestCase_subclass(self):
  305. m = types.ModuleType('m')
  306. class MyTestCase(unittest.TestCase):
  307. def test(self):
  308. pass
  309. m.testcase_1 = MyTestCase
  310. loader = unittest.TestLoader()
  311. suite = loader.loadTestsFromName('testcase_1', m)
  312. self.assertIsInstance(suite, loader.suiteClass)
  313. self.assertEqual(list(suite), [MyTestCase('test')])
  314. # "The specifier name is a ``dotted name'' that may resolve either to
  315. # a module, a test case class, a TestSuite instance, a test method
  316. # within a test case class, or a callable object which returns a
  317. # TestCase or TestSuite instance."
  318. def test_loadTestsFromName__relative_TestSuite(self):
  319. m = types.ModuleType('m')
  320. class MyTestCase(unittest.TestCase):
  321. def test(self):
  322. pass
  323. m.testsuite = unittest.TestSuite([MyTestCase('test')])
  324. loader = unittest.TestLoader()
  325. suite = loader.loadTestsFromName('testsuite', m)
  326. self.assertIsInstance(suite, loader.suiteClass)
  327. self.assertEqual(list(suite), [MyTestCase('test')])
  328. # "The specifier name is a ``dotted name'' that may resolve ... to
  329. # ... a test method within a test case class"
  330. def test_loadTestsFromName__relative_testmethod(self):
  331. m = types.ModuleType('m')
  332. class MyTestCase(unittest.TestCase):
  333. def test(self):
  334. pass
  335. m.testcase_1 = MyTestCase
  336. loader = unittest.TestLoader()
  337. suite = loader.loadTestsFromName('testcase_1.test', m)
  338. self.assertIsInstance(suite, loader.suiteClass)
  339. self.assertEqual(list(suite), [MyTestCase('test')])
  340. # "The specifier name is a ``dotted name'' that may resolve either to
  341. # a module, a test case class, a TestSuite instance, a test method
  342. # within a test case class, or a callable object which returns a
  343. # TestCase or TestSuite instance."
  344. #
  345. # Does loadTestsFromName() raise the proper exception when trying to
  346. # resolve "a test method within a test case class" that doesn't exist
  347. # for the given name (relative to a provided module)?
  348. def test_loadTestsFromName__relative_invalid_testmethod(self):
  349. m = types.ModuleType('m')
  350. class MyTestCase(unittest.TestCase):
  351. def test(self):
  352. pass
  353. m.testcase_1 = MyTestCase
  354. loader = unittest.TestLoader()
  355. try:
  356. loader.loadTestsFromName('testcase_1.testfoo', m)
  357. except AttributeError, e:
  358. self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
  359. else:
  360. self.fail("Failed to raise AttributeError")
  361. # "The specifier name is a ``dotted name'' that may resolve ... to
  362. # ... a callable object which returns a ... TestSuite instance"
  363. def test_loadTestsFromName__callable__TestSuite(self):
  364. m = types.ModuleType('m')
  365. testcase_1 = unittest.FunctionTestCase(lambda: None)
  366. testcase_2 = unittest.FunctionTestCase(lambda: None)
  367. def return_TestSuite():
  368. return unittest.TestSuite([testcase_1, testcase_2])
  369. m.return_TestSuite = return_TestSuite
  370. loader = unittest.TestLoader()
  371. suite = loader.loadTestsFromName('return_TestSuite', m)
  372. self.assertIsInstance(suite, loader.suiteClass)
  373. self.assertEqual(list(suite), [testcase_1, testcase_2])
  374. # "The specifier name is a ``dotted name'' that may resolve ... to
  375. # ... a callable object which returns a TestCase ... instance"
  376. def test_loadTestsFromName__callable__TestCase_instance(self):
  377. m = types.ModuleType('m')
  378. testcase_1 = unittest.FunctionTestCase(lambda: None)
  379. def return_TestCase():
  380. return testcase_1
  381. m.return_TestCase = return_TestCase
  382. loader = unittest.TestLoader()
  383. suite = loader.loadTestsFromName('return_TestCase', m)
  384. self.assertIsInstance(suite, loader.suiteClass)
  385. self.assertEqual(list(suite), [testcase_1])
  386. # "The specifier name is a ``dotted name'' that may resolve ... to
  387. # ... a callable object which returns a TestCase ... instance"
  388. #*****************************************************************
  389. #Override the suiteClass attribute to ensure that the suiteClass
  390. #attribute is used
  391. def test_loadTestsFromName__callable__TestCase_instance_ProperSuiteClass(self):
  392. class SubTestSuite(unittest.TestSuite):
  393. pass
  394. m = types.ModuleType('m')
  395. testcase_1 = unittest.FunctionTestCase(lambda: None)
  396. def return_TestCase():
  397. return testcase_1
  398. m.return_TestCase = return_TestCase
  399. loader = unittest.TestLoader()
  400. loader.suiteClass = SubTestSuite
  401. suite = loader.loadTestsFromName('return_TestCase', m)
  402. self.assertIsInstance(suite, loader.suiteClass)
  403. self.assertEqual(list(suite), [testcase_1])
  404. # "The specifier name is a ``dotted name'' that may resolve ... to
  405. # ... a test method within a test case class"
  406. #*****************************************************************
  407. #Override the suiteClass attribute to ensure that the suiteClass
  408. #attribute is used
  409. def test_loadTestsFromName__relative_testmethod_ProperSuiteClass(self):
  410. class SubTestSuite(unittest.TestSuite):
  411. pass
  412. m = types.ModuleType('m')
  413. class MyTestCase(unittest.TestCase):
  414. def test(self):
  415. pass
  416. m.testcase_1 = MyTestCase
  417. loader = unittest.TestLoader()
  418. loader.suiteClass=SubTestSuite
  419. suite = loader.loadTestsFromName('testcase_1.test', m)
  420. self.assertIsInstance(suite, loader.suiteClass)
  421. self.assertEqual(list(suite), [MyTestCase('test')])
  422. # "The specifier name is a ``dotted name'' that may resolve ... to
  423. # ... a callable object which returns a TestCase or TestSuite instance"
  424. #
  425. # What happens if the callable returns something else?
  426. def test_loadTestsFromName__callable__wrong_type(self):
  427. m = types.ModuleType('m')
  428. def return_wrong():
  429. return 6
  430. m.return_wrong = return_wrong
  431. loader = unittest.TestLoader()
  432. try:
  433. loader.loadTestsFromName('return_wrong', m)
  434. except TypeError:
  435. pass
  436. else:
  437. self.fail("TestLoader.loadTestsFromName failed to raise TypeError")
  438. # "The specifier can refer to modules and packages which have not been
  439. # imported; they will be imported as a side-effect"
  440. def test_loadTestsFromName__module_not_loaded(self):
  441. # We're going to try to load this module as a side-effect, so it
  442. # better not be loaded before we try.
  443. #
  444. module_name = 'unittest.test.dummy'
  445. sys.modules.pop(module_name, None)
  446. loader = unittest.TestLoader()
  447. try:
  448. suite = loader.loadTestsFromName(module_name)
  449. self.assertIsInstance(suite, loader.suiteClass)
  450. self.assertEqual(list(suite), [])
  451. # module should now be loaded, thanks to loadTestsFromName()
  452. self.assertIn(module_name, sys.modules)
  453. finally:
  454. if module_name in sys.modules:
  455. del sys.modules[module_name]
  456. ################################################################
  457. ### Tests for TestLoader.loadTestsFromName()
  458. ### Tests for TestLoader.loadTestsFromNames()
  459. ################################################################
  460. # "Similar to loadTestsFromName(), but takes a sequence of names rather
  461. # than a single name."
  462. #
  463. # What happens if that sequence of names is empty?
  464. def test_loadTestsFromNames__empty_name_list(self):
  465. loader = unittest.TestLoader()
  466. suite = loader.loadTestsFromNames([])
  467. self.assertIsInstance(suite, loader.suiteClass)
  468. self.assertEqual(list(suite), [])
  469. # "Similar to loadTestsFromName(), but takes a sequence of names rather
  470. # than a single name."
  471. # ...
  472. # "The method optionally resolves name relative to the given module"
  473. #
  474. # What happens if that sequence of names is empty?
  475. #
  476. # XXX Should this raise a ValueError or just return an empty TestSuite?
  477. def test_loadTestsFromNames__relative_empty_name_list(self):
  478. loader = unittest.TestLoader()
  479. suite = loader.loadTestsFromNames([], unittest)
  480. self.assertIsInstance(suite, loader.suiteClass)
  481. self.assertEqual(list(suite), [])
  482. # "The specifier name is a ``dotted name'' that may resolve either to
  483. # a module, a test case class, a TestSuite instance, a test method
  484. # within a test case class, or a callable object which returns a
  485. # TestCase or TestSuite instance."
  486. #
  487. # Is ValueError raised in response to an empty name?
  488. def test_loadTestsFromNames__empty_name(self):
  489. loader = unittest.TestLoader()
  490. try:
  491. loader.loadTestsFromNames([''])
  492. except ValueError, e:
  493. self.assertEqual(str(e), "Empty module name")
  494. else:
  495. self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
  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. # What happens when presented with an impossible module name?
  502. def test_loadTestsFromNames__malformed_name(self):
  503. loader = unittest.TestLoader()
  504. # XXX Should this raise ValueError or ImportError?
  505. try:
  506. loader.loadTestsFromNames(['abc () //'])
  507. except ValueError:
  508. pass
  509. except ImportError:
  510. pass
  511. else:
  512. self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
  513. # "The specifier name is a ``dotted name'' that may resolve either to
  514. # a module, a test case class, a TestSuite instance, a test method
  515. # within a test case class, or a callable object which returns a
  516. # TestCase or TestSuite instance."
  517. #
  518. # What happens when no module can be found for the given name?
  519. def test_loadTestsFromNames__unknown_module_name(self):
  520. loader = unittest.TestLoader()
  521. try:
  522. loader.loadTestsFromNames(['sdasfasfasdf'])
  523. except ImportError, e:
  524. self.assertEqual(str(e), "No module named sdasfasfasdf")
  525. else:
  526. self.fail("TestLoader.loadTestsFromNames failed to raise ImportError")
  527. # "The specifier name is a ``dotted name'' that may resolve either to
  528. # a module, a test case class, a TestSuite instance, a test method
  529. # within a test case class, or a callable object which returns a
  530. # TestCase or TestSuite instance."
  531. #
  532. # What happens when the module can be found, but not the attribute?
  533. def test_loadTestsFromNames__unknown_attr_name(self):
  534. loader = unittest.TestLoader()
  535. try:
  536. loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest'])
  537. except AttributeError, e:
  538. self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
  539. else:
  540. self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError")
  541. # "The specifier name is a ``dotted name'' that may resolve either to
  542. # a module, a test case class, a TestSuite instance, a test method
  543. # within a test case class, or a callable object which returns a
  544. # TestCase or TestSuite instance."
  545. # ...
  546. # "The method optionally resolves name relative to the given module"
  547. #
  548. # What happens when given an unknown attribute on a specified `module`
  549. # argument?
  550. def test_loadTestsFromNames__unknown_name_relative_1(self):
  551. loader = unittest.TestLoader()
  552. try:
  553. loader.loadTestsFromNames(['sdasfasfasdf'], unittest)
  554. except AttributeError, e:
  555. self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
  556. else:
  557. self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
  558. # "The specifier name is a ``dotted name'' that may resolve either to
  559. # a module, a test case class, a TestSuite instance, a test method
  560. # within a test case class, or a callable object which returns a
  561. # TestCase or TestSuite instance."
  562. # ...
  563. # "The method optionally resolves name relative to the given module"
  564. #
  565. # Do unknown attributes (relative to a provided module) still raise an
  566. # exception even in the presence of valid attribute names?
  567. def test_loadTestsFromNames__unknown_name_relative_2(self):
  568. loader = unittest.TestLoader()
  569. try:
  570. loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)
  571. except AttributeError, e:
  572. self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
  573. else:
  574. self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
  575. # "The specifier name is a ``dotted name'' that may resolve either to
  576. # a module, a test case class, a TestSuite instance, a test method
  577. # within a test case class, or a callable object which returns a
  578. # TestCase or TestSuite instance."
  579. # ...
  580. # "The method optionally resolves name relative to the given module"
  581. #
  582. # What happens when faced with the empty string?
  583. #
  584. # XXX This currently raises AttributeError, though ValueError is probably
  585. # more appropriate
  586. def test_loadTestsFromNames__relative_empty_name(self):
  587. loader = unittest.TestLoader()
  588. try:
  589. loader.loadTestsFromNames([''], unittest)
  590. except AttributeError:
  591. pass
  592. else:
  593. self.fail("Failed to raise ValueError")
  594. # "The specifier name is a ``dotted name'' that may resolve either to
  595. # a module, a test case class, a TestSuite instance, a test method
  596. # within a test case class, or a callable object which returns a
  597. # TestCase or TestSuite instance."
  598. # ...
  599. # "The method optionally resolves name relative to the given module"
  600. #
  601. # What happens when presented with an impossible attribute name?
  602. def test_loadTestsFromNames__relative_malformed_name(self):
  603. loader = unittest.TestLoader()
  604. # XXX Should this raise AttributeError or ValueError?
  605. try:
  606. loader.loadTestsFromNames(['abc () //'], unittest)
  607. except AttributeError:
  608. pass
  609. except ValueError:
  610. pass
  611. else:
  612. self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
  613. # "The method optionally resolves name relative to the given module"
  614. #
  615. # Does loadTestsFromNames() make sure the provided `module` is in fact
  616. # a module?
  617. #
  618. # XXX This validation is currently not done. This flexibility should
  619. # either be documented or a TypeError should be raised.
  620. def test_loadTestsFromNames__relative_not_a_module(self):
  621. class MyTestCase(unittest.TestCase):
  622. def test(self):
  623. pass
  624. class NotAModule(object):
  625. test_2 = MyTestCase
  626. loader = unittest.TestLoader()
  627. suite = loader.loadTestsFromNames(['test_2'], NotAModule)
  628. reference = [unittest.TestSuite([MyTestCase('test')])]
  629. self.assertEqual(list(suite), reference)
  630. # "The specifier name is a ``dotted name'' that may resolve either to
  631. # a module, a test case class, a TestSuite instance, a test method
  632. # within a test case class, or a callable object which returns a
  633. # TestCase or TestSuite instance."
  634. #
  635. # Does it raise an exception if the name resolves to an invalid
  636. # object?
  637. def test_loadTestsFromNames__relative_bad_object(self):
  638. m = types.ModuleType('m')
  639. m.testcase_1 = object()
  640. loader = unittest.TestLoader()
  641. try:
  642. loader.loadTestsFromNames(['testcase_1'], m)
  643. except TypeError:
  644. pass
  645. else:
  646. self.fail("Should have raised TypeError")
  647. # "The specifier name is a ``dotted name'' that may resolve ... to
  648. # ... a test case class"
  649. def test_loadTestsFromNames__relative_TestCase_subclass(self):
  650. m = types.ModuleType('m')
  651. class MyTestCase(unittest.TestCase):
  652. def test(self):
  653. pass
  654. m.testcase_1 = MyTestCase
  655. loader = unittest.TestLoader()
  656. suite = loader.loadTestsFromNames(['testcase_1'], m)
  657. self.assertIsInstance(suite, loader.suiteClass)
  658. expected = loader.suiteClass([MyTestCase('test')])
  659. self.assertEqual(list(suite), [expected])
  660. # "The specifier name is a ``dotted name'' that may resolve ... to
  661. # ... a TestSuite instance"
  662. def test_loadTestsFromNames__relative_TestSuite(self):
  663. m = types.ModuleType('m')
  664. class MyTestCase(unittest.TestCase):
  665. def test(self):
  666. pass
  667. m.testsuite = unittest.TestSuite([MyTestCase('test')])
  668. loader = unittest.TestLoader()
  669. suite = loader.loadTestsFromNames(['testsuite'], m)
  670. self.assertIsInstance(suite, loader.suiteClass)
  671. self.assertEqual(list(suite), [m.testsuite])
  672. # "The specifier name is a ``dotted name'' that may resolve ... to ... a
  673. # test method within a test case class"
  674. def test_loadTestsFromNames__relative_testmethod(self):
  675. m = types.ModuleType('m')
  676. class MyTestCase(unittest.TestCase):
  677. def test(self):
  678. pass
  679. m.testcase_1 = MyTestCase
  680. loader = unittest.TestLoader()
  681. suite = loader.loadTestsFromNames(['testcase_1.test'], m)
  682. self.assertIsInstance(suite, loader.suiteClass)
  683. ref_suite = unittest.TestSuite([MyTestCase('test')])
  684. self.assertEqual(list(suite), [ref_suite])
  685. # "The specifier name is a ``dotted name'' that may resolve ... to ... a
  686. # test method within a test case class"
  687. #
  688. # Does the method gracefully handle names that initially look like they
  689. # resolve to "a test method within a test case class" but don't?
  690. def test_loadTestsFromNames__relative_invalid_testmethod(self):
  691. m = types.ModuleType('m')
  692. class MyTestCase(unittest.TestCase):
  693. def test(self):
  694. pass
  695. m.testcase_1 = MyTestCase
  696. loader = unittest.TestLoader()
  697. try:
  698. loader.loadTestsFromNames(['testcase_1.testfoo'], m)
  699. except AttributeError, e:
  700. self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
  701. else:
  702. self.fail("Failed to raise AttributeError")
  703. # "The specifier name is a ``dotted name'' that may resolve ... to
  704. # ... a callable object which returns a ... TestSuite instance"
  705. def test_loadTestsFromNames__callable__TestSuite(self):
  706. m = types.ModuleType('m')
  707. testcase_1 = unittest.FunctionTestCase(lambda: None)
  708. testcase_2 = unittest.FunctionTestCase(lambda: None)
  709. def return_TestSuite():
  710. return unittest.TestSuite([testcase_1, testcase_2])
  711. m.return_TestSuite = return_TestSuite
  712. loader = unittest.TestLoader()
  713. suite = loader.loadTestsFromNames(['return_TestSuite'], m)
  714. self.assertIsInstance(suite, loader.suiteClass)
  715. expected = unittest.TestSuite([testcase_1, testcase_2])
  716. self.assertEqual(list(suite), [expected])
  717. # "The specifier name is a ``dotted name'' that may resolve ... to
  718. # ... a callable object which returns a TestCase ... instance"
  719. def test_loadTestsFromNames__callable__TestCase_instance(self):
  720. m = types.ModuleType('m')
  721. testcase_1 = unittest.FunctionTestCase(lambda: None)
  722. def return_TestCase():
  723. return testcase_1
  724. m.return_TestCase = return_TestCase
  725. loader = unittest.TestLoader()
  726. suite = loader.loadTestsFromNames(['return_TestCase'], m)
  727. self.assertIsInstance(suite, loader.suiteClass)
  728. ref_suite = unittest.TestSuite([testcase_1])
  729. self.assertEqual(list(suite), [ref_suite])
  730. # "The specifier name is a ``dotted name'' that may resolve ... to
  731. # ... a callable object which returns a TestCase or TestSuite instance"
  732. #
  733. # Are staticmethods handled correctly?
  734. def test_loadTestsFromNames__callable__call_staticmethod(self):
  735. m = types.ModuleType('m')
  736. class Test1(unittest.TestCase):
  737. def test(self):
  738. pass
  739. testcase_1 = Test1('test')
  740. class Foo(unittest.TestCase):
  741. @staticmethod
  742. def foo():
  743. return testcase_1
  744. m.Foo = Foo
  745. loader = unittest.TestLoader()
  746. suite = loader.loadTestsFromNames(['Foo.foo'], m)
  747. self.assertIsInstance(suite, loader.suiteClass)
  748. ref_suite = unittest.TestSuite([testcase_1])
  749. self.assertEqual(list(suite), [ref_suite])
  750. # "The specifier name is a ``dotted name'' that may resolve ... to
  751. # ... a callable object which returns a TestCase or TestSuite instance"
  752. #
  753. # What happens when the callable returns something else?
  754. def test_loadTestsFromNames__callable__wrong_type(self):
  755. m = types.ModuleType('m')
  756. def return_wrong():
  757. return 6
  758. m.return_wrong = return_wrong
  759. loader = unittest.TestLoader()
  760. try:
  761. loader.loadTestsFromNames(['return_wrong'], m)
  762. except TypeError:
  763. pass
  764. else:
  765. self.fail("TestLoader.loadTestsFromNames failed to raise TypeError")
  766. # "The specifier can refer to modules and packages which have not been
  767. # imported; they will be imported as a side-effect"
  768. def test_loadTestsFromNames__module_not_loaded(self):
  769. # We're going to try to load this module as a side-effect, so it
  770. # better not be loaded before we try.
  771. #
  772. module_name = 'unittest.test.dummy'
  773. sys.modules.pop(module_name, None)
  774. loader = unittest.TestLoader()
  775. try:
  776. suite = loader.loadTestsFromNames([module_name])
  777. self.assertIsInstance(suite, loader.suiteClass)
  778. self.assertEqual(list(suite), [unittest.TestSuite()])
  779. # module should now be loaded, thanks to loadTestsFromName()
  780. self.assertIn(module_name, sys.modules)
  781. finally:
  782. if module_name in sys.modules:
  783. del sys.modules[module_name]
  784. ################################################################
  785. ### /Tests for TestLoader.loadTestsFromNames()
  786. ### Tests for TestLoader.getTestCaseNames()
  787. ################################################################
  788. # "Return a sorted sequence of method names found within testCaseClass"
  789. #
  790. # Test.foobar is defined to make sure getTestCaseNames() respects
  791. # loader.testMethodPrefix
  792. def test_getTestCaseNames(self):
  793. class Test(unittest.TestCase):
  794. def test_1(self): pass
  795. def test_2(self): pass
  796. def foobar(self): pass
  797. loader = unittest.TestLoader()
  798. self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2'])
  799. # "Return a sorted sequence of method names found within testCaseClass"
  800. #
  801. # Does getTestCaseNames() behave appropriately if no tests are found?
  802. def test_getTestCaseNames__no_tests(self):
  803. class Test(unittest.TestCase):
  804. def foobar(self): pass
  805. loader = unittest.TestLoader()
  806. self.assertEqual(loader.getTestCaseNames(Test), [])
  807. # "Return a sorted sequence of method names found within testCaseClass"
  808. #
  809. # Are not-TestCases handled gracefully?
  810. #
  811. # XXX This should raise a TypeError, not return a list
  812. #
  813. # XXX It's too late in the 2.5 release cycle to fix this, but it should
  814. # probably be revisited for 2.6
  815. def test_getTestCaseNames__not_a_TestCase(self):
  816. class BadCase(int):
  817. def test_foo(self):
  818. pass
  819. loader = unittest.TestLoader()
  820. names = loader.getTestCaseNames(BadCase)
  821. self.assertEqual(names, ['test_foo'])
  822. # "Return a sorted sequence of method names found within testCaseClass"
  823. #
  824. # Make sure inherited names are handled.
  825. #
  826. # TestP.foobar is defined to make sure getTestCaseNames() respects
  827. # loader.testMethodPrefix
  828. def test_getTestCaseNames__inheritance(self):
  829. class TestP(unittest.TestCase):
  830. def test_1(self): pass
  831. def test_2(self): pass
  832. def foobar(self): pass
  833. class TestC(TestP):
  834. def test_1(self): pass
  835. def test_3(self): pass
  836. loader = unittest.TestLoader()
  837. names = ['test_1', 'test_2', 'test_3']
  838. self.assertEqual(loader.getTestCaseNames(TestC), names)
  839. ################################################################
  840. ### /Tests for TestLoader.getTestCaseNames()
  841. ### Tests for TestLoader.testMethodPrefix
  842. ################################################################
  843. # "String giving the prefix of method names which will be interpreted as
  844. # test methods"
  845. #
  846. # Implicit in the documentation is that testMethodPrefix is respected by
  847. # all loadTestsFrom* methods.
  848. def test_testMethodPrefix__loadTestsFromTestCase(self):
  849. class Foo(unittest.TestCase):
  850. def test_1(self): pass
  851. def test_2(self): pass
  852. def foo_bar(self): pass
  853. tests_1 = unittest.TestSuite([Foo('foo_bar')])
  854. tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
  855. loader = unittest.TestLoader()
  856. loader.testMethodPrefix = 'foo'
  857. self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1)
  858. loader.testMethodPrefix = 'test'
  859. self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2)
  860. # "String giving the prefix of method names which will be interpreted as
  861. # test methods"
  862. #
  863. # Implicit in the documentation is that testMethodPrefix is respected by
  864. # all loadTestsFrom* methods.
  865. def test_testMethodPrefix__loadTestsFromModule(self):
  866. m = types.ModuleType('m')
  867. class Foo(unittest.TestCase):
  868. def test_1(self): pass
  869. def test_2(self): pass
  870. def foo_bar(self): pass
  871. m.Foo = Foo
  872. tests_1 = [unittest.TestSuite([Foo('foo_bar')])]
  873. tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])]
  874. loader = unittest.TestLoader()
  875. loader.testMethodPrefix = 'foo'
  876. self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1)
  877. loader.testMethodPrefix = 'test'
  878. self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2)
  879. # "String giving the prefix of method names which will be interpreted as
  880. # test methods"
  881. #
  882. # Implicit in the documentation is that testMethodPrefix is respected by
  883. # all loadTestsFrom* methods.
  884. def test_testMethodPrefix__loadTestsFromName(self):
  885. m = types.ModuleType('m')
  886. class Foo(unittest.TestCase):
  887. def test_1(self): pass
  888. def test_2(self): pass
  889. def foo_bar(self): pass
  890. m.Foo = Foo
  891. tests_1 = unittest.TestSuite([Foo('foo_bar')])
  892. tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
  893. loader = unittest.TestLoader()
  894. loader.testMethodPrefix = 'foo'
  895. self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1)
  896. loader.testMethodPrefix = 'test'
  897. self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2)
  898. # "String giving the prefix of method names which will be interpreted as
  899. # test methods"
  900. #
  901. # Implicit in the documentation is that testMethodPrefix is respected by
  902. # all loadTestsFrom* methods.
  903. def test_testMethodPrefix__loadTestsFromNames(self):
  904. m = types.ModuleType('m')
  905. class Foo(unittest.TestCase):
  906. def test_1(self): pass
  907. def test_2(self): pass
  908. def foo_bar(self): pass
  909. m.Foo = Foo
  910. tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])])
  911. tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
  912. tests_2 = unittest.TestSuite([tests_2])
  913. loader = unittest.TestLoader()
  914. loader.testMethodPrefix = 'foo'
  915. self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1)
  916. loader.testMethodPrefix = 'test'
  917. self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2)
  918. # "The default value is 'test'"
  919. def test_testMethodPrefix__default_value(self):
  920. loader = unittest.TestLoader()
  921. self.assertTrue(loader.testMethodPrefix == 'test')
  922. ################################################################
  923. ### /Tests for TestLoader.testMethodPrefix
  924. ### Tests for TestLoader.sortTestMethodsUsing
  925. ################################################################
  926. # "Function to be used to compare method names when sorting them in
  927. # getTestCaseNames() and all the loadTestsFromX() methods"
  928. def test_sortTestMethodsUsing__loadTestsFromTestCase(self):
  929. def reversed_cmp(x, y):
  930. return -cmp(x, y)
  931. class Foo(unittest.TestCase):
  932. def test_1(self): pass
  933. def test_2(self): pass
  934. loader = unittest.TestLoader()
  935. loader.sortTestMethodsUsing = reversed_cmp
  936. tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
  937. self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
  938. # "Function to be used to compare method names when sorting them in
  939. # getTestCaseNames() and all the loadTestsFromX() methods"
  940. def test_sortTestMethodsUsing__loadTestsFromModule(self):
  941. def reversed_cmp(x, y):
  942. return -cmp(x, y)
  943. m = types.ModuleType('m')
  944. class Foo(unittest.TestCase):
  945. def test_1(self): pass
  946. def test_2(self): pass
  947. m.Foo = Foo
  948. loader = unittest.TestLoader()
  949. loader.sortTestMethodsUsing = reversed_cmp
  950. tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
  951. self.assertEqual(list(loader.loadTestsFromModule(m)), tests)
  952. # "Function to be used to compare method names when sorting them in
  953. # getTestCaseNames() and all the loadTestsFromX() methods"
  954. def test_sortTestMethodsUsing__loadTestsFromName(self):
  955. def reversed_cmp(x, y):
  956. return -cmp(x, y)
  957. m = types.ModuleType('m')
  958. class Foo(unittest.TestCase):
  959. def test_1(self): pass
  960. def test_2(self): pass
  961. m.Foo = Foo
  962. loader = unittest.TestLoader()
  963. loader.sortTestMethodsUsing = reversed_cmp
  964. tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
  965. self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
  966. # "Function to be used to compare method names when sorting them in
  967. # getTestCaseNames() and all the loadTestsFromX() methods"
  968. def test_sortTestMethodsUsing__loadTestsFromNames(self):
  969. def reversed_cmp(x, y):
  970. return -cmp(x, y)
  971. m = types.ModuleType('m')
  972. class Foo(unittest.TestCase):
  973. def test_1(self): pass
  974. def test_2(self): pass
  975. m.Foo = Foo
  976. loader = unittest.TestLoader()
  977. loader.sortTestMethodsUsing = reversed_cmp
  978. tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
  979. self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests)
  980. # "Function to be used to compare method names when sorting them in
  981. # getTestCaseNames()"
  982. #
  983. # Does it actually affect getTestCaseNames()?
  984. def test_sortTestMethodsUsing__getTestCaseNames(self):
  985. def reversed_cmp(x, y):
  986. return -cmp(x, y)
  987. class Foo(unittest.TestCase):
  988. def test_1(self): pass
  989. def test_2(self): pass
  990. loader = unittest.TestLoader()
  991. loader.sortTestMethodsUsing = reversed_cmp
  992. test_names = ['test_2', 'test_1']
  993. self.assertEqual(loader.getTestCaseNames(Foo), test_names)
  994. # "The default value is the built-in cmp() function"
  995. def test_sortTestMethodsUsing__default_value(self):
  996. loader = unittest.TestLoader()
  997. self.assertTrue(loader.sortTestMethodsUsing is cmp)
  998. # "it can be set to None to disable the sort."
  999. #
  1000. # XXX How is this different from reassigning cmp? Are the tests returned
  1001. # in a random order or something? This behaviour should die
  1002. def test_sortTestMethodsUsing__None(self):
  1003. class Foo(unittest.TestCase):
  1004. def test_1(self): pass
  1005. def test_2(self): pass
  1006. loader = unittest.TestLoader()
  1007. loader.sortTestMethodsUsing = None
  1008. test_names = ['test_2', 'test_1']
  1009. self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names))
  1010. ################################################################
  1011. ### /Tests for TestLoader.sortTestMethodsUsing
  1012. ### Tests for TestLoader.suiteClass
  1013. ################################################################
  1014. # "Callable object that constructs a test suite from a list of tests."
  1015. def test_suiteClass__loadTestsFromTestCase(self):
  1016. class Foo(unittest.TestCase):
  1017. def test_1(self): pass
  1018. def test_2(self): pass
  1019. def foo_bar(self): pass
  1020. tests = [Foo('test_1'), Foo('test_2')]
  1021. loader = unittest.TestLoader()
  1022. loader.suiteClass = list
  1023. self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
  1024. # It is implicit in the documentation for TestLoader.suiteClass that
  1025. # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
  1026. def test_suiteClass__loadTestsFromModule(self):
  1027. m = types.ModuleType('m')
  1028. class Foo(unittest.TestCase):
  1029. def test_1(self): pass
  1030. def test_2(self): pass
  1031. def foo_bar(self): pass
  1032. m.Foo = Foo
  1033. tests = [[Foo('test_1'), Foo('test_2')]]
  1034. loader = unittest.TestLoader()
  1035. loader.suiteClass = list
  1036. self.assertEqual(loader.loadTestsFromModule(m), tests)
  1037. # It is implicit in the documentation for TestLoader.suiteClass that
  1038. # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
  1039. def test_suiteClass__loadTestsFromName(self):
  1040. m = types.ModuleType('m')
  1041. class Foo(unittest.TestCase):
  1042. def test_1(self): pass
  1043. def test_2(self): pass
  1044. def foo_bar(self): pass
  1045. m.Foo = Foo
  1046. tests = [Foo('test_1'), Foo('test_2')]
  1047. loader = unittest.TestLoader()
  1048. loader.suiteClass = list
  1049. self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
  1050. # It is implicit in the documentation for TestLoader.suiteClass that
  1051. # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
  1052. def test_suiteClass__loadTestsFromNames(self):
  1053. m = types.ModuleType('m')
  1054. class Foo(unittest.TestCase):
  1055. def test_1(self): pass
  1056. def test_2(self): pass
  1057. def foo_bar(self): pass
  1058. m.Foo = Foo
  1059. tests = [[Foo('test_1'), Foo('test_2')]]
  1060. loader = unittest.TestLoader()
  1061. loader.suiteClass = list
  1062. self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests)
  1063. # "The default value is the TestSuite class"
  1064. def test_suiteClass__default_value(self):
  1065. loader = unittest.TestLoader()
  1066. self.assertIs(loader.suiteClass, unittest.TestSuite)
  1067. # Make sure the dotted name resolution works even if the actual
  1068. # function doesn't have the same name as is used to find it.
  1069. def test_loadTestsFromName__function_with_different_name_than_method(self):
  1070. # lambdas have the name '<lambda>'.
  1071. m = types.ModuleType('m')
  1072. class MyTestCase(unittest.TestCase):
  1073. test = lambda: 1
  1074. m.testcase_1 = MyTestCase
  1075. loader = unittest.TestLoader()
  1076. suite = loader.loadTestsFromNames(['testcase_1.test'], m)
  1077. self.assertIsInstance(suite, loader.suiteClass)
  1078. ref_suite = unittest.TestSuite([MyTestCase('test')])
  1079. self.assertEqual(list(suite), [ref_suite])
  1080. if __name__ == '__main__':
  1081. unittest.main()