Dugan Chen's Homepage

Various Things

Test Driven Development with PyQt Maya Plugins

2012-06-28

Do you want to use PyQt to develop your Maya plugins? Of course you do; the alternative is Mel. Do you want to develop these plugins via the process known as test driven development (or a variation, as shown below)? Again, of course you do. Any other process would be inefficient. Well guess what. You can do both.

The trick is that if you test any part of your application that uses a GUI, then you need to run the tests from inside Maya. If you don’t, then stop reading and just run the tests with Maya’s python interpreter and Nose. If you do, then Python’s unit testing documentation tells you how to run tests with “no requirement to be run from the command line”. That allows you to run the tests from an interactive environment such as, say, Maya.

Testing PyQt’s GUI components is done with its provided QTest class. The QTest class is meant to be used with Python’s existing unit testing framework. A particularly good tutorial can be found here:

I am paranoid enough about keeping the API at version 2 that I simply treat the setapi calls as boilerplate. To make this less painful, I recommend an editor that supports snippets, such as vim with neocomplcache-snippets-complete.

Launch Maya and verify that it runs. Click the button and confirm that it it saves a scene named “testscene.ma”.

from plugin import main
main()

At this point, we are ready to start writing our automated tests. As we work, they will reduce the chance of regressions.

Our “unit” test suite consists of a single functional test. It simulates a button click, and then tests that the scene has been saved. If it has, then it cleans up. Here is tests.py: