From 9c37f92a010d404428e515b0409decda0df46ef3 Mon Sep 17 00:00:00 2001 From: derAnfaenger Date: Tue, 14 Nov 2017 11:25:24 +0100 Subject: [PATCH] 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. --- tools/test-bots | 14 ++++++++++++++ .../bots/github_detail/test_github_detail.py | 15 +++++++++++---- zulip_bots/zulip_bots/test_lib.py | 10 +++++++++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/tools/test-bots b/tools/test-bots index 0ef8429..c298bdb 100755 --- a/tools/test-bots +++ b/tools/test-bots @@ -8,6 +8,7 @@ import sys import argparse import glob import unittest +from unittest import TestCase, TestSuite def load_tests_from_modules(names, template): loader = unittest.defaultTestLoader @@ -91,6 +92,19 @@ def main(): else: 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) runner = unittest.TextTestRunner(verbosity=2) result = runner.run(suite) diff --git a/zulip_bots/zulip_bots/bots/github_detail/test_github_detail.py b/zulip_bots/zulip_bots/bots/github_detail/test_github_detail.py index b562138..29a28b2 100755 --- a/zulip_bots/zulip_bots/bots/github_detail/test_github_detail.py +++ b/zulip_bots/zulip_bots/bots/github_detail/test_github_detail.py @@ -5,10 +5,18 @@ from __future__ import print_function 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" + 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): bot_response = '**[zulip/zulip#5365](https://github.com/zulip/zulip/issues/5365)'\ @@ -75,8 +83,7 @@ class TestGithubDetailBot(BotTestCase): 'the default repo is zulip.' # This message calls the `send_reply` function of BotHandlerApi - mock_config = {'owner': 'zulip', 'repo': 'zulip'} - with self.mock_config_info(mock_config): + with self.mock_config_info(self.mock_config): self.initialize_bot() self.assert_bot_response( message = {'content': 'help'}, diff --git a/zulip_bots/zulip_bots/test_lib.py b/zulip_bots/zulip_bots/test_lib.py index f2a37f4..48dd05d 100755 --- a/zulip_bots/zulip_bots/test_lib.py +++ b/zulip_bots/zulip_bots/test_lib.py @@ -26,7 +26,8 @@ from types import ModuleType from copy import deepcopy -class BotTestCase(TestCase): +class BotTestCaseBase(TestCase): + """Test class for common Bot test helper methods""" bot_name = '' # type: str def get_bot_message_handler(self): @@ -153,3 +154,10 @@ class BotTestCase(TestCase): # Strictly speaking, this function is not needed anymore, # kept for now for legacy reasons. 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(), '')