如何在Tkinter应用上运行unittest?

35 投票
4 回答
20283 浏览
提问于 2025-04-16 06:28

我刚开始学习测试驱动开发(TDD),并且正在用Tkinter做一个图形界面的程序。唯一的问题是,一旦调用了.mainloop()这个方法,测试程序就会卡住,直到窗口被关闭。

这是我的代码示例:

# server.py
import Tkinter as tk

class Server(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.mainloop()

# test.py
import unittest
import server

class ServerTestCase(unittest.TestCase):
    def testClassSetup(self):
       server.Server()
       # and of course I can't call any server.whatever functions here

if __name__ == '__main__':
    unittest.main()

那么,测试Tkinter应用程序的合适方法是什么呢?还是说根本就不应该测试?

4 个回答

4

这个回答适用于 Python 3.7 及以上版本(这些版本支持异步方法)

在你的 main.py 文件中,或者你启动主界面的地方:

def start_application() -> Application:
    root = tk.Tk()
    app = Application(master=root)
    app.load_settings()
    return app # will return the application without starting the main loop.

if __name__=='__main__':
    start_application().mainloop()

然后在你的 tests.py 文件中:

from myapp.main import start_application

class TestGui(unittest.TestCase):
    
    # this will run on a separate thread.
    async def _start_app(self):
        self.app.mainloop()
    
    def setUp(self):
        self.app = start_application()
        self._start_app()
    
    def tearDown(self):
        self.app.destroy()
    
    def test_startup(self):
        title = self.app.winfo_toplevel().title()
        expected = 'The Application My Boss Wants Me To Make'
        self.assertEqual(title, expected)

这个测试不会显示任何结果,但它会通过测试。此外,你会看到一个警告,提示我们没有等待 _start_application 的执行。在这个情况下,这个警告可以忽略。(如果你想严格遵循多线程的规则,那你就得自己管理线程……我觉得这对于单元测试来说太麻烦了)。

14

总结:在一个会引发用户界面事件的操作之后,执行下面的代码,然后再进行一个需要这个事件效果的后续操作。


IPython 提供了一个优雅的解决方案,它的 gui tk 魔法命令实现不需要使用线程,具体代码可以在 terminal/pt_inputhooks/tk.py 找到。

它不是使用 root.mainloop(),而是循环运行 root.dooneevent(),每次循环都检查是否有退出条件(比如是否有交互输入到达)。这样,当 IPython 正在处理命令时,事件循环就不会运行。

在测试中,没有外部事件需要等待,而测试总是处于“忙碌”状态,所以需要手动(或半自动)在“合适的时刻”运行这个循环。那么,什么是“合适的时刻”呢?

测试表明,如果没有事件循环,可以直接改变控件(使用 <widget>.tk.call() 以及任何包装它的东西),但事件处理程序却不会被触发。因此,每当发生事件并且我们需要它的效果时,就需要运行这个循环——也就是说,在任何改变某些东西的操作之后,在需要这个改变结果的操作之前。

根据前面提到的 IPython 过程,代码如下:

def pump_events(root):
    while root.dooneevent(_tkinter.ALL_EVENTS|_tkinter.DONT_WAIT):
        pass

这段代码会处理(执行处理程序)所有待处理的事件,以及所有直接由这些事件引起的事件。

(tkinter.Tk.dooneevent() 委托给 Tcl_DoOneEvent().)


顺便提一下,使用这个替代:

root.update()
root.update_idletasks()

不一定会产生相同的效果,因为这两个函数并不会处理 所有 类型的事件。由于每个处理程序可能会生成其他任意事件,所以这样做我无法确保已经处理了所有事件。


下面是一个示例,用于测试一个简单的弹出对话框来编辑字符串值:

class TKinterTestCase(unittest.TestCase):
    """These methods are going to be the same for every GUI test,
    so refactored them into a separate class
    """
    def setUp(self):
        self.root=tkinter.Tk()
        self.pump_events()

    def tearDown(self):
        if self.root:
            self.root.destroy()
            self.pump_events()

    def pump_events(self):
        while self.root.dooneevent(_tkinter.ALL_EVENTS | _tkinter.DONT_WAIT):
            pass

class TestViewAskText(TKinterTestCase):
    def test_enter(self):
        v = View_AskText(self.root,value=u"йцу")
        self.pump_events()
        v.e.focus_set()
        v.e.insert(tkinter.END,u'кен')
        v.e.event_generate('<Return>')
        self.pump_events()

        self.assertRaises(tkinter.TclError, lambda: v.top.winfo_viewable())
        self.assertEqual(v.value,u'йцукен')


# ###########################################################
# The class being tested (normally, it's in a separate module
# and imported at the start of the test's file)
# ###########################################################

class View_AskText(object):
    def __init__(self, master, value=u""):
        self.value=None

        top = self.top = tkinter.Toplevel(master)
        top.grab_set()
        self.l = ttk.Label(top, text=u"Value:")
        self.l.pack()
        self.e = ttk.Entry(top)
        self.e.pack()
        self.b = ttk.Button(top, text='Ok', command=self.save)
        self.b.pack()

        if value: self.e.insert(0,value)
        self.e.focus_set()
        top.bind('<Return>', self.save)

    def save(self, *_):
        self.value = self.e.get()
        self.top.destroy()


if __name__ == '__main__':
    import unittest
    unittest.main()
2

你可以做的一件事是把主循环放到一个单独的线程里,然后用你的主线程来运行实际的测试;就像在监视主循环线程一样。在进行断言之前,确保检查一下Tk窗口的状态。

多线程编程是很复杂的。你可能想把你的Tk程序拆分成可以测试的小部分,而不是一次性对整个程序进行单元测试(那样其实并不算真正的单元测试)。

最后,我建议至少在控制层面进行测试,如果能更深入一些就更好了,这对你的程序会有很大帮助。

撰写回答