summaryrefslogtreecommitdiff
path: root/functional_tests/test_coverage_plugin.py
blob: 23dd8c375ee51856c7c705467fad99a2d1b91f71 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""Test the coverage plugin."""
import os
import sys
import unittest
import shutil

from nose.exc import SkipTest
from nose.plugins import PluginTester
from nose.plugins.cover import Coverage

support = os.path.join(os.path.dirname(__file__), 'support')

try:
    import coverage

    # Python 3.3 may accidentally pick up our support area when running the unit
    # tests.  Look for the coverage attribute to make sure we've got the right
    # package.
    hasCoverage = hasattr(coverage, 'coverage')
except ImportError:
    hasCoverage = False


class TestCoveragePlugin(PluginTester, unittest.TestCase):
    activate = "--with-coverage"
    args = ['-v', '--cover-package=blah', '--cover-html', '--cover-min-percentage', '25']
    plugins = [Coverage()]
    suitepath = os.path.join(support, 'coverage')

    def setUp(self):
        if not hasCoverage:
            raise SkipTest('coverage not available; skipping')

        self.cover_file = os.path.join(os.getcwd(), '.coverage')
        self.cover_html_dir = os.path.join(os.getcwd(), 'cover')
        if os.path.exists(self.cover_file):
            os.unlink(self.cover_file)
        if os.path.exists(self.cover_html_dir):
            shutil.rmtree(self.cover_html_dir)
        super(TestCoveragePlugin, self).setUp()

    def runTest(self):
        self.assertTrue("blah        4      3    25%   1" in self.output)
        self.assertTrue("Ran 1 test in" in self.output)
        # Assert coverage html report exists
        self.assertTrue(os.path.exists(os.path.join(self.cover_html_dir,
                        'index.html')))
        # Assert coverage data is saved
        self.assertTrue(os.path.exists(self.cover_file))


class TestCoverageMinPercentagePlugin(PluginTester, unittest.TestCase):
    activate = "--with-coverage"
    args = ['-v', '--cover-package=blah', '--cover-min-percentage', '100']
    plugins = [Coverage()]
    suitepath = os.path.join(support, 'coverage')

    def setUp(self):
        if not hasCoverage:
            raise SkipTest('coverage not available; skipping')

        self.cover_file = os.path.join(os.getcwd(), '.coverage')
        self.cover_html_dir = os.path.join(os.getcwd(), 'cover')
        if os.path.exists(self.cover_file):
            os.unlink(self.cover_file)
        if os.path.exists(self.cover_html_dir):
            shutil.rmtree(self.cover_html_dir)
        self.assertRaises(SystemExit,
                          super(TestCoverageMinPercentagePlugin, self).setUp)

    def runTest(self):
        pass


class TestCoverageMinPercentageSinglePackagePlugin(
        PluginTester, unittest.TestCase):
    activate = "--with-coverage"
    args = ['-v', '--cover-package=blah', '--cover-html',
            '--cover-min-percentage', '100']
    plugins = [Coverage()]
    suitepath = os.path.join(support, 'coverage')

    def setUp(self):
        if not hasCoverage:
            raise SkipTest('coverage not available; skipping')

        self.cover_file = os.path.join(os.getcwd(), '.coverage')
        self.cover_html_dir = os.path.join(os.getcwd(), 'cover')
        if os.path.exists(self.cover_file):
            os.unlink(self.cover_file)
        if os.path.exists(self.cover_html_dir):
            shutil.rmtree(self.cover_html_dir)
        self.assertRaises(SystemExit,
                          super(TestCoverageMinPercentageSinglePackagePlugin,
                              self).setUp)

    def runTest(self):
        pass


class TestCoverageMinPercentageSinglePackageWithBranchesPlugin(
        PluginTester, unittest.TestCase):
    activate = "--with-coverage"
    args = ['-v', '--cover-package=blah', '--cover-branches',
            '--cover-html', '--cover-min-percentage', '100']
    plugins = [Coverage()]
    suitepath = os.path.join(support, 'coverage')

    def setUp(self):
        if not hasCoverage:
            raise SkipTest('coverage not available; skipping')

        self.cover_file = os.path.join(os.getcwd(), '.coverage')
        self.cover_html_dir = os.path.join(os.getcwd(), 'cover')
        if os.path.exists(self.cover_file):
            os.unlink(self.cover_file)
        if os.path.exists(self.cover_html_dir):
            shutil.rmtree(self.cover_html_dir)
        self.assertRaises(
                SystemExit,
                super(TestCoverageMinPercentageSinglePackageWithBranchesPlugin,
                    self).setUp)

    def runTest(self):
        pass


class TestCoverageMinPercentageTOTALPlugin(PluginTester, unittest.TestCase):
    activate = "--with-coverage"
    args = ['-v', '--cover-package=blah', '--cover-package=moo',
            '--cover-min-percentage', '100']
    plugins = [Coverage()]
    suitepath = os.path.join(support, 'coverage2')

    def setUp(self):
        if not hasCoverage:
            raise SkipTest('coverage not available; skipping')

        self.cover_file = os.path.join(os.getcwd(), '.coverage')
        self.cover_html_dir = os.path.join(os.getcwd(), 'cover')
        if os.path.exists(self.cover_file):
            os.unlink(self.cover_file)
        if os.path.exists(self.cover_html_dir):
            shutil.rmtree(self.cover_html_dir)
        self.assertRaises(SystemExit,
                          super(TestCoverageMinPercentageTOTALPlugin, self).setUp)

    def runTest(self):
        pass


class TestCoverageMinPercentageWithBranchesTOTALPlugin(
        PluginTester, unittest.TestCase):
    activate = "--with-coverage"
    args = ['-v', '--cover-package=blah', '--cover-package=moo',
            '--cover-branches', '--cover-min-percentage', '100']
    plugins = [Coverage()]
    suitepath = os.path.join(support, 'coverage2')

    def setUp(self):
        if not hasCoverage:
            raise SkipTest('coverage not available; skipping')

        self.cover_file = os.path.join(os.getcwd(), '.coverage')
        self.cover_html_dir = os.path.join(os.getcwd(), 'cover')
        if os.path.exists(self.cover_file):
            os.unlink(self.cover_file)
        if os.path.exists(self.cover_html_dir):
            shutil.rmtree(self.cover_html_dir)
        self.assertRaises(
                SystemExit,
                super(TestCoverageMinPercentageWithBranchesTOTALPlugin, self).setUp)

    def runTest(self):
        pass

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