/unit_tests/test_cases.py

https://bitbucket.org/jpellerin/nose/ · Python · 274 lines · 212 code · 54 blank · 8 comment · 3 complexity · 963b7e6d68195feb636ff324698c68f2 MD5 · raw file

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