-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_chatbot.py
More file actions
30 lines (24 loc) · 1.21 KB
/
Copy pathtest_chatbot.py
File metadata and controls
30 lines (24 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import unittest
from unittest.mock import patch
import io
import sys
from chatbot import simple_chatbot
class TestChatbot(unittest.TestCase):
@patch('builtins.input', side_effect=['hello', 'who are you', 'how are you', 'joke', 'random string', 'bye'])
def test_chatbot_responses(self, mock_input):
captured_output = io.StringIO()
sys.stdout = captured_output
try:
simple_chatbot()
finally:
sys.stdout = sys.__stdout__
output = captured_output.getvalue().strip().split('\n')
# Check some key responses based on the input side effect
self.assertTrue(any("Hi there! How can I help you?" in line for line in output))
self.assertTrue(any("I am Chatterbox, a simple Python Chatbot." in line for line in output))
self.assertTrue(any("I'm just a computer program" in line for line in output))
self.assertTrue(any("Because he couldn't C# (see sharp)!" in line for line in output))
self.assertTrue(any("I'm not sure I understand that." in line for line in output))
self.assertTrue(any("Goodbye! Have a great day!" in line for line in output))
if __name__ == '__main__':
unittest.main()