我寫了一個應用程式 - 一個簡單的游戲,用戶在其中寫下他的問題并從串列中隨機獲得一個答案。現在我們需要做一個測驗來檢查隨機答案的功能:我們比較她的答案并得出是否為空的結論(檢查很重要:答案不為空)。
我還沒有遇到過測驗,即使是它的簡單版本。我怎么了?
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()
接下來,我撰寫測驗代碼:
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()
測驗通過了,但它給出了錯誤,雖然它不應該是。這是為什么?
uj5u.com熱心網友回復:
考慮這行測驗代碼:
self.assertEqual(self.questionsAnswers.answerQuery(), len(self.questionsAnswers.answerQuery())!=0)
您正在斷言answerQuery回傳的內容。由于該函式沒有return陳述句,它回傳None. 您的斷言在功能上與assertEqual(None, len(None)!= 0).
相反,您需要對函式的副作用進行斷言。由于該函式最終是向文本小部件添加文本,因此您需要對文本小部件的內容進行斷言。它可能看起來像這樣:
actual = self.text_box_answer.get("1.0", "end-1c")
self.assertEqual(actual, "whatever you expect it to be")
由于問題的標題是如何測驗回應不為空,因此您可以僅對結果的長度進行斷言:
self.assert(len(actual) > 0)
......或者更多的pythonic:
self.assert(actual)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/345029.html
標籤:特金特
