Automating UI tests (was Re: Art of Unit Testing)

Mike Clarkson support at internetdiscovery.com
Sat Aug 25 19:32:49 EDT 2001


On Fri, 17 Aug 2001 18:33:46 GMT, Joseph Andrew Knapka
<jknapka at earthlink.net> wrote:

>Boudewijn Rempt wrote:
>
>> I've found little value in writing unittests for GUI apps
>
>This is a subject that's been on my mind lately, and since the
>subject has come up here, maybe some one can give me some pointers
>on this matter:
>
>How exactly would one go about automating unit tests of GUI code?
>Specifically, I'm working on some UI stuff in Tkinter.
>At first I thought, "well, I can just write tests that call
>my Tk callback methods directly with appropriate values", but
>then I realized that that can't work because my code
>depends on the widget state (eg might call event.widget.get()
>for a key-release event, or something like that).
>
>So that means I'd need to write code that actually drives
>the Tk widgets "from outside", as it were, either by synthesizing
>events somehow at the OS/window-system level, or by knowing
>enough about the Tk internals to convince the widgets that they're
>being clicked on, typed at, etc. And at that point it looks like
>a lot more effort than it's worth.

It's probably easier than you think: look at the Tk 'event' command.
It can deliver an arbitrary keyboard mouse or virtual event to any Tk
widget. 

In your UI code,  setup a dictionary which contains the widgets 
that you want to test by sending events to, so that you can refer to
then in the testng code. Then in the testing code, use the event
command to send thngs like <Button-1> events to those widgets.

Note that if you are testing Dialogs (popped up windows with a grab)
you probably have to use the Tk 'after' command to queue up the events
before instantiating the Dialog.

Mike.





More information about the Python-list mailing list