Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ตัวอย่างการ Functional Testing แอปพลิเคชัน Python Tkinter

สมมติว่าเรามีแอปพลิเคชัน Python Tkinter แบบ GUI ที่รับ Text Input จากผู้ใช้และ Validate โดยบันทึกใน Text File ใหม่ ไฟล์จะมี Text Input เดียวกับที่ผู้ใช้พิมพ์ เราสามารถตรวจสอบและ Validate User Input จากไฟล์ได้

ใน Functional Testing เราสนใจหลักๆ ที่ Backend API, Database, User-Server Communication, Input และ Output เป็นต้น

เพื่อตรวจสอบแอปพลิเคชันด้วยกลยุทธ์ Functional Testing เราต้องเข้าใจ User Requirement และ Input/Output ก่อน หลังทดสอบ Pre-phase เราจะทดสอบแอปพลิเคชันสำหรับ Test Cases ต่างๆ

ตัวอย่างเช่น เรามีแอปพลิเคชัน Tkinter แบบ GUI ที่รับ User Input และบันทึกเป็น Text File ในระบบ

ตัวอย่าง

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()

ผลลัพธ์

การรันโค้ดข้างต้นจะสร้างหน้าต่างแบบนี้:

ตัวอย่างการ Functional Testing แอปพลิเคชัน Python Tkinter

เมื่อเราคลิกปุ่ม Save the File จะบันทึกชื่อไฟล์เป็น "Tutorials.txt"

ตอนนี้ ไปที่ตำแหน่งไฟล์และเปิด Text File ภายนอก จะมีข้อความเหมือนกับ User Input

ตัวอย่างการ Functional Testing แอปพลิเคชัน Python Tkinter