test-bots: Add option to use pytest for running tests.
* Add pytest to requirements.txt * Add pass-through option to run pytest in verbose mode * Use various default pytest options * Exclude merels bot for now
This commit is contained in:
parent
69fab99ba7
commit
d73fa0f50f
|
@ -3,6 +3,7 @@ twine
|
||||||
coverage>=4.4.1
|
coverage>=4.4.1
|
||||||
pycodestyle==2.3.1
|
pycodestyle==2.3.1
|
||||||
mock
|
mock
|
||||||
|
pytest
|
||||||
-e ./zulip
|
-e ./zulip
|
||||||
-e ./zulip_bots
|
-e ./zulip_bots
|
||||||
-e ./zulip_botserver
|
-e ./zulip_botserver
|
||||||
|
|
|
@ -9,6 +9,7 @@ import argparse
|
||||||
import glob
|
import glob
|
||||||
import unittest
|
import unittest
|
||||||
from unittest import TestCase, TestSuite
|
from unittest import TestCase, TestSuite
|
||||||
|
import pytest
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
description = """
|
description = """
|
||||||
|
@ -50,6 +51,14 @@ the tests for xkcd and wikipedia bots):
|
||||||
default=False,
|
default=False,
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="whether to exit if a bot has tests which won't run due to no __init__.py")
|
help="whether to exit if a bot has tests which won't run due to no __init__.py")
|
||||||
|
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)')
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
@ -80,28 +89,44 @@ def main():
|
||||||
|
|
||||||
bots_to_test = filter(lambda bot: bot not in options.exclude, specified_bots)
|
bots_to_test = filter(lambda bot: bot not in options.exclude, specified_bots)
|
||||||
|
|
||||||
# Codecov seems to work only when using loader.discover. It failed to
|
if options.pytest:
|
||||||
# capture line executions for functions like loader.loadTestFromModule
|
excluded_bots = ['merels']
|
||||||
# or loader.loadTestFromNames.
|
pytest_bots_to_test = sorted([bot for bot in bots_to_test if bot not in excluded_bots])
|
||||||
top_level = "zulip_bots/zulip_bots/bots/"
|
pytest_options = [
|
||||||
loader = unittest.defaultTestLoader
|
'-s', # show output from tests; this hides the progress bar though
|
||||||
test_suites = []
|
'-x', # stop on first test failure
|
||||||
for name in bots_to_test:
|
'--ff', # runs last failure first
|
||||||
try:
|
]
|
||||||
test_suites.append(loader.discover(top_level + name, top_level_dir=top_level))
|
pytest_options += (['-v'] if options.verbose else [])
|
||||||
except ImportError as exception:
|
os.chdir(bots_dir)
|
||||||
print(exception)
|
result = pytest.main(pytest_bots_to_test + pytest_options)
|
||||||
print("This likely indicates that you need a '__init__.py' file in your bot directory.")
|
if result != 0:
|
||||||
if options.error_on_no_init:
|
sys.exit(1)
|
||||||
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.
|
||||||
|
top_level = "zulip_bots/zulip_bots/bots/"
|
||||||
|
loader = unittest.defaultTestLoader
|
||||||
|
test_suites = []
|
||||||
|
for name in bots_to_test:
|
||||||
|
try:
|
||||||
|
test_suites.append(loader.discover(top_level + name, top_level_dir=top_level))
|
||||||
|
except ImportError as exception:
|
||||||
|
print(exception)
|
||||||
|
print("This likely indicates that you need a '__init__.py' file in your bot directory.")
|
||||||
|
if options.error_on_no_init:
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
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)
|
||||||
if result.failures or result.errors:
|
failures = result.failures
|
||||||
sys.exit(1)
|
if failures or result.errors:
|
||||||
|
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