views:

77

answers:

3

I've heard of unit testing, and written some tests myself just as tests but never used any testing frameworks. Now I'm writing a wxPython GUI for some in-house data analysis/visualisation libraries. I've read some of the obvious Google results, like http://wiki.wxpython.org/Unit%20Testing%20with%20wxPython and its link http://pywinauto.openqa.org/ but am still uncertain where to start.

Does anyone have experience or good references for someone who sort of knows the theory but has never used any of the frameworks and has no idea how it works with GUIs?

I am on a Windows machine developing a theoretically cross-platform application that uses NumPy, Matplotlib, Newville's MPlot package, and wxPython 2.8.11. Python 2.6 with plans for 3.1. I work for a bunch of scientists, so there is no in-house unit-testing policy.

A: 

If you want to unit-test your application, you haven't to focus on GUI testing techniques. It is much better to write the application using MVC, MVP, or other meta-pattern like these. So you get business logic and presentation layer separated.

It is much more important to cover the business layer with tests since this is your code. Presentation layer is tested already by wxWidgets developers. To test the business layer it will be enough just basic tools like standard unittest module and maybe nose.

To make sure the whole application behave correctly, you should add few acceptance tests that will test functionality from end to end. These will deal with GUI, but there will be few such tests comparing to number of unit-tests.

If you will limit yourself with acceptance tests only, you'll get low coverage, fragile and very slow test code base.

nailxx
maybe I'm misusing the terminology here. The business logic---how to plot XAFS data, etc.---has already been written. I was brought in specifically to put pretty menus on top, so almost all my code //is// presentation layer and the sort of bugs I'm looking to catch are things like, did I forget to redraw the canvas so that the new plot doesn't appear until the user resizes the window? Or do I misunderstand what presentation layer means?
Wang
Automatic testing of beautifulness and visual correctness is a hard task. In case of canvas resize it would be better to test Presenter/Controller configured with mock canvas: call `my_presenter.handle_button_x_click` and then `assert my_presenter.canvas.redraw.called`.
nailxx
A: 

To unit test your application without requiring lots of mock objects/stubs, your GUI's event handlers should basically delegate to other method calls, passing in values from the Event object as parameters to the delegated method.

Otherwise you'll be unable to test your application without having to mock wx's objects.

Take a look at the PyPubSub project for a great module to help with MVC.

Steven Sproat
A: 

In one early project of mine I really test wxPython application using GUI layer. So tests really spin live wxApp object, pops up real windows and then starts messing with a real MainLoop(). Very soon I realize it was a wrong way to do testing. My tests was run very slow and unreliable. Much better way is to separate GUI stuff aside and test only the "model" level of your application. Note that you can actually create model for presentation level logic (model that represent some visual part of your application) and test it. But this model should not involve any "real" gui objects (windows, dialogs, widgets).

Vladimir Ignatov