zulip_bots: Add common tests.
unittest includes by default all module-level classes that inherit from TestCase and implement at least one method starting with 'test'. Since it doesn't provide a convenient way for excluding TestSuites, we need to manually filter out the unwanted testing of our test base class itself.
This commit is contained in:
parent
94b7c2eaef
commit
9c37f92a01
|
@ -8,6 +8,7 @@ import sys
|
||||||
import argparse
|
import argparse
|
||||||
import glob
|
import glob
|
||||||
import unittest
|
import unittest
|
||||||
|
from unittest import TestCase, TestSuite
|
||||||
|
|
||||||
def load_tests_from_modules(names, template):
|
def load_tests_from_modules(names, template):
|
||||||
loader = unittest.defaultTestLoader
|
loader = unittest.defaultTestLoader
|
||||||
|
@ -91,6 +92,19 @@ def main():
|
||||||
else:
|
else:
|
||||||
test_suites = load_all_tests()
|
test_suites = load_all_tests()
|
||||||
|
|
||||||
|
def filter_tests(tests):
|
||||||
|
# type: (Union[TestSuite, TestCase]) -> TestSuite
|
||||||
|
filtered_tests = TestSuite()
|
||||||
|
for test in tests:
|
||||||
|
if isinstance(test, TestCase):
|
||||||
|
# Exclude test base class from being tested.
|
||||||
|
if test.__class__.__name__ not in ['BotTestCase', 'BotTestCaseBase']:
|
||||||
|
filtered_tests.addTest(test)
|
||||||
|
else:
|
||||||
|
filtered_tests.addTest(filter_tests(test))
|
||||||
|
return filtered_tests
|
||||||
|
test_suites = filter_tests(test_suites)
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
@ -5,10 +5,18 @@ from __future__ import print_function
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from zulip_bots.test_lib import BotTestCase
|
from zulip_bots.test_lib import BotTestCaseBase
|
||||||
|
|
||||||
class TestGithubDetailBot(BotTestCase):
|
class TestGithubDetailBot(BotTestCaseBase):
|
||||||
bot_name = "github_detail"
|
bot_name = "github_detail"
|
||||||
|
mock_config = {'owner': 'zulip', 'repo': 'zulip'}
|
||||||
|
|
||||||
|
# Overrides default test_bot_usage().
|
||||||
|
def test_bot_usage(self):
|
||||||
|
# type: () -> None
|
||||||
|
with self.mock_config_info(self.mock_config):
|
||||||
|
self.initialize_bot()
|
||||||
|
self.assertNotEqual(self.message_handler.usage(), '')
|
||||||
|
|
||||||
def test_issue(self):
|
def test_issue(self):
|
||||||
bot_response = '**[zulip/zulip#5365](https://github.com/zulip/zulip/issues/5365)'\
|
bot_response = '**[zulip/zulip#5365](https://github.com/zulip/zulip/issues/5365)'\
|
||||||
|
@ -75,8 +83,7 @@ class TestGithubDetailBot(BotTestCase):
|
||||||
'the default repo is zulip.'
|
'the default repo is zulip.'
|
||||||
# This message calls the `send_reply` function of BotHandlerApi
|
# This message calls the `send_reply` function of BotHandlerApi
|
||||||
|
|
||||||
mock_config = {'owner': 'zulip', 'repo': 'zulip'}
|
with self.mock_config_info(self.mock_config):
|
||||||
with self.mock_config_info(mock_config):
|
|
||||||
self.initialize_bot()
|
self.initialize_bot()
|
||||||
self.assert_bot_response(
|
self.assert_bot_response(
|
||||||
message = {'content': 'help'},
|
message = {'content': 'help'},
|
||||||
|
|
|
@ -26,7 +26,8 @@ from types import ModuleType
|
||||||
|
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
class BotTestCase(TestCase):
|
class BotTestCaseBase(TestCase):
|
||||||
|
"""Test class for common Bot test helper methods"""
|
||||||
bot_name = '' # type: str
|
bot_name = '' # type: str
|
||||||
|
|
||||||
def get_bot_message_handler(self):
|
def get_bot_message_handler(self):
|
||||||
|
@ -153,3 +154,10 @@ class BotTestCase(TestCase):
|
||||||
# Strictly speaking, this function is not needed anymore,
|
# Strictly speaking, this function is not needed anymore,
|
||||||
# kept for now for legacy reasons.
|
# kept for now for legacy reasons.
|
||||||
self.call_request(message, expected_method, response)
|
self.call_request(message, expected_method, response)
|
||||||
|
|
||||||
|
class BotTestCase(BotTestCaseBase):
|
||||||
|
"""Test class extending BotTestCaseBase to add common default tests
|
||||||
|
that should be run (by default) for all our bots"""
|
||||||
|
def test_bot_usage(self):
|
||||||
|
# type: () -> None
|
||||||
|
self.assertNotEqual(self.message_handler.usage(), '')
|
||||||
|
|
Loading…
Reference in a new issue