Processing Algorithms Testing¶
The original version of these instructions is available at https://github.com/qgis/QGIS/blob/release-3_4/python/plugins/processing/tests/README.md
QGIS provides several algorithms under the Processing framework. You can extend this list with algorithms of your own and, like any new feature, adding tests is required.
To test algorithms you can add entries into
testdata/gdal_algorithm_tests.yaml as appropriate.
This file is structured with yaml syntax.
A basic test appears under the toplevel key
tests and looks like this:
- name: centroid
- type: vector
To add a new test please follow these steps:
Run the algorithm you want to test in QGIS from the processing toolbox. If the result is a vector layer prefer GML, with its XSD, as output for its support of mixed geometry types and good readability. Redirect output to
python/plugins/processing/tests/testdata/expected. For input layers prefer to use what's already there in the folder
testdata. If you need extra data, put it into
When you have run the algorithm, go toand find the algorithm which you have just run.
Right click the algorithm and click Create Test. A new window will open with a text definition.
Open the file
python/plugins/processing/tests/testdata/algorithm_tests.yaml, copy the text definition there.
The first string from the command goes to the key
algorithm, the subsequent
params and the last one(s) to
The above translates to
- name: densify
- type: vector
- 2 # Interval
It is also possible to create tests for Processing scripts. Scripts should
be placed in the
scripts subdirectory in the test data directory
python/plugins/processing/tests/testdata/. The script file name
should match the script algorithm name.
Parameters and results are specified as lists or dictionaries:
NAME: A processing test
- another param
You will often need to specify layers as parameters. To specify a layer you will need to specify:
the type, ie
a name, with a relative path like
This is what it looks like in action:
OTHER: another param
If you need an external file for the algorithm test, you need to specify the 'file' type and the (relative) path to the file in its 'name':
OTHER: another param
Results are specified very similarly.
It couldn't be more trivial
Add the expected GML and XSD files in the folder.
Sometimes different platforms create slightly different results which are still acceptable. In this case (but only then) you may also use additional properties to define how a layer is compared.
To deal with a certain tolerance for output values you can specify a
property for an output. The compare property can contain sub-properties for
fields. This contains information about how precisely a certain field is
precision) or a field can even entirely be
skip``ed. There is a
special field name ``__all__ which will apply a certain tolerance to all fields.
There is another property
geometry which also accepts a
precision which is
applied to each vertex.
precision: 5 # compare to a precision of .00001 on all fields
A: skip # skip field A
precision: 5 # compare coordinates with a precision of 5 digits
Raster files are compared with a hash checksum. This is calculated when you create a test from the processing history.
You can compare the content of an output file to an expected result reference file
Or you can use one or more regular expressions that will be matched against the file content
- 'Extent: \(-1.000000, -3.000000\) - \(11.000000, 5.000000\)'
- 'Geometry: Line String'
- 'Feature Count: 6'
There are a few more definitions that can modify the context of the algorithm - these can be specified at the top level of test:
project- will load a specified QGIS project file before running the algorithm. If not specified, the algorithm will run with an empty project
project_crs- overrides the default project CRS - e.g.
ellipsoid- overrides the default project ellipsoid used for measurements, e.g.