PyGTK is unmaintained and has been archived. It has been superseded by PyGObject. Links below might not work anymore and information below might be incorrect.
Functional/Acceptance Testing Using guitest
Introdution
guitest by Gintautas Miliauskas is a helper library for unit-testing GUI applications written in Python. In this article I will demonstrate how to test a PyGTK application. This article assume you are familiar with unittest module and unit testing.
Installation
The latest version 0.3.1 released on 2005-11-26 is available from here: http://gintas.pov.lt/guitest/guitest-0.3.1.tar.gz . Invoke python setup.py install to install the library into the local python's site-packages directory. Alternatively you may simply copy the guitest subdirectory to your project's main source directory.
Getting Started
Consider this example:
1 import gtk
2
3 class HelloWorld(object):
4
5 def __init__(self):
6 self.window = gtk.Window()
7 self.button = gtk.Button("Hello")
8 self.window.add(self.button)
9 self.window.show_all()
10
11 def main(self):
12 gtk.main()
13
14 if __name__ == '__main__':
15 helloworld = HelloWorld()
16 helloworld.main()
Now think what are the things you have to test. Let's say you want to make sure that button is a child of window. And you want to test the label of button is "Hello".
Just look in to this code, should be very easy to understand.
1 import unittest
2 import gtk
3 from guitest.gtktest import GtkTestCase
4
5 import hello1
6
7
8 class TestHelloWorld(GtkTestCase):
9
10 def test_simple_run(self):
11 helloworld = hello1.HelloWorld()
12
13 def test_button(self):
14 helloworld = hello1.HelloWorld()
15 button = helloworld.window.get_child()
16 assert type(button) == gtk.Button
17
18 def test_button_text(self):
19 helloworld = hello1.HelloWorld()
20 button = helloworld.window.get_child()
21 assert button.get_label() == "Hello"
22
23
24 def test_suite():
25 suite = unittest.TestSuite()
26 suite.addTest(unittest.makeSuite(TestHelloWorld))
27 return suite
28
29
30 if __name__ == '__main__':
31 unittest.main()
The test case class is inheriting from GtkTestCase. test_simple_run is just running the app. Other two test cases are self explanatory, yes! you should be familiar with gtk api, that's all.
When testing gui dialog handlers will be very usefull. We will extend the first example:
1 import gtk
2
3 class HelloWorld(object):
4
5 def __init__(self):
6 self.window = gtk.Window()
7 self.button = gtk.Button("Hello")
8 self.button.connect("clicked", self.on_button_clicked)
9 self.window.add(self.button)
10 self.window.show_all()
11
12 def on_button_clicked(self, *args):
13 dlg = gtk.MessageDialog(None, 0, gtk.MESSAGE_INFO, gtk.BUTTONS_OK)
14 dlg.set_markup("OK")
15 ret = dlg.run()
16 dlg.destroy()
17 return ret
18
19 def main(self):
20 gtk.main()
21
22 if __name__ == '__main__':
23 helloworld = HelloWorld()
24 helloworld.main()
Here is setting a dialog handler for 'Hello' button and testing the label text:
1 import unittest
2 import gtk
3 from guitest.gtktest import GtkTestCase, guistate
4
5 import hello2
6
7 class TestHelloWorld(GtkTestCase):
8
9 def test_button_clicked(self):
10 helloworld = hello2.HelloWorld()
11 button = helloworld.button
12 guistate.dlg_handler = self.handle_hello_clicked
13 button.emit("clicked")
14
15 def handle_hello_clicked(self, dlg, *args):
16 label = dlg.label
17 if label.get_label() == "OK":
18 return gtk.RESPONSE_OK
19 else:
20 self.fail("Label is not 'OK'")
21
22
23 def test_suite():
24 suite = unittest.TestSuite()
25 suite.addTest(unittest.makeSuite(TestHelloWorld))
26 return suite
27
28
29 if __name__ == '__main__':
30 unittest.main()
For experimenting this code, just change the assertion, 'label.get_label() == "OK"'. If there is another dialog box coming after 'OK' button clicked, you can add a new handler inside handle_hello_clicked function. For example: