This post originated from an RSS feed registered with Python Buzz
by Baiju M.
Original Post: Functional/Acceptance Testing Using guitest
Feed Title: Programming life without Python(?)
Feed URL: http://baijum81.livejournal.com/data/rss
Feed Description: Programming life without Python(?)
guitest (http://gintas.pov.lt/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.
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. ::
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::
Here is setting a dialog handler for 'Hello' button and testing the label text::
import unittest
import gtk
from guitest.gtktest import GtkTestCase, guistate
import hello2
class TestHelloWorld(GtkTestCase):
def test_button_clicked(self):
helloworld = hello2.HelloWorld()
button = helloworld.button
guistate.dlg_handler = self.handle_hello_clicked
button.emit("clicked")
def handle_hello_clicked(self, dlg, *args):
label = dlg.label
if label.get_label() == "OK":
return gtk.RESPONSE_OK
else:
self.fail("Label is not 'OK'")
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestHelloWorld))
return suite
if __name__ == '__main__':
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::
def handle_hello_clicked(self, dlg, *args):
label = dlg.label
if label.get_label() == "OK":
guistate.dlg_handler = self.handle_ok_clicked
return gtk.RESPONSE_OK
else:
self.fail("Label is not 'OK'")