Imported Upstream version 1.1.2
[platform/upstream/python-nose.git] / unit_tests / test_deprecated_plugin.py
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
13
14 class TestDeprecatedPlugin(unittest.TestCase):
15
16     def test_api_present(self):
17         sk = Deprecated()
18         sk.addOptions
19         sk.configure
20         sk.prepareTestResult        
21
22     def test_prepare_patches_result(self):
23         stream = _WritelnDecorator(StringIO())
24         res = _TextTestResult(stream, 0, 1)
25         sk = Deprecated()
26         sk.prepareTestResult(res)
27         res._orig_addError
28         res._orig_printErrors
29         res._orig_wasSuccessful
30         res.deprecated
31         self.assertEqual(
32             res.errorClasses,
33             {DeprecatedTest: (res.deprecated, 'DEPRECATED', False)})
34
35         # result w/out print works too
36         res = unittest.TestResult()
37         sk = Deprecated()
38         sk.prepareTestResult(res)
39         res._orig_addError
40         res.deprecated
41         self.assertEqual(
42             res.errorClasses,
43             {DeprecatedTest: (res.deprecated, 'DEPRECATED', False)})
44
45     def test_patched_result_handles_deprecated(self):
46         res = unittest.TestResult()
47         sk = Deprecated()
48         sk.prepareTestResult(res)
49
50         class TC(unittest.TestCase):
51             def test(self):
52                 raise DeprecatedTest('deprecated me')
53
54         test = TC('test')
55         test(res)
56         assert not res.errors, "Deprecated was not caught: %s" % res.errors
57         assert res.deprecated
58         assert res.deprecated[0][0] is test
59
60     def test_patches_only_when_needed(self):
61         class NoPatch(unittest.TestResult):
62             def __init__(self):
63                 self.errorClasses = {}
64                 
65         res = NoPatch()
66         sk = Deprecated()
67         sk.prepareTestResult(res)
68         assert not hasattr(res, '_orig_addError'), \
69                "Deprecated patched a result class it didn't need to patch"
70         
71
72     def test_deprecated_output(self):
73         class TC(unittest.TestCase):
74             def test(self):
75                 raise DeprecatedTest('deprecated me')
76
77         stream = _WritelnDecorator(StringIO())
78         res = _TextTestResult(stream, 0, 1)
79         sk = Deprecated()
80         sk.prepareTestResult(res)
81
82         test = TC('test')
83         test(res)
84         assert not res.errors, "Deprecated was not caught: %s" % res.errors
85         assert res.deprecated            
86
87         res.printErrors()
88         out = stream.getvalue()
89         assert out
90         assert out.strip() == "D"
91         assert res.wasSuccessful()
92
93     def test_deprecated_output_verbose(self):
94
95         class TC(unittest.TestCase):
96             def test(self):
97                 raise DeprecatedTest('deprecated me too')
98         
99         stream = _WritelnDecorator(StringIO())
100         res = _TextTestResult(stream, 0, verbosity=2)
101         sk = Deprecated()
102         sk.prepareTestResult(res)
103         test = TC('test')
104         test(res)
105         assert not res.errors, "Deprecated was not caught: %s" % res.errors
106         assert res.deprecated            
107
108         res.printErrors()
109         out = stream.getvalue()
110         print out
111         assert out
112
113         assert ' ... DEPRECATED' in out
114         assert 'deprecated me too' in out
115
116     def test_enabled_by_default(self):
117         sk = Deprecated()
118         assert sk.enabled, "Deprecated was not enabled by default"
119
120     def test_can_be_disabled(self):
121         parser = OptionParser()
122         sk = Deprecated()
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"
128         
129
130 if __name__ == '__main__':
131     unittest.main()