2017-09-05 10:55:12 -04:00
|
|
|
from importlib import import_module
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import unittest
|
2018-06-09 14:56:52 -04:00
|
|
|
import pytest
|
2017-09-05 10:55:12 -04:00
|
|
|
|
|
|
|
TOOLS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
os.chdir(os.path.dirname(TOOLS_DIR))
|
|
|
|
|
2018-06-09 15:21:54 -04:00
|
|
|
def handle_input_and_run_tests_for_package(package_name, path_list):
|
|
|
|
parser = argparse.ArgumentParser(description="Run tests for {}.".format(package_name))
|
2017-09-05 10:55:12 -04:00
|
|
|
parser.add_argument('--coverage',
|
|
|
|
nargs='?',
|
|
|
|
const=True,
|
|
|
|
default=False,
|
|
|
|
help='compute test coverage (--coverage combine to combine with previous reports)')
|
2018-06-09 14:56:52 -04:00
|
|
|
parser.add_argument('--pytest', '-p',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help="run tests with pytest")
|
|
|
|
parser.add_argument('--verbose', '-v',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='show verbose output (with pytest)')
|
2017-09-05 10:55:12 -04:00
|
|
|
options = parser.parse_args()
|
|
|
|
|
|
|
|
if options.coverage:
|
|
|
|
import coverage
|
|
|
|
cov = coverage.Coverage(config_file="tools/.coveragerc")
|
|
|
|
if options.coverage == 'combine':
|
|
|
|
cov.load()
|
|
|
|
cov.start()
|
|
|
|
|
2018-06-09 14:56:52 -04:00
|
|
|
if options.pytest:
|
2018-06-09 15:21:54 -04:00
|
|
|
location_to_run_in = os.path.join(TOOLS_DIR, '..', *path_list)
|
2018-06-09 14:56:52 -04:00
|
|
|
paths_to_test = ['.']
|
|
|
|
pytest_options = [
|
|
|
|
'-s', # show output from tests; this hides the progress bar though
|
|
|
|
'-x', # stop on first test failure
|
|
|
|
'--ff', # runs last failure first
|
|
|
|
]
|
|
|
|
pytest_options += (['-v'] if options.verbose else [])
|
|
|
|
os.chdir(location_to_run_in)
|
|
|
|
result = pytest.main(paths_to_test + pytest_options)
|
|
|
|
if result != 0:
|
|
|
|
sys.exit(1)
|
|
|
|
failures = False
|
|
|
|
else:
|
|
|
|
# Codecov seems to work only when using loader.discover. It failed to capture line executions
|
|
|
|
# for functions like loader.loadTestFromModule or loader.loadTestFromNames.
|
2018-06-09 15:21:54 -04:00
|
|
|
test_suites = unittest.defaultTestLoader.discover(os.path.join(*path_list))
|
2018-06-09 14:56:52 -04:00
|
|
|
suite = unittest.TestSuite(test_suites)
|
|
|
|
runner = unittest.TextTestRunner(verbosity=2)
|
|
|
|
result = runner.run(suite)
|
|
|
|
failures = result.failures
|
|
|
|
if result.failures or result.errors:
|
|
|
|
sys.exit(1)
|
2017-09-05 10:55:12 -04:00
|
|
|
|
2018-06-09 14:56:52 -04:00
|
|
|
if not failures and options.coverage:
|
2017-09-05 10:55:12 -04:00
|
|
|
cov.stop()
|
|
|
|
cov.data_suffix = False # Disable suffix so that filename is .coverage
|
|
|
|
cov.save()
|
|
|
|
cov.html_report()
|
|
|
|
print("HTML report saved in directory 'htmlcov'.")
|