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
9 from unittest.runner import _WritelnDecorator
11 from unittest import _WritelnDecorator
14 class TestDeprecatedPlugin(unittest.TestCase):
16 def test_api_present(self):
22 def test_prepare_patches_result(self):
23 stream = _WritelnDecorator(StringIO())
24 res = _TextTestResult(stream, 0, 1)
26 sk.prepareTestResult(res)
29 res._orig_wasSuccessful
33 {DeprecatedTest: (res.deprecated, 'DEPRECATED', False)})
35 # result w/out print works too
36 res = unittest.TestResult()
38 sk.prepareTestResult(res)
43 {DeprecatedTest: (res.deprecated, 'DEPRECATED', False)})
45 def test_patched_result_handles_deprecated(self):
46 res = unittest.TestResult()
48 sk.prepareTestResult(res)
50 class TC(unittest.TestCase):
52 raise DeprecatedTest('deprecated me')
56 assert not res.errors, "Deprecated was not caught: %s" % res.errors
58 assert res.deprecated[0][0] is test
60 def test_patches_only_when_needed(self):
61 class NoPatch(unittest.TestResult):
63 self.errorClasses = {}
67 sk.prepareTestResult(res)
68 assert not hasattr(res, '_orig_addError'), \
69 "Deprecated patched a result class it didn't need to patch"
72 def test_deprecated_output(self):
73 class TC(unittest.TestCase):
75 raise DeprecatedTest('deprecated me')
77 stream = _WritelnDecorator(StringIO())
78 res = _TextTestResult(stream, 0, 1)
80 sk.prepareTestResult(res)
84 assert not res.errors, "Deprecated was not caught: %s" % res.errors
88 out = stream.getvalue()
90 assert out.strip() == "D"
91 assert res.wasSuccessful()
93 def test_deprecated_output_verbose(self):
95 class TC(unittest.TestCase):
97 raise DeprecatedTest('deprecated me too')
99 stream = _WritelnDecorator(StringIO())
100 res = _TextTestResult(stream, 0, verbosity=2)
102 sk.prepareTestResult(res)
105 assert not res.errors, "Deprecated was not caught: %s" % res.errors
106 assert res.deprecated
109 out = stream.getvalue()
113 assert ' ... DEPRECATED' in out
114 assert 'deprecated me too' in out
116 def test_enabled_by_default(self):
118 assert sk.enabled, "Deprecated was not enabled by default"
120 def test_can_be_disabled(self):
121 parser = OptionParser()
123 sk.addOptions(parser)
124 options, args = parser.parse_args(['--no-deprecated'])
125 sk.configure(options, Config())
126 assert not sk.enabled, \
127 "Deprecated was not disabled by noDeprecated option"
130 if __name__ == '__main__':