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:
parent
ea370031ee
commit
a6a5da2653
|
@ -4,6 +4,7 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
import unittest
|
import unittest
|
||||||
|
import pytest
|
||||||
|
|
||||||
TOOLS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
TOOLS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
os.chdir(os.path.dirname(TOOLS_DIR))
|
os.chdir(os.path.dirname(TOOLS_DIR))
|
||||||
|
@ -15,6 +16,14 @@ def handle_input_and_run_tests_for_package(package_name):
|
||||||
const=True,
|
const=True,
|
||||||
default=False,
|
default=False,
|
||||||
help='compute test coverage (--coverage combine to combine with previous reports)')
|
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()
|
options = parser.parse_args()
|
||||||
|
|
||||||
if options.coverage:
|
if options.coverage:
|
||||||
|
@ -24,16 +33,32 @@ def handle_input_and_run_tests_for_package(package_name):
|
||||||
cov.load()
|
cov.load()
|
||||||
cov.start()
|
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
|
# Codecov seems to work only when using loader.discover. It failed to capture line executions
|
||||||
# for functions like loader.loadTestFromModule or loader.loadTestFromNames.
|
# for functions like loader.loadTestFromModule or loader.loadTestFromNames.
|
||||||
test_suites = unittest.defaultTestLoader.discover(package_name)
|
test_suites = unittest.defaultTestLoader.discover(package_name)
|
||||||
suite = unittest.TestSuite(test_suites)
|
suite = unittest.TestSuite(test_suites)
|
||||||
runner = unittest.TextTestRunner(verbosity=2)
|
runner = unittest.TextTestRunner(verbosity=2)
|
||||||
result = runner.run(suite)
|
result = runner.run(suite)
|
||||||
|
failures = result.failures
|
||||||
if result.failures or result.errors:
|
if result.failures or result.errors:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if not result.failures and options.coverage:
|
if not failures and options.coverage:
|
||||||
cov.stop()
|
cov.stop()
|
||||||
cov.data_suffix = False # Disable suffix so that filename is .coverage
|
cov.data_suffix = False # Disable suffix so that filename is .coverage
|
||||||
cov.save()
|
cov.save()
|
||||||
|
|
Loading…
Reference in a new issue