From a6a5da2653b8ff723c8e576bb8e0b973ea2b06b3 Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Sat, 9 Jun 2018 11:56:52 -0700 Subject: [PATCH] test_handler: Support running various tests using pytest. This enables use of pytest with: * the zulip package * the botserver package --- tools/server_lib/test_handler.py | 43 +++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/tools/server_lib/test_handler.py b/tools/server_lib/test_handler.py index ad2c0f0..a4c8f0c 100644 --- a/tools/server_lib/test_handler.py +++ b/tools/server_lib/test_handler.py @@ -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() - # 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) - if result.failures or result.errors: - sys.exit(1) + 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()