summaryrefslogtreecommitdiff
path: root/unit_tests/test_deprecated_plugin.py
blob: 6c6248114072203ef34e096166f23f350ed94698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import unittest
from nose.config import Config
from nose.plugins.deprecated import Deprecated, DeprecatedTest
from nose.result import TextTestResult, _TextTestResult
from StringIO import StringIO
from optparse import OptionParser
try:
    # 2.7+
    from unittest.runner import _WritelnDecorator
except ImportError:
    from unittest import _WritelnDecorator


class TestDeprecatedPlugin(unittest.TestCase):

    def test_api_present(self):
        sk = Deprecated()
        sk.addOptions
        sk.configure
        sk.prepareTestResult        

    def test_prepare_patches_result(self):
        stream = _WritelnDecorator(StringIO())
        res = _TextTestResult(stream, 0, 1)
        sk = Deprecated()
        sk.prepareTestResult(res)
        res._orig_addError
        res._orig_printErrors
        res._orig_wasSuccessful
        res.deprecated
        self.assertEqual(
            res.errorClasses,
            {DeprecatedTest: (res.deprecated, 'DEPRECATED', False)})

        # result w/out print works too
        res = unittest.TestResult()
        sk = Deprecated()
        sk.prepareTestResult(res)
        res._orig_addError
        res.deprecated
        self.assertEqual(
            res.errorClasses,
            {DeprecatedTest: (res.deprecated, 'DEPRECATED', False)})

    def test_patched_result_handles_deprecated(self):
        res = unittest.TestResult()
        sk = Deprecated()
        sk.prepareTestResult(res)

        class TC(unittest.TestCase):
            def test(self):
                raise DeprecatedTest('deprecated me')

        test = TC('test')
        test(res)
        assert not res.errors, "Deprecated was not caught: %s" % res.errors
        assert res.deprecated
        assert res.deprecated[0][0] is test

    def test_patches_only_when_needed(self):
        class NoPatch(unittest.TestResult):
            def __init__(self):
                self.errorClasses = {}
                
        res = NoPatch()
        sk = Deprecated()
        sk.prepareTestResult(res)
        assert not hasattr(res, '_orig_addError'), \
               "Deprecated patched a result class it didn't need to patch"
        

    def test_deprecated_output(self):
        class TC(unittest.TestCase):
            def test(self):
                raise DeprecatedTest('deprecated me')

        stream = _WritelnDecorator(StringIO())
        res = _TextTestResult(stream, 0, 1)
        sk = Deprecated()
        sk.prepareTestResult(res)

        test = TC('test')
        test(res)
        assert not res.errors, "Deprecated was not caught: %s" % res.errors
        assert res.deprecated            

        res.printErrors()
        out = stream.getvalue()
        assert out
        assert out.strip() == "D"
        assert res.wasSuccessful()

    def test_deprecated_output_verbose(self):

        class TC(unittest.TestCase):
            def test(self):
                raise DeprecatedTest('deprecated me too')
        
        stream = _WritelnDecorator(StringIO())
        res = _TextTestResult(stream, 0, verbosity=2)
        sk = Deprecated()
        sk.prepareTestResult(res)
        test = TC('test')
        test(res)
        assert not res.errors, "Deprecated was not caught: %s" % res.errors
        assert res.deprecated            

        res.printErrors()
        out = stream.getvalue()
        print out
        assert out

        assert ' ... DEPRECATED' in out
        assert 'deprecated me too' in out

    def test_enabled_by_default(self):
        sk = Deprecated()
        assert sk.enabled, "Deprecated was not enabled by default"

    def test_can_be_disabled(self):
        parser = OptionParser()
        sk = Deprecated()
        sk.addOptions(parser)
        options, args = parser.parse_args(['--no-deprecated'])
        sk.configure(options, Config())
        assert not sk.enabled, \
               "Deprecated was not disabled by noDeprecated option"
        

if __name__ == '__main__':
    unittest.main()