有没有人知道Python Tkinter应用程序功能测试的一个很好的例子?

Wes*_*ant 5 python user-interface tkinter functional-testing

我发现了一个很棒的网站,讨论使用IronPython对Python GUI应用程序进行功能测试:http://www.voidspace.org.uk/python/articles/testing/但是我想使用Tkinter并且很难在图书馆.

Michael为IronPython展示了这个例子:

class FunctionalTest(TestCase):

    def setUp(self):
        self.mainForm = None
        self._thread = Thread(ThreadStart(self.startMultiDoc))
        self._thread.SetApartmentState(ApartmentState.STA)
        self._thread.Start()
        while self.mainForm is None:
            Thread.CurrentThread.Join(100)

    def invokeOnGUIThread(self, function):
        return self.mainForm.Invoke(CallTarget0(function))
Run Code Online (Sandbox Code Playgroud)

...而且我很难将其转换为如何连接到基于Tkinter的应用程序,该应用程序将具有基本设置:

from tkinter import *
from tkinter import ttk

root = Tk()
ttk.Button(root, text="Hello World").grid()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

...我想你也想在第二个线程中的主根对象上运行一个方法,但是我没有看到与mainForm.Invoke()相同的方法.也许我在想错了.也许以这种方式进行功能测试GUI应用并不常见?

例子真棒!

小智 1

对于基于 Tkinter 的应用程序,方法会有所不同。在基于 Tkinter 的应用程序中,您可以使用 StringVar 或 IntVar 的跟踪方法。像这样:

从 tkinter 导入 * 从 tkinter 导入 ttk

def on_value_change(*args):
    print("Value changed to:", var.get())

root = Tk()
var = StringVar()
var.trace("w", on_value_change) # detect changes in the variable
ttk.Entry(root, textvariable=var).grid()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)