summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDomen Kožar <domen@dev.si>2012-08-13 19:26:18 +0200
committerDomen Kožar <domen@dev.si>2012-08-13 19:26:18 +0200
commit9c393d56633ff2d274e95b1eb23b1dbc53ba67cf (patch)
treeddcdca821a30c13189f453f2e14d48fa85a19687
parenta6894e0d23afc3afeb921620a2b61ebf6bbea5c8 (diff)
downloadnose-9c393d56633ff2d274e95b1eb23b1dbc53ba67cf.tar.gz
add --cover-min-percentage support to cover plugin.
-rw-r--r--functional_tests/test_coverage_plugin.py21
-rw-r--r--nose/plugins/cover.py25
2 files changed, 45 insertions, 1 deletions
diff --git a/functional_tests/test_coverage_plugin.py b/functional_tests/test_coverage_plugin.py
index d1b2632..d6fb8a6 100644
--- a/functional_tests/test_coverage_plugin.py
+++ b/functional_tests/test_coverage_plugin.py
@@ -25,7 +25,7 @@ class TestCoveragePlugin(PluginTester, unittest.TestCase):
super(TestCoveragePlugin, self).setUp()
def runTest(self):
- self.assertTrue("blah 4 1 75% 6" in self.output)
+ 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,
@@ -33,5 +33,24 @@ class TestCoveragePlugin(PluginTester, unittest.TestCase):
# 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):
+ 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
+
if __name__ == '__main__':
unittest.main()
diff --git a/nose/plugins/cover.py b/nose/plugins/cover.py
index f91e12a..d851430 100644
--- a/nose/plugins/cover.py
+++ b/nose/plugins/cover.py
@@ -13,6 +13,7 @@ variable.
import logging
import re
import sys
+import StringIO
from nose.plugins.base import Plugin
from nose.util import src, tolist
@@ -27,6 +28,7 @@ class Coverage(Plugin):
coverPackages = None
coverInstance = None
coverErase = False
+ coverMinPercentage = None
score = 200
status = {}
@@ -51,6 +53,11 @@ class Coverage(Plugin):
default=env.get('NOSE_COVER_TESTS'),
help="Include test modules in coverage report "
"[NOSE_COVER_TESTS]")
+ parser.add_option("--cover-min-percentage", action="store",
+ dest="cover_min_percentage",
+ default=env.get('NOSE_COVER_MIN_PERCENTAGE'),
+ help="Minimum percentage of coverage for tests"
+ "to pass [NOSE_COVER_MIN_PERCENTAGE]")
parser.add_option("--cover-inclusive", action="store_true",
dest="cover_inclusive",
default=env.get('NOSE_COVER_INCLUSIVE'),
@@ -119,6 +126,8 @@ class Coverage(Plugin):
log.debug('Will put HTML coverage report in %s', self.coverHtmlDir)
self.coverBranches = options.cover_branches
self.coverXmlFile = None
+ if options.cover_min_percentage:
+ self.coverMinPercentage = int(options.cover_min_percentage.rstrip('%'))
if options.cover_xml:
self.coverXmlFile = options.cover_xml_file
log.debug('Will put XML coverage report in %s', self.coverXmlFile)
@@ -161,6 +170,22 @@ class Coverage(Plugin):
log.debug("Generating XML coverage report")
self.coverInstance.xml_report(modules, self.coverXmlFile)
+ # make sure we have minimum required coverage
+ if self.coverMinPercentage:
+ f = StringIO.StringIO()
+ self.coverInstance.report(modules, file=f)
+ m = re.search(r'\w+\s+\d+\s+\d+\s+(\d+)%', f.getvalue())
+ if m:
+ percentage = int(m.groups()[0])
+ if percentage < self.coverMinPercentage:
+ log.error('TOTAL Coverage did not reach minimum '
+ 'required: %d%%' % self.coverMinPercentage)
+ sys.exit(1)
+ else:
+ log.error("No total percentage was found in coverage output, "
+ "something went wrong.")
+
+
def wantModuleCoverage(self, name, module):
if not hasattr(module, '__file__'):
log.debug("no coverage of %s: no __file__", name)