Imported Upstream version 1.2.1
[platform/upstream/python-nose.git] / functional_tests / test_coverage_plugin.py
1 """Test the coverage plugin."""
2 import os
3 import unittest
4 import shutil
5
6 from nose.plugins import PluginTester
7 from nose.plugins.cover import Coverage
8
9 support = os.path.join(os.path.dirname(__file__), 'support')
10
11
12 class TestCoveragePlugin(PluginTester, unittest.TestCase):
13     activate = "--with-coverage"
14     args = ['-v', '--cover-package=blah', '--cover-html', '--cover-min-percentage', '25']
15     plugins = [Coverage()]
16     suitepath = os.path.join(support, 'coverage')
17
18     def setUp(self):
19         self.cover_file = os.path.join(os.getcwd(), '.coverage')
20         self.cover_html_dir = os.path.join(os.getcwd(), 'cover')
21         if os.path.exists(self.cover_file):
22             os.unlink(self.cover_file)
23         if os.path.exists(self.cover_html_dir):
24             shutil.rmtree(self.cover_html_dir)
25         super(TestCoveragePlugin, self).setUp()
26
27     def runTest(self):
28         self.assertTrue("blah        4      3    25%   1" in self.output)
29         self.assertTrue("Ran 1 test in""" in self.output)
30         # Assert coverage html report exists
31         self.assertTrue(os.path.exists(os.path.join(self.cover_html_dir,
32                         'index.html')))
33         # Assert coverage data is saved
34         self.assertTrue(os.path.exists(self.cover_file))
35
36
37 class TestCoverageMinPercentagePlugin(PluginTester, unittest.TestCase):
38     activate = "--with-coverage"
39     args = ['-v', '--cover-package=blah', '--cover-min-percentage', '100']
40     plugins = [Coverage()]
41     suitepath = os.path.join(support, 'coverage')
42
43     def setUp(self):
44         self.cover_file = os.path.join(os.getcwd(), '.coverage')
45         self.cover_html_dir = os.path.join(os.getcwd(), 'cover')
46         if os.path.exists(self.cover_file):
47             os.unlink(self.cover_file)
48         if os.path.exists(self.cover_html_dir):
49             shutil.rmtree(self.cover_html_dir)
50         self.assertRaises(SystemExit,
51                           super(TestCoverageMinPercentagePlugin, self).setUp)
52
53     def runTest(self):
54         pass
55
56
57 class TestCoverageMinPercentageTOTALPlugin(PluginTester, unittest.TestCase):
58     activate = "--with-coverage"
59     args = ['-v', '--cover-package=blah', '--cover-package=moo',
60             '--cover-min-percentage', '100']
61     plugins = [Coverage()]
62     suitepath = os.path.join(support, 'coverage2')
63
64     def setUp(self):
65         self.cover_file = os.path.join(os.getcwd(), '.coverage')
66         self.cover_html_dir = os.path.join(os.getcwd(), 'cover')
67         if os.path.exists(self.cover_file):
68             os.unlink(self.cover_file)
69         if os.path.exists(self.cover_html_dir):
70             shutil.rmtree(self.cover_html_dir)
71         self.assertRaises(SystemExit,
72                           super(TestCoverageMinPercentageTOTALPlugin, self).setUp)
73
74     def runTest(self):
75         pass
76
77 if __name__ == '__main__':
78     unittest.main()