What's the recommended way to unittest Python GUI applications?

前端 未结 4 527
情歌与酒
情歌与酒 2021-02-02 07:01

I\'m currently foolish enough to try to maintaintain two parallel code bases for a Python desktop application, one using PyGObject introspection for GTK 3 and one using PyGTK fo

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-02 07:17

    There is a great way to test PyGTK functions and widgets directly, without going through acceptance/functional/integration testing frameworks that clikety their way into oblivion. I learned about this in this post which is fairly self explanatory. But the basic idea is that you treat your widgets as functions/classes, and you can test them directly. If you need to process callbacks and so on, there's a neat trick that I will reproduce here:

    import time
    import gtk
    
    # Stolen from Kiwi
    def refresh_gui(delay=0):
      while gtk.events_pending():
          gtk.main_iteration_do(block=False)
      time.sleep(delay)
    

    As mentionned in the blog post, this code is LGPL. Otherwise, when you think about it, as long as you don't show() windows or widgets, you can test them all you want and they should behave as if they were real because, in a way, they are. They are just not displayed.

    Of course, you need to simulate interaction on buttons and interactive widgets yourself by calling clicked() on a button for example. See again Ali Afshar's excellent post about unit testing in PyGTK.

提交回复
热议问题