/packages/nose/unit_tests/test_cases.py

https://github.com/lmorchard/home-snippets-server-lib · Python · 252 lines · 193 code · 52 blank · 7 comment · 3 complexity · 3c26c57122953de8744f68a8fa2f5300 MD5 · raw file

  1. import unittest
  2. import pdb
  3. import sys
  4. import nose.case
  5. import nose.failure
  6. from nose.config import Config
  7. from mock import ResultProxyFactory, ResultProxy
  8. class TestNoseCases(unittest.TestCase):
  9. def test_function_test_case(self):
  10. res = unittest.TestResult()
  11. a = []
  12. def func(a=a):
  13. a.append(1)
  14. case = nose.case.FunctionTestCase(func)
  15. case(res)
  16. assert a[0] == 1
  17. def test_method_test_case(self):
  18. res = unittest.TestResult()
  19. a = []
  20. class TestClass(object):
  21. def test_func(self, a=a):
  22. a.append(1)
  23. case = nose.case.MethodTestCase(TestClass.test_func)
  24. case(res)
  25. assert a[0] == 1
  26. def test_method_test_case_with_metaclass(self):
  27. res = unittest.TestResult()
  28. class TestType(type):
  29. def __new__(cls, name, bases, dct):
  30. return type.__new__(cls, name, bases, dct)
  31. a = []
  32. class TestClass(object):
  33. __metaclass__ = TestType
  34. def test_func(self, a=a):
  35. a.append(1)
  36. case = nose.case.MethodTestCase(TestClass.test_func)
  37. case(res)
  38. assert a[0] == 1
  39. def test_method_test_case_fixtures(self):
  40. res = unittest.TestResult()
  41. called = []
  42. class TestClass(object):
  43. def setup(self):
  44. called.append('setup')
  45. def teardown(self):
  46. called.append('teardown')
  47. def test_func(self):
  48. called.append('test')
  49. case = nose.case.MethodTestCase(TestClass.test_func)
  50. case(res)
  51. self.assertEqual(called, ['setup', 'test', 'teardown'])
  52. class TestClassFailingSetup(TestClass):
  53. def setup(self):
  54. called.append('setup')
  55. raise Exception("failed")
  56. called[:] = []
  57. case = nose.case.MethodTestCase(TestClassFailingSetup.test_func)
  58. case(res)
  59. self.assertEqual(called, ['setup'])
  60. class TestClassFailingTest(TestClass):
  61. def test_func(self):
  62. called.append('test')
  63. raise Exception("failed")
  64. called[:] = []
  65. case = nose.case.MethodTestCase(TestClassFailingTest.test_func)
  66. case(res)
  67. self.assertEqual(called, ['setup', 'test', 'teardown'])
  68. def test_function_test_case_fixtures(self):
  69. from nose.tools import with_setup
  70. res = unittest.TestResult()
  71. called = {}
  72. def st():
  73. called['st'] = True
  74. def td():
  75. called['td'] = True
  76. def func_exc():
  77. called['func'] = True
  78. raise TypeError("An exception")
  79. func_exc = with_setup(st, td)(func_exc)
  80. case = nose.case.FunctionTestCase(func_exc)
  81. case(res)
  82. assert 'st' in called
  83. assert 'func' in called
  84. assert 'td' in called
  85. def test_failure_case(self):
  86. res = unittest.TestResult()
  87. f = nose.failure.Failure(ValueError, "No such test spam")
  88. f(res)
  89. assert res.errors
  90. class TestNoseTestWrapper(unittest.TestCase):
  91. def test_case_fixtures_called(self):
  92. """Instance fixtures are properly called for wrapped tests"""
  93. res = unittest.TestResult()
  94. called = []
  95. class TC(unittest.TestCase):
  96. def setUp(self):
  97. print "TC setUp %s" % self
  98. called.append('setUp')
  99. def runTest(self):
  100. print "TC runTest %s" % self
  101. called.append('runTest')
  102. def tearDown(self):
  103. print "TC tearDown %s" % self
  104. called.append('tearDown')
  105. case = nose.case.Test(TC())
  106. case(res)
  107. assert not res.errors, res.errors
  108. assert not res.failures, res.failures
  109. self.assertEqual(called, ['setUp', 'runTest', 'tearDown'])
  110. def test_result_proxy_used(self):
  111. """A result proxy is used to wrap the result for all tests"""
  112. class TC(unittest.TestCase):
  113. def runTest(self):
  114. raise Exception("error")
  115. ResultProxy.called[:] = []
  116. res = unittest.TestResult()
  117. config = Config()
  118. case = nose.case.Test(TC(), config=config,
  119. resultProxy=ResultProxyFactory())
  120. case(res)
  121. assert not res.errors, res.errors
  122. assert not res.failures, res.failures
  123. calls = [ c[0] for c in ResultProxy.called ]
  124. self.assertEqual(calls, ['beforeTest', 'startTest', 'addError',
  125. 'stopTest', 'afterTest'])
  126. def test_address(self):
  127. from nose.util import absfile, src
  128. class TC(unittest.TestCase):
  129. def runTest(self):
  130. raise Exception("error")
  131. def dummy(i):
  132. pass
  133. def test():
  134. pass
  135. class Test:
  136. def test(self):
  137. pass
  138. def test_gen(self):
  139. def tryit(i):
  140. pass
  141. for i in range (0, 2):
  142. yield tryit, i
  143. def try_something(self, a, b):
  144. pass
  145. fl = src(absfile(__file__))
  146. case = nose.case.Test(TC())
  147. self.assertEqual(case.address(), (fl, __name__, 'TC.runTest'))
  148. case = nose.case.Test(nose.case.FunctionTestCase(test))
  149. self.assertEqual(case.address(), (fl, __name__, 'test'))
  150. case = nose.case.Test(nose.case.FunctionTestCase(
  151. dummy, arg=(1,), descriptor=test))
  152. self.assertEqual(case.address(), (fl, __name__, 'test'))
  153. case = nose.case.Test(nose.case.MethodTestCase(Test.test))
  154. self.assertEqual(case.address(), (fl, __name__, 'Test.test'))
  155. case = nose.case.Test(
  156. nose.case.MethodTestCase(Test.try_something, arg=(1,2,),
  157. descriptor=Test.test_gen))
  158. self.assertEqual(case.address(),
  159. (fl, __name__, 'Test.test_gen'))
  160. case = nose.case.Test(
  161. nose.case.MethodTestCase(Test.test_gen, test=dummy, arg=(1,)))
  162. self.assertEqual(case.address(),
  163. (fl, __name__, 'Test.test_gen'))
  164. def test_context(self):
  165. class TC(unittest.TestCase):
  166. def runTest(self):
  167. pass
  168. def test():
  169. pass
  170. class Test:
  171. def test(self):
  172. pass
  173. case = nose.case.Test(TC())
  174. self.assertEqual(case.context, TC)
  175. case = nose.case.Test(nose.case.FunctionTestCase(test))
  176. self.assertEqual(case.context, sys.modules[__name__])
  177. case = nose.case.Test(nose.case.MethodTestCase(Test.test))
  178. self.assertEqual(case.context, Test)
  179. def test_short_description(self):
  180. class TC(unittest.TestCase):
  181. def test_a(self):
  182. """
  183. This is the description
  184. """
  185. pass
  186. def test_b(self):
  187. """This is the description
  188. """
  189. pass
  190. def test_c(self):
  191. pass
  192. case_a = nose.case.Test(TC('test_a'))
  193. case_b = nose.case.Test(TC('test_b'))
  194. case_c = nose.case.Test(TC('test_c'))
  195. assert case_a.shortDescription().endswith("This is the description")
  196. assert case_b.shortDescription().endswith("This is the description")
  197. assert case_c.shortDescription() in (None, # pre 2.7
  198. 'test_c (test_cases.TC)') # 2.7
  199. if __name__ == '__main__':
  200. unittest.main()