QGIS Plugins Automated Testing – Writing Effective Tests

pyqgisqgis-pluginsunit testing

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:

  • Their special QGIS plugin tester which can automate tests inside the QGIS environment
  • The use of docker QGIS images, allowing testing against various QGIS versions/configurations in a container-base environment
  • A special docker QGIS image, which is used for testing QGIS itself, but which - by invoking qgis_testrunner.sh can be used to run unit tests on a plugin
  • The use of Travis CI for continuous integration - i.e. full test suite is run with every new code commit

If you are familiar with Travis CI/docker it ought to be relatively easy to set up.

  1. Pull the Docker image with the QGIS testing environment and run it
  2. Run qgis_setup.sh NameOfYourPlugin to install the plugin and prepare QGIS for the test runner
  3. Optionally perform all operations needed to build your plugin
  4. Run the test runner inside the Docker invoking the 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.

UPDATE (April-2020): Some of the mentioned links no longer work, but a small example of how to run the tests using Travis CI is provided here along with the qgis-testing environment Dockerfile by Planet, formerly known as Boundless.

Related Question