/unit_tests/test_deprecated_plugin.py

https://bitbucket.org/jpellerin/nose/ · Python · 131 lines · 105 code · 24 blank · 2 comment · 3 complexity · c8b823a9074218baa32bd72e9f686dfd MD5 · raw file

  1. import unittest
  2. from nose.config import Config
  3. from nose.plugins.deprecated import Deprecated, DeprecatedTest
  4. from nose.result import TextTestResult, _TextTestResult
  5. from StringIO import StringIO
  6. from optparse import OptionParser
  7. try:
  8. # 2.7+
  9. from unittest.runner import _WritelnDecorator
  10. except ImportError:
  11. from unittest import _WritelnDecorator
  12. class TestDeprecatedPlugin(unittest.TestCase):
  13. def test_api_present(self):
  14. sk = Deprecated()
  15. sk.addOptions
  16. sk.configure
  17. sk.prepareTestResult
  18. def test_prepare_patches_result(self):
  19. stream = _WritelnDecorator(StringIO())
  20. res = _TextTestResult(stream, 0, 1)
  21. sk = Deprecated()
  22. sk.prepareTestResult(res)
  23. res._orig_addError
  24. res._orig_printErrors
  25. res._orig_wasSuccessful
  26. res.deprecated
  27. self.assertEqual(
  28. res.errorClasses,
  29. {DeprecatedTest: (res.deprecated, 'DEPRECATED', False)})
  30. # result w/out print works too
  31. res = unittest.TestResult()
  32. sk = Deprecated()
  33. sk.prepareTestResult(res)
  34. res._orig_addError
  35. res.deprecated
  36. self.assertEqual(
  37. res.errorClasses,
  38. {DeprecatedTest: (res.deprecated, 'DEPRECATED', False)})
  39. def test_patched_result_handles_deprecated(self):
  40. res = unittest.TestResult()
  41. sk = Deprecated()
  42. sk.prepareTestResult(res)
  43. class TC(unittest.TestCase):
  44. def test(self):
  45. raise DeprecatedTest('deprecated me')
  46. test = TC('test')
  47. test(res)
  48. assert not res.errors, "Deprecated was not caught: %s" % res.errors
  49. assert res.deprecated
  50. assert res.deprecated[0][0] is test
  51. def test_patches_only_when_needed(self):
  52. class NoPatch(unittest.TestResult):
  53. def __init__(self):
  54. self.errorClasses = {}
  55. res = NoPatch()
  56. sk = Deprecated()
  57. sk.prepareTestResult(res)
  58. assert not hasattr(res, '_orig_addError'), \
  59. "Deprecated patched a result class it didn't need to patch"
  60. def test_deprecated_output(self):
  61. class TC(unittest.TestCase):
  62. def test(self):
  63. raise DeprecatedTest('deprecated me')
  64. stream = _WritelnDecorator(StringIO())
  65. res = _TextTestResult(stream, 0, 1)
  66. sk = Deprecated()
  67. sk.prepareTestResult(res)
  68. test = TC('test')
  69. test(res)
  70. assert not res.errors, "Deprecated was not caught: %s" % res.errors
  71. assert res.deprecated
  72. res.printErrors()
  73. out = stream.getvalue()
  74. assert out
  75. assert out.strip() == "D"
  76. assert res.wasSuccessful()
  77. def test_deprecated_output_verbose(self):
  78. class TC(unittest.TestCase):
  79. def test(self):
  80. raise DeprecatedTest('deprecated me too')
  81. stream = _WritelnDecorator(StringIO())
  82. res = _TextTestResult(stream, 0, verbosity=2)
  83. sk = Deprecated()
  84. sk.prepareTestResult(res)
  85. test = TC('test')
  86. test(res)
  87. assert not res.errors, "Deprecated was not caught: %s" % res.errors
  88. assert res.deprecated
  89. res.printErrors()
  90. out = stream.getvalue()
  91. print out
  92. assert out
  93. assert ' ... DEPRECATED' in out
  94. assert 'deprecated me too' in out
  95. def test_enabled_by_default(self):
  96. sk = Deprecated()
  97. assert sk.enabled, "Deprecated was not enabled by default"
  98. def test_can_be_disabled(self):
  99. parser = OptionParser()
  100. sk = Deprecated()
  101. sk.addOptions(parser)
  102. options, args = parser.parse_args(['--no-deprecated'])
  103. sk.configure(options, Config())
  104. assert not sk.enabled, \
  105. "Deprecated was not disabled by noDeprecated option"
  106. if __name__ == '__main__':
  107. unittest.main()