PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/clion/bin/gdb/lib/python2.7/unittest/test/test_suite.py

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