Tests: Extend test-lib script to support coverage parameter & add to test-main

This commit is contained in:
neiljp (Neil Pilgrim) 2017-11-29 16:48:11 -08:00 committed by showell
parent 39601d190c
commit 1e5e931421
2 changed files with 25 additions and 8 deletions

View file

@ -1,20 +1,35 @@
#!/usr/bin/env python #!/usr/bin/env python
import coverage
import os import os
import sys import sys
import unittest import unittest
import argparse
from importlib import import_module from importlib import import_module
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--coverage',
nargs='?',
const=True,
default=False,
help='compute test coverage ("--coverage combine" to combine with previous reports)')
return parser.parse_args()
def run_all(): def run_all():
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.abspath(os.path.join(TOOLS_DIR, '..')) ROOT_DIR = os.path.abspath(os.path.join(TOOLS_DIR, '..'))
BOTS_DIR = os.path.join(ROOT_DIR, 'zulib_bots') BOTS_DIR = os.path.join(ROOT_DIR, 'zulib_bots')
sys.path.insert(0, BOTS_DIR) sys.path.insert(0, BOTS_DIR)
cov = coverage.Coverage(config_file="tools/.coveragerc") options = parse_args()
cov.start()
if options.coverage:
import coverage
cov = coverage.Coverage(config_file="tools/.coveragerc")
if options.coverage == 'combine':
cov.load()
cov.start()
module = import_module('zulip_bots.lib_tests') module = import_module('zulip_bots.lib_tests')
suite = unittest.defaultTestLoader.loadTestsFromModule(module) suite = unittest.defaultTestLoader.loadTestsFromModule(module)
@ -25,11 +40,12 @@ def run_all():
if result.failures or result.errors: if result.failures or result.errors:
sys.exit(1) sys.exit(1)
cov.stop() if not result.failures and options.coverage:
cov.data_suffix = False # Disable suffix so that filename is .coverage cov.stop()
cov.save() cov.data_suffix = False # Disable suffix so that filename is .coverage
cov.html_report() cov.save()
print("HTML report saved under directory 'htmlcov'.") cov.html_report()
print("HTML report saved under directory 'htmlcov'.")
if __name__ == '__main__': if __name__ == '__main__':
run_all() run_all()

View file

@ -5,3 +5,4 @@ set -ev
tools/test-bots --coverage tools/test-bots --coverage
tools/test-botserver --coverage combine tools/test-botserver --coverage combine
tools/test-zulip --coverage combine tools/test-zulip --coverage combine
tools/test-lib --coverage combine