test_handler: Support running various tests using pytest.

This enables use of pytest with:
* the zulip package
* the botserver package
This commit is contained in:
neiljp (Neil Pilgrim) 2018-06-09 11:56:52 -07:00 committed by showell
parent ea370031ee
commit a6a5da2653

View file

@ -4,6 +4,7 @@ import os
import sys
import argparse
import unittest
import pytest
TOOLS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.chdir(os.path.dirname(TOOLS_DIR))
@ -15,6 +16,14 @@ def handle_input_and_run_tests_for_package(package_name):
const=True,
default=False,
help='compute test coverage (--coverage combine to combine with previous reports)')
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)')
options = parser.parse_args()
if options.coverage:
@ -24,16 +33,32 @@ def handle_input_and_run_tests_for_package(package_name):
cov.load()
cov.start()
if options.pytest:
location_to_run_in = os.path.join(TOOLS_DIR, '..', package_name)
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.
test_suites = unittest.defaultTestLoader.discover(package_name)
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)
if not result.failures and options.coverage:
if not failures and options.coverage:
cov.stop()
cov.data_suffix = False # Disable suffix so that filename is .coverage
cov.save()