tests: Add --coverage option to print a report

This commit is contained in:
Cole Robinson 2013-07-02 17:37:45 -04:00
parent 88f2d1abe0
commit 351ff1ae57

View File

@ -356,11 +356,14 @@ class configure(Command):
class TestBaseCommand(Command): class TestBaseCommand(Command):
user_options = [('debug', 'd', 'Show debug output')] user_options = [
boolean_options = ['debug'] ('debug', 'd', 'Show debug output'),
('coverage', 'c', 'Show coverage report')
]
def initialize_options(self): def initialize_options(self):
self.debug = 0 self.debug = 0
self.coverage = 0
self._testfiles = [] self._testfiles = []
self._dir = os.getcwd() self._dir = os.getcwd()
@ -370,19 +373,24 @@ class TestBaseCommand(Command):
def run(self): def run(self):
try: try:
# Use system 'coverage' if available
import coverage import coverage
use_coverage = True use_cov = True
except: except:
use_coverage = False use_cov = False
if use_cov:
omit = ["/usr/*", "/*/tests/*"]
cov = coverage.coverage(omit=omit)
cov.erase()
cov.start()
import tests as testsmodule
testsmodule.cov = cov
tests = unittest.TestLoader().loadTestsFromNames(self._testfiles) tests = unittest.TestLoader().loadTestsFromNames(self._testfiles)
t = unittest.TextTestRunner(verbosity=1) t = unittest.TextTestRunner(verbosity=1)
if use_coverage:
coverage.erase()
coverage.start()
if hasattr(unittest, "installHandler"): if hasattr(unittest, "installHandler"):
try: try:
unittest.installHandler() unittest.installHandler()
@ -394,11 +402,16 @@ class TestBaseCommand(Command):
except KeyboardInterrupt: except KeyboardInterrupt:
sys.exit(1) sys.exit(1)
if use_coverage: if use_cov:
coverage.stop() cov.stop()
cov.save()
err = int(bool(len(result.failures) > 0 or
len(result.errors) > 0))
if not err and use_cov and self.coverage:
cov.report(show_missing=False)
sys.exit(err)
sys.exit(int(bool(len(result.failures) > 0 or
len(result.errors) > 0)))
class TestCommand(TestBaseCommand): class TestCommand(TestBaseCommand):