I'm looking for advice on writing automatic tests for QGIS plugins written in Python.
I have written tests for Python scripts in the past using PyUnit (the unittest
module), but have never done so for an application with a GUI. I've found a page describing how to use PyQt4.QTest
to do unit tests on Qt widgets (http://www.voom.net/pyqt-qtest-example), but I'm struggling to see how I can use this with a widget that has been designed to run from within QGIS.
The section on "Testing" in the PyQGIS documentation is notably absent.
What I have so far is:
- Keep the actual processing of data in isolated modules or functions, and write unit tests for those;
- Perform basic testing of the UI using
QTest
; - Pray that it all holds together when using the plugin from within QGIS.
Is there a better way?
Best Answer
The capabilities for testing QGIS plugins (particularly the question of integration testing, within a QGIS environment, as the OP highlights) has improved a great deal recently. I therefore hope this update will help contemporary readers, as well as the OP.
Boundless published a must-read article in July 2016 for anyone serious about automating the testing of QGIS plugins entitled; QGIS Continuous Integration Testing Environment for Python Plugins. It describes the approach and tools they use - all of which are open source.
Key aspects are:
qgis_testrunner.sh
can be used to run unit tests on a pluginIf you are familiar with Travis CI/docker it ought to be relatively easy to set up.
qgis_testrunner.sh
You asked for best practice & as of today I'd certainly consider this it. QGIS docs still haven't a dedicated section on plugin testing (I expect this will change shortly) but the "Pray that it all holds together" approach is certainly no longer the only option.