Graphics Reference
In-Depth Information
Unit testing
Unit tests are a common programming technique to make sure each part of your code
works as it should. The following is a very simple example of a unit test written in Python:
import unittest
def double(n):
return n * 2
class TestDouble(unittest.TestCase):
def test(self):
self.assertEqual(double(2), 4)
You can run this unit test either directly from the command line, or by adding extra code to
create a TestRunner object that you can then use to run the test.
We're not going to describe the rationale behind unit testing, or how to use the unittest
library to test your Python code. However, it is worth spending some time learning how
you can write and run unit tests for your QGIS plugins.
Note
If you haven't worked with the unittest module before, check out http://docs.python-
guide.org/en/latest/writing/tests .
Unit testing is done outside of QGIS itself; that is, the unit tests run as an external Python
application that loads your plugin and then tests it. Doing this isn't as bad as it sounds; in
Chapter 1 , Getting Started with QGIS , we looked at a simple external application built on
top of QGIS, and we can use pretty much the same process to write our testing code. Here's
the boilerplate example of an external application, copied from Chapter 1 , Getting Started
with QGIS :
import os
from qgis.core import *
QgsApplication.setPrefixPath(os.environ['QGIS_PREFIX'], True)
QgsApplication.initQgis()
Search WWH ::




Custom Search