test_suite.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. import unittest
  2. import gc
  3. import sys
  4. import weakref
  5. from unittest.test.support import LoggingResult, TestEquality
  6. ### Support code for Test_TestSuite
  7. ################################################################
  8. class Test(object):
  9. class Foo(unittest.TestCase):
  10. def test_1(self): pass
  11. def test_2(self): pass
  12. def test_3(self): pass
  13. def runTest(self): pass
  14. def _mk_TestSuite(*names):
  15. return unittest.TestSuite(Test.Foo(n) for n in names)
  16. ################################################################
  17. class Test_TestSuite(unittest.TestCase, TestEquality):
  18. ### Set up attributes needed by inherited tests
  19. ################################################################
  20. # Used by TestEquality.test_eq
  21. eq_pairs = [(unittest.TestSuite(), unittest.TestSuite())
  22. ,(unittest.TestSuite(), unittest.TestSuite([]))
  23. ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_1'))]
  24. # Used by TestEquality.test_ne
  25. ne_pairs = [(unittest.TestSuite(), _mk_TestSuite('test_1'))
  26. ,(unittest.TestSuite([]), _mk_TestSuite('test_1'))
  27. ,(_mk_TestSuite('test_1', 'test_2'), _mk_TestSuite('test_1', 'test_3'))
  28. ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_2'))]
  29. ################################################################
  30. ### /Set up attributes needed by inherited tests
  31. ### Tests for TestSuite.__init__
  32. ################################################################
  33. # "class TestSuite([tests])"
  34. #
  35. # The tests iterable should be optional
  36. def test_init__tests_optional(self):
  37. suite = unittest.TestSuite()
  38. self.assertEqual(suite.countTestCases(), 0)
  39. # countTestCases() still works after tests are run
  40. suite.run(unittest.TestResult())
  41. self.assertEqual(suite.countTestCases(), 0)
  42. # "class TestSuite([tests])"
  43. # ...
  44. # "If tests is given, it must be an iterable of individual test cases
  45. # or other test suites that will be used to build the suite initially"
  46. #
  47. # TestSuite should deal with empty tests iterables by allowing the
  48. # creation of an empty suite
  49. def test_init__empty_tests(self):
  50. suite = unittest.TestSuite([])
  51. self.assertEqual(suite.countTestCases(), 0)
  52. # countTestCases() still works after tests are run
  53. suite.run(unittest.TestResult())
  54. self.assertEqual(suite.countTestCases(), 0)
  55. # "class TestSuite([tests])"
  56. # ...
  57. # "If tests is given, it must be an iterable of individual test cases
  58. # or other test suites that will be used to build the suite initially"
  59. #
  60. # TestSuite should allow any iterable to provide tests
  61. def test_init__tests_from_any_iterable(self):
  62. def tests():
  63. yield unittest.FunctionTestCase(lambda: None)
  64. yield unittest.FunctionTestCase(lambda: None)
  65. suite_1 = unittest.TestSuite(tests())
  66. self.assertEqual(suite_1.countTestCases(), 2)
  67. suite_2 = unittest.TestSuite(suite_1)
  68. self.assertEqual(suite_2.countTestCases(), 2)
  69. suite_3 = unittest.TestSuite(set(suite_1))
  70. self.assertEqual(suite_3.countTestCases(), 2)
  71. # countTestCases() still works after tests are run
  72. suite_1.run(unittest.TestResult())
  73. self.assertEqual(suite_1.countTestCases(), 2)
  74. suite_2.run(unittest.TestResult())
  75. self.assertEqual(suite_2.countTestCases(), 2)
  76. suite_3.run(unittest.TestResult())
  77. self.assertEqual(suite_3.countTestCases(), 2)
  78. # "class TestSuite([tests])"
  79. # ...
  80. # "If tests is given, it must be an iterable of individual test cases
  81. # or other test suites that will be used to build the suite initially"
  82. #
  83. # Does TestSuite() also allow other TestSuite() instances to be present
  84. # in the tests iterable?
  85. def test_init__TestSuite_instances_in_tests(self):
  86. def tests():
  87. ftc = unittest.FunctionTestCase(lambda: None)
  88. yield unittest.TestSuite([ftc])
  89. yield unittest.FunctionTestCase(lambda: None)
  90. suite = unittest.TestSuite(tests())
  91. self.assertEqual(suite.countTestCases(), 2)
  92. # countTestCases() still works after tests are run
  93. suite.run(unittest.TestResult())
  94. self.assertEqual(suite.countTestCases(), 2)
  95. ################################################################
  96. ### /Tests for TestSuite.__init__
  97. # Container types should support the iter protocol
  98. def test_iter(self):
  99. test1 = unittest.FunctionTestCase(lambda: None)
  100. test2 = unittest.FunctionTestCase(lambda: None)
  101. suite = unittest.TestSuite((test1, test2))
  102. self.assertEqual(list(suite), [test1, test2])
  103. # "Return the number of tests represented by the this test object.
  104. # ...this method is also implemented by the TestSuite class, which can
  105. # return larger [greater than 1] values"
  106. #
  107. # Presumably an empty TestSuite returns 0?
  108. def test_countTestCases_zero_simple(self):
  109. suite = unittest.TestSuite()
  110. self.assertEqual(suite.countTestCases(), 0)
  111. # "Return the number of tests represented by the this test object.
  112. # ...this method is also implemented by the TestSuite class, which can
  113. # return larger [greater than 1] values"
  114. #
  115. # Presumably an empty TestSuite (even if it contains other empty
  116. # TestSuite instances) returns 0?
  117. def test_countTestCases_zero_nested(self):
  118. class Test1(unittest.TestCase):
  119. def test(self):
  120. pass
  121. suite = unittest.TestSuite([unittest.TestSuite()])
  122. self.assertEqual(suite.countTestCases(), 0)
  123. # "Return the number of tests represented by the this test object.
  124. # ...this method is also implemented by the TestSuite class, which can
  125. # return larger [greater than 1] values"
  126. def test_countTestCases_simple(self):
  127. test1 = unittest.FunctionTestCase(lambda: None)
  128. test2 = unittest.FunctionTestCase(lambda: None)
  129. suite = unittest.TestSuite((test1, test2))
  130. self.assertEqual(suite.countTestCases(), 2)
  131. # countTestCases() still works after tests are run
  132. suite.run(unittest.TestResult())
  133. self.assertEqual(suite.countTestCases(), 2)
  134. # "Return the number of tests represented by the this test object.
  135. # ...this method is also implemented by the TestSuite class, which can
  136. # return larger [greater than 1] values"
  137. #
  138. # Make sure this holds for nested TestSuite instances, too
  139. def test_countTestCases_nested(self):
  140. class Test1(unittest.TestCase):
  141. def test1(self): pass
  142. def test2(self): pass
  143. test2 = unittest.FunctionTestCase(lambda: None)
  144. test3 = unittest.FunctionTestCase(lambda: None)
  145. child = unittest.TestSuite((Test1('test2'), test2))
  146. parent = unittest.TestSuite((test3, child, Test1('test1')))
  147. self.assertEqual(parent.countTestCases(), 4)
  148. # countTestCases() still works after tests are run
  149. parent.run(unittest.TestResult())
  150. self.assertEqual(parent.countTestCases(), 4)
  151. self.assertEqual(child.countTestCases(), 2)
  152. # "Run the tests associated with this suite, collecting the result into
  153. # the test result object passed as result."
  154. #
  155. # And if there are no tests? What then?
  156. def test_run__empty_suite(self):
  157. events = []
  158. result = LoggingResult(events)
  159. suite = unittest.TestSuite()
  160. suite.run(result)
  161. self.assertEqual(events, [])
  162. # "Note that unlike TestCase.run(), TestSuite.run() requires the
  163. # "result object to be passed in."
  164. def test_run__requires_result(self):
  165. suite = unittest.TestSuite()
  166. try:
  167. suite.run()
  168. except TypeError:
  169. pass
  170. else:
  171. self.fail("Failed to raise TypeError")
  172. # "Run the tests associated with this suite, collecting the result into
  173. # the test result object passed as result."
  174. def test_run(self):
  175. events = []
  176. result = LoggingResult(events)
  177. class LoggingCase(unittest.TestCase):
  178. def run(self, result):
  179. events.append('run %s' % self._testMethodName)
  180. def test1(self): pass
  181. def test2(self): pass
  182. tests = [LoggingCase('test1'), LoggingCase('test2')]
  183. unittest.TestSuite(tests).run(result)
  184. self.assertEqual(events, ['run test1', 'run test2'])
  185. # "Add a TestCase ... to the suite"
  186. def test_addTest__TestCase(self):
  187. class Foo(unittest.TestCase):
  188. def test(self): pass
  189. test = Foo('test')
  190. suite = unittest.TestSuite()
  191. suite.addTest(test)
  192. self.assertEqual(suite.countTestCases(), 1)
  193. self.assertEqual(list(suite), [test])
  194. # countTestCases() still works after tests are run
  195. suite.run(unittest.TestResult())
  196. self.assertEqual(suite.countTestCases(), 1)
  197. # "Add a ... TestSuite to the suite"
  198. def test_addTest__TestSuite(self):
  199. class Foo(unittest.TestCase):
  200. def test(self): pass
  201. suite_2 = unittest.TestSuite([Foo('test')])
  202. suite = unittest.TestSuite()
  203. suite.addTest(suite_2)
  204. self.assertEqual(suite.countTestCases(), 1)
  205. self.assertEqual(list(suite), [suite_2])
  206. # countTestCases() still works after tests are run
  207. suite.run(unittest.TestResult())
  208. self.assertEqual(suite.countTestCases(), 1)
  209. # "Add all the tests from an iterable of TestCase and TestSuite
  210. # instances to this test suite."
  211. #
  212. # "This is equivalent to iterating over tests, calling addTest() for
  213. # each element"
  214. def test_addTests(self):
  215. class Foo(unittest.TestCase):
  216. def test_1(self): pass
  217. def test_2(self): pass
  218. test_1 = Foo('test_1')
  219. test_2 = Foo('test_2')
  220. inner_suite = unittest.TestSuite([test_2])
  221. def gen():
  222. yield test_1
  223. yield test_2
  224. yield inner_suite
  225. suite_1 = unittest.TestSuite()
  226. suite_1.addTests(gen())
  227. self.assertEqual(list(suite_1), list(gen()))
  228. # "This is equivalent to iterating over tests, calling addTest() for
  229. # each element"
  230. suite_2 = unittest.TestSuite()
  231. for t in gen():
  232. suite_2.addTest(t)
  233. self.assertEqual(suite_1, suite_2)
  234. # "Add all the tests from an iterable of TestCase and TestSuite
  235. # instances to this test suite."
  236. #
  237. # What happens if it doesn't get an iterable?
  238. def test_addTest__noniterable(self):
  239. suite = unittest.TestSuite()
  240. try:
  241. suite.addTests(5)
  242. except TypeError:
  243. pass
  244. else:
  245. self.fail("Failed to raise TypeError")
  246. def test_addTest__noncallable(self):
  247. suite = unittest.TestSuite()
  248. self.assertRaises(TypeError, suite.addTest, 5)
  249. def test_addTest__casesuiteclass(self):
  250. suite = unittest.TestSuite()
  251. self.assertRaises(TypeError, suite.addTest, Test_TestSuite)
  252. self.assertRaises(TypeError, suite.addTest, unittest.TestSuite)
  253. def test_addTests__string(self):
  254. suite = unittest.TestSuite()
  255. self.assertRaises(TypeError, suite.addTests, "foo")
  256. def test_function_in_suite(self):
  257. def f(_):
  258. pass
  259. suite = unittest.TestSuite()
  260. suite.addTest(f)
  261. # when the bug is fixed this line will not crash
  262. suite.run(unittest.TestResult())
  263. def test_remove_test_at_index(self):
  264. if not unittest.BaseTestSuite._cleanup:
  265. raise unittest.SkipTest("Suite cleanup is disabled")
  266. suite = unittest.TestSuite()
  267. suite._tests = [1, 2, 3]
  268. suite._removeTestAtIndex(1)
  269. self.assertEqual([1, None, 3], suite._tests)
  270. def test_remove_test_at_index_not_indexable(self):
  271. if not unittest.BaseTestSuite._cleanup:
  272. raise unittest.SkipTest("Suite cleanup is disabled")
  273. suite = unittest.TestSuite()
  274. suite._tests = None
  275. # if _removeAtIndex raises for noniterables this next line will break
  276. suite._removeTestAtIndex(2)
  277. def assert_garbage_collect_test_after_run(self, TestSuiteClass):
  278. if not unittest.BaseTestSuite._cleanup:
  279. raise unittest.SkipTest("Suite cleanup is disabled")
  280. class Foo(unittest.TestCase):
  281. def test_nothing(self):
  282. pass
  283. test = Foo('test_nothing')
  284. wref = weakref.ref(test)
  285. suite = TestSuiteClass([wref()])
  286. suite.run(unittest.TestResult())
  287. del test
  288. # for the benefit of non-reference counting implementations
  289. gc.collect()
  290. self.assertEqual(suite._tests, [None])
  291. self.assertIsNone(wref())
  292. def test_garbage_collect_test_after_run_BaseTestSuite(self):
  293. self.assert_garbage_collect_test_after_run(unittest.BaseTestSuite)
  294. def test_garbage_collect_test_after_run_TestSuite(self):
  295. self.assert_garbage_collect_test_after_run(unittest.TestSuite)
  296. def test_basetestsuite(self):
  297. class Test(unittest.TestCase):
  298. wasSetUp = False
  299. wasTornDown = False
  300. @classmethod
  301. def setUpClass(cls):
  302. cls.wasSetUp = True
  303. @classmethod
  304. def tearDownClass(cls):
  305. cls.wasTornDown = True
  306. def testPass(self):
  307. pass
  308. def testFail(self):
  309. fail
  310. class Module(object):
  311. wasSetUp = False
  312. wasTornDown = False
  313. @staticmethod
  314. def setUpModule():
  315. Module.wasSetUp = True
  316. @staticmethod
  317. def tearDownModule():
  318. Module.wasTornDown = True
  319. Test.__module__ = 'Module'
  320. sys.modules['Module'] = Module
  321. self.addCleanup(sys.modules.pop, 'Module')
  322. suite = unittest.BaseTestSuite()
  323. suite.addTests([Test('testPass'), Test('testFail')])
  324. self.assertEqual(suite.countTestCases(), 2)
  325. result = unittest.TestResult()
  326. suite.run(result)
  327. self.assertFalse(Module.wasSetUp)
  328. self.assertFalse(Module.wasTornDown)
  329. self.assertFalse(Test.wasSetUp)
  330. self.assertFalse(Test.wasTornDown)
  331. self.assertEqual(len(result.errors), 1)
  332. self.assertEqual(len(result.failures), 0)
  333. self.assertEqual(result.testsRun, 2)
  334. self.assertEqual(suite.countTestCases(), 2)
  335. def test_overriding_call(self):
  336. class MySuite(unittest.TestSuite):
  337. called = False
  338. def __call__(self, *args, **kw):
  339. self.called = True
  340. unittest.TestSuite.__call__(self, *args, **kw)
  341. suite = MySuite()
  342. result = unittest.TestResult()
  343. wrapper = unittest.TestSuite()
  344. wrapper.addTest(suite)
  345. wrapper(result)
  346. self.assertTrue(suite.called)
  347. # reusing results should be permitted even if abominable
  348. self.assertFalse(result._testRunEntered)
  349. if __name__ == '__main__':
  350. unittest.main()