Efficient python programming...
peter at engcorp.com
Sat Jun 8 06:07:34 CEST 2002
Sean 'Shaleh' Perry wrote:
> > I'll step back from any claim that unit tests really prove anything,
> > but in a very Pythonic and pragmatic fashion they are Good Enough to
> > make them a /necessity/ for anyone trying to deliver reliable software.
> my current project is a window manager for X Windows. It is all X lib calls
> and UI events. I can maybe test 5% of it. Seems most of the programs I have
> worked on turn out that way. I write as many tests as I can but usually it
> just doesn't work out for me.
There are approaches that could increase that significantly.
The key pattern is to create a layer which accepts the various calls
you make into the UI and library and turns the information into something
like strings which you can easily test. Take simple code which makes
such calls, test your test code with it, and then freeze the test code.
Now you have a layer which can replace ("mock") the UI or library
and allow you to intercept and check calls that would have been made to it.
Now all your UI stuff is testable at least in terms of whether the right
routines are called at the right times with the right parameters. You
aren't testing whether the right pixel turns on, but then you are using
a library for that: you don't need to test it.
Stepping back even farther: keep the untestable code in a very thin
layer, test it manually and thoroughly, then never change it. You
don't have to test this bit of code that is "so simple it cannot fail",
when the alternative is to skip testing of 95% of your application.
Automated testing of GUIs is a very underdeveloped area of software
development, but it's a frontier that almost nobody is bothering to
explore. If more people check it out I think we'll make better
progress developing it.
More information about the Python-list