I wrote an application - a simple game where the user writes his question and gets one of the answers that randomly get from the list. Now we need to make a test that would check the function of random answers: we compare her answer and make a conclusion about whether it is empty or not (it is important to check: the answer is not empty).
I have not encountered testing, even with a simple version of it. What's wrong with me?
import random
import tkinter as tk
RESPONSES = ['Yes', 'No']
class QuestionsAnswers(tk.Frame):
def __init__(self, main):
tk.Frame.__init__(self, main)
self.main = main
# let's create a frame where there will be a field for user questions
self.frame_question_answer = tk.Frame(master=self.main)
# let's create a frame that forms an area with answers to the user's questions
self.frame_answer = tk.Frame(master=self.main)
# let's add a Text widget - an area for writing questions
self.text_box_question = tk.Text(self.frame_question_answer)
# widget for displaying random answers
self.text_box_answer = tk.Text(master=self.frame_answer)
# let's create an element of the Ask button
self.button_ask = tk.Button(master=self.frame_question_answer,
text='Ask')
# creating a Clear button element
self.button_clear = tk.Button(master=self.frame_question_answer,
text='Clear')
# bind all widgets to the grid
self.frame_question_answer.grid(row=0, column=0, columnspan=1, sticky='e')
self.frame_answer.grid(row=0, column=1, columnspan = 1,sticky='w')
self.text_box_question.grid(row=0, column=0, sticky='news')
self.text_box_answer.grid(row=0, column=0, sticky='news')
self.button_ask.grid(row=3, column=0, sticky='ew')
self.button_clear.grid(row=3, column=1, sticky='ew')
# binding the event processing to the button
self.button_ask.bind('<Button>', self.questionQuery)
# we bind the command to the button for clearing the question and answer fields
self.button_clear.bind('<Button>', self.clearText)
# The handler of the user's question
def questionQuery(self, event):
question = self.text_box_question.get('1.0', tk.END) # reading the text from the widget
self.main.update_idletasks()
self.main.after(2000, lambda: self.button_ask.configure(text='Ask'))
if not question.strip():
self.text_box_answer.insert(tk.END, 'Ask your question\n')
else:
self.answerQuery()
# Random response return function
def answerQuery(self):
time.sleep(2)
self.text_box_answer.insert(tk.END, 'Hmm...' '\n') # output to the widget
self.main.update_idletasks()
time.sleep(2)
self.text_box_answer.insert(tk.END, 'I look beyond the twists of fate' '\n')
self.main.update_idletasks()
self.main.after(2000, lambda: self.text_box_answer.insert(tk.END, random.choice(RESPONSES) '\n'))
# the function of clearing the question and answer fields
def clearText(self, event):
time.sleep(1)
self.text_box_question.delete('1.0','end')
time.sleep(1)
self.text_box_answer.delete('1.0', tk.END)
if __name__ == '__main__':
window = tk.Tk()
quest_answer = QuestionsAnswers(window)
window.update()
window.mainloop()
Next, I write the test code:
import unittest
class TKinterTestCase(unittest.TestCase):
def setUp(self):
self.questionsAnswers = QuestionsAnswers()
def test_answerQuery(self):
self.assertEqual(self.questionsAnswers.answerQuery(), len(self.questionsAnswers.answerQuery())!=0)
if __name__ == '__main__':
unittest.main()
The test was passed, but it gave an error, although it should not be. Why is that?
CodePudding user response:
consider this line of your test code:
self.assertEqual(self.questionsAnswers.answerQuery(), len(self.questionsAnswers.answerQuery())!=0)
You are asserting on what answerQuery
returns. Since the function has no return
statement, it's returning None
. Your assertion is functionally identical to assertEqual(None, len(None)!= 0)
.
Instead, you need to be asserting on the side effects of the function. Since the function is ultimately adding text to a text widget, you need to assert on the contents of the text widget. It might look something like this:
actual = self.text_box_answer.get("1.0", "end-1c")
self.assertEqual(actual, "whatever you expect it to be")
Since the title of the question is how to test that the response is not empty, you can just assert on the length of the result:
self.assert(len(actual) > 0)
... or the more pythonic:
self.assert(actual)