test-bots: Simplify and unify test discovery.

This commit is contained in:
neiljp (Neil Pilgrim) 2018-05-25 23:00:51 -07:00
parent 4364dc7599
commit 1d60794f1c

View file

@ -10,21 +10,6 @@ import glob
import unittest import unittest
from unittest import TestCase, TestSuite from unittest import TestCase, TestSuite
def load_tests_from_modules(names, template):
loader = unittest.defaultTestLoader
test_suites = []
for name in names:
module = import_module(template.format(name=name))
test_suites.append(loader.loadTestsFromModule(module))
return test_suites
def load_all_tests():
loader = unittest.defaultTestLoader
# Codecov seems to work only when using loader.discover. It failed to capture line executions
# for functions like loader.loadTestFromModule or loader.loadTestFromNames.
return loader.discover('zulip_bots')
def parse_args(available_bots): def parse_args(available_bots):
description = """ description = """
Script to run test_<bot_name>.py files in the Script to run test_<bot_name>.py files in the
@ -83,14 +68,19 @@ def main():
cov.load() cov.load()
cov.start() cov.start()
loader = unittest.defaultTestLoader
top_level = "zulip_bots/zulip_bots/bots/"
if options.bots_to_test: if options.bots_to_test:
bots_to_test = filter(lambda bot: bot not in options.exclude, bots_to_test = filter(lambda bot: bot not in options.exclude,
options.bots_to_test) options.bots_to_test)
test_suites = load_tests_from_modules( # Should add a check here that __init__.py is absent if test_*.py is present?
bots_to_test, test_suites = [loader.discover(top_level + name, top_level_dir=top_level)
template='zulip_bots.bots.{name}.test_{name}') for name in bots_to_test]
else: else:
test_suites = load_all_tests() # 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 = loader.discover(top_level)
def filter_tests(tests): def filter_tests(tests):
# type: (Union[TestSuite, TestCase]) -> TestSuite # type: (Union[TestSuite, TestCase]) -> TestSuite