Python Tkinter 应用的功能测试示例


假设我们有一个基于 GUI 的 Python tkinter 应用程序,它接收用户的文本输入并通过将其保存到新的文本文件中进行验证。该文件包含用户输入的相同文本。我们可以从文件中检查和验证用户输入。

在功能测试中,我们主要关注后端 API、数据库、用户-服务器通信、输入和输出等。

要使用功能测试策略检查应用程序,我们必须首先了解用户需求和输入/输出。在测试预阶段之后,我们针对不同的测试用例测试我们的应用程序。

例如,我们有一个基于 GUI 的 tkinter 应用程序,它接收用户输入并将其以文本文件的形式保存在系统中。

示例

from tkinter import *

win = Tk()

win.geometry("700x600")

# Create title label
title_label = Label(win, text="Enter the File Name")
title_label.pack(anchor='n')

# Create title entry
title_entry = Entry(win, width=35)
title_entry.pack(anchor='nw')

# Create save button and function
def save():
   # Get contents of title entry and text entry
   # Create a file to write these contents in to it
   file_title = title_entry.get()
   file_contents = text_entry.get(0.0, END)
   with open(file_title + ".txt", "w") as file:
      file.write(file_contents)
      print("File successfully created")
      file.close()
   pass
#Create a save button to save the content of the file
save_button = Button(win, text="Save The File", command=save)
save_button.pack()

# Create text entry
text_entry = Text(win, width=40, height=30, border=4, relief=RAISED)
text_entry.pack()

win.mainloop()

输出

运行以上代码将创建一个如下所示的窗口,

一旦我们点击“保存文件”按钮,它将文件名保存为“Tutorials.txt”。

现在,转到文件位置并从外部打开文本文件。它将包含与用户输入相同的文本。

更新于: 2021年3月4日

3K+ 阅读量

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.