Graphics Reference
In-Depth Information
Tip
You can, of course, have multiple test_XXX() methods in your test case. The PyQGIS
library will be initialized before the first test is run, and shut down after the last test fin-
ishes.
Testing plugins in this way does reveal a major limitation of this approach: there is no
QgisInterface object available for your plugin to use. This means that the parts of
the plugin you're testing can't interact with the rest of the QGIS system via the iface
variable.
Unit tests get around this limitation by creating a fake QGIS environment (including a
Python implementation of QgisInterface ), which the plugin can use for testing. The
plugin is then loaded by adding the plugin's directory to sys.path and then calling the
plugin's ClassFactory() function with the fake QgisInterface :
sys.path.append("/path/to/my/plugin")
import MyPlugin
plugin = MyPlugin.classFactory(fake_iface)
While it seems complex and might introduce errors that only occur while the plugin is be-
ing tested, this process is actually very useful. If you want to use unit tests, you can either
implement your own QgsInterface or make use of the unit testing framework
provided by the Plugin Builder.
Note
If you want to roll your own unit tests, a good starting point is available at http://snorf.net/
blog/2014/01/04/writing-unit-tests-for-qgis-python-plugins .
If you are doing unit testing, then you would normally add an extra target to your Make-
file so you can run the unit tests simply by typing:
make test
Search WWH ::




Custom Search