bots: Simplify automated testing library.
This commit is contained in:
parent
a85c7a0705
commit
971fabe8ad
|
@ -5,36 +5,25 @@ from __future__ import print_function
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
|
||||||
from unittest import TestCase
|
|
||||||
|
|
||||||
our_dir = os.path.dirname(os.path.abspath(__file__))
|
our_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
# For dev setups, we can find the API in the repo itself.
|
# For dev setups, we can find the API in the repo itself.
|
||||||
if os.path.exists(os.path.join(our_dir, '..')):
|
if os.path.exists(os.path.join(our_dir, '..')):
|
||||||
sys.path.insert(0, '..')
|
sys.path.insert(0, '..')
|
||||||
from bots_test_lib import BotTestCase
|
from bots_test_lib import BotTestCase
|
||||||
|
|
||||||
class TestDefineBot(TestCase):
|
class TestDefineBot(BotTestCase):
|
||||||
def setUp(self):
|
bot_name = "define"
|
||||||
# Messages to be sent to bot for testing.
|
|
||||||
self.request_messages = [
|
|
||||||
{'content': "foo", 'type': "private", 'sender_email': "foo"},
|
|
||||||
{'content': "cat", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
|
||||||
]
|
|
||||||
# Reply messages from the test bot.
|
|
||||||
self.bot_response_messages = [
|
|
||||||
"**foo**:\nDefinition not available.",
|
|
||||||
("**cat**:\n\n* (**noun**) a small domesticated carnivorous mammal "
|
|
||||||
"with soft fur, a short snout, and retractile claws. It is widely "
|
|
||||||
"kept as a pet or for catching mice, and many breeds have been "
|
|
||||||
"developed.\n their pet cat\n\n"),
|
|
||||||
]
|
|
||||||
|
|
||||||
def runTest(self):
|
def test_bot(self):
|
||||||
# type: None -> None
|
self.assert_bot_output(
|
||||||
# Edit bot_module to test different bots, the below code can be looped for all the bots.
|
{'content': "foo", 'type': "private", 'sender_email': "foo"},
|
||||||
bot_module = os.path.join(our_dir, "define.py")
|
"**foo**:\nDefinition not available."
|
||||||
test_case = BotTestCase()
|
)
|
||||||
test_case.bot_test(messages=self.request_messages, bot_module=bot_module,
|
self.assert_bot_output(
|
||||||
bot_response=self.bot_response_messages)
|
{'content': "cat", 'type': "stream", 'display_recipient': "foo", 'subject': "foo"},
|
||||||
|
("**cat**:\n\n* (**noun**) a small domesticated carnivorous mammal "
|
||||||
|
"with soft fur, a short snout, and retractile claws. It is widely "
|
||||||
|
"kept as a pet or for catching mice, and many breeds have been "
|
||||||
|
"developed.\n their pet cat\n\n"),
|
||||||
|
)
|
||||||
|
|
|
@ -14,7 +14,19 @@ from bot_lib import StateHandler
|
||||||
from contrib_bots import bot_lib
|
from contrib_bots import bot_lib
|
||||||
from six.moves import zip
|
from six.moves import zip
|
||||||
|
|
||||||
class BotTestCase():
|
from unittest import TestCase
|
||||||
|
|
||||||
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
class BotTestCase(TestCase):
|
||||||
|
bot_name = None
|
||||||
|
|
||||||
|
def assert_bot_output(self, request, response):
|
||||||
|
# type: (str, str) -> None
|
||||||
|
bot_module = os.path.join(current_dir, "bots",
|
||||||
|
self.bot_name, self.bot_name + ".py")
|
||||||
|
self.bot_test(messages=[request], bot_module=bot_module,
|
||||||
|
bot_response=[response])
|
||||||
|
|
||||||
def mock_test(self, messages, message_handler, bot_response):
|
def mock_test(self, messages, message_handler, bot_response):
|
||||||
# type: (List[Dict[str, str]], Function) -> None
|
# type: (List[Dict[str, str]], Function) -> None
|
||||||
|
@ -37,6 +49,5 @@ class BotTestCase():
|
||||||
return message_handler
|
return message_handler
|
||||||
|
|
||||||
def bot_test(self, messages, bot_module, bot_response):
|
def bot_test(self, messages, bot_module, bot_response):
|
||||||
|
|
||||||
message_handler = self.bot_to_run(bot_module)
|
message_handler = self.bot_to_run(bot_module)
|
||||||
self.mock_test(messages=messages, message_handler=message_handler, bot_response=bot_response)
|
self.mock_test(messages=messages, message_handler=message_handler, bot_response=bot_response)
|
||||||
|
|
Loading…
Reference in a new issue