I'm building a PySide 1.1.0-based application, and have been looking for good examples to look at for unit and functional testing my application. I want to be able to do functional testing of the UI (simulating clicks, key presses, etc), unit testing of UI slots that alter the layout of the UI (presumably using a partially-mocked sender and receiver), as well as unit testing of code that involves widgets, but without requiring any windows to be rendered.
As one example, I dynamically create submenus of one menu in the menubar when an item is added to a model (QAbstractItemModel-derived object) that provides data to a QTreeView. The model and submenu must stay in sync, so I want to be able to write a unit test that submits data to the controller that manages the model and submenu, and asserts that both the model and submenu were properly updated.
I would prefer to NOT have to set up a QApplication in my test code if I can avoid it. I also would like to not have to display any windows when I only care about validating data structures in widgets, not their visualization.
I can't find anything of suitable value at http://www.pyside.org or in my Google searches. Does anyone have any experience or know of good sample code that I should look at?
I've been playing around a bit now with unit-testing pyside code and came to the conclusion that combining python's
unittest
module with qt'sQTest
module works pretty good.You will have to have a
QApplication
object instantiated, but you do not need to run itsexec_
method, because you don't need the event loop to be running.Here is an example on how I test if a
QCheckBox
in a dialog does what it is supposed to do:This completely works off-screen, involves clicking widgets and typing text in a
QLineEdit
.Here is how I test a (rather simple)
QAbstractListModel
:I hope this helps a littlebit.
In my case, I was getting an error 'QPixmap: Must construct a QApplication before a QPaintDevice'.
If you need to have a QApplication instance for your tests (eg use QPixmap), here's one way to do it. Just create a singleton so that you are ensured one and only one QApplication instance.
This is buried as a helper for tests in the PySide source.
and then subclass UsesQApplication
hope this helps