6. Teste de Algoritmos de Processamento

6.1. Testes de algoritmo

Nota

The original version of these instructions is available at https://github.com/qgis/QGIS/blob/release-3_16/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/qgis_algorithm_tests.yaml or 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
  algorithm: qgis:polygoncentroids
  params:
    - type: vector
      name: polys.gml
  results:
    OUTPUT_LAYER:
      type: vector
      name: expected/polys_centroid.gml

6.1.1. Como

Para adicionar um novo teste, por favor, siga os passos em baixo:

  1. 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 testdata/custom.

  2. When you have run the algorithm, go to Processing ► History and find the algorithm which you have just run.

  3. Right click the algorithm and click Create Test. A new window will open with a text definition.

  4. 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 ones to params and the last one(s) to results.

The above translates to

- name: densify
  algorithm: qgis:densifygeometriesgivenaninterval
  params:
    - type: vector
      name: polys.gml
    - 2 # Interval
  results:
    OUTPUT:
      type: vector
      name: expected/polys_densify.gml

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.

6.1.2. Parâmetros e resultados

Parâmetros do tipo trivial

Os parâmetros e os resultados são especificados como listas ou dicionários:

params:
  INTERVAL: 5
  INTERPOLATE: True
  NAME: A processing test

ou

params:
  - 2
  - string
  - another param

Parâmetros do tipo camada

You will often need to specify layers as parameters. To specify a layer you will need to specify:

  • o tipo , por exemplo, vector ou raster

  • a name, with a relative path like expected/polys_centroid.gml

This is what it looks like in action:

params:
  PAR: 2
  STR: string
  LAYER:
    type: vector
    name: polys.gml
  OTHER: another param

Parâmetros do tipo ficheiro

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”:

params:
  PAR: 2
  STR: string
  EXTFILE:
    type: file
    name: custom/grass7/extfile.txt
  OTHER: another param

Resultados

Os resultados são especificados muito similarmente.

Ficheiros de vetor básico

It couldn’t be more trivial

OUTPUT:
 name: expected/qgis_intersection.gml
 type: vector

Add the expected GML and XSD files in the folder.

Vetor com tolerância

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 compare property for an output. The compare property can contain sub-properties for fields. This contains information about how precisely a certain field is compared (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.

OUTPUT:
 type: vector
 name: expected/abcd.gml
 compare:
   fields:
     __all__:
       precision: 5 # compare to a precision of .00001 on all fields
     A: skip # skip field A
   geometry:
     precision: 5 # compare coordinates with a precision of 5 digits
Raster files

Raster files are compared with a hash checksum. This is calculated when you create a test from the processing history.

OUTPUT:
 type: rasterhash
 hash: f1fedeb6782f9389cf43590d4c85ada9155ab61fef6dc285aaeb54d6
Ficheiros

Pode comparar o conteúdo de um ficheiro de saída com um ficheiro de referência de resultado esperado

OUTPUT_HTML_FILE:
 name: expected/basic_statistics_string.html
 type: file

Ou pode utilizar uma ou mais expressões regulares que serão correspondidas <https://docs.python.org/3/library/re.html#re.search>`_ contra o conteúdo do ficheiro

OUTPUT:
 name: layer_info.html
 type: regex
 rules:
   - 'Extent: \(-1.000000, -3.000000\) - \(11.000000, 5.000000\)'
   - 'Geometry: Line String'
   - 'Feature Count: 6'
Diretorias

You can compare the content of an output directory with an expected result reference directory

OUTPUT_DIR:
 name: expected/tiles_xyz/test_1
 type: directory

6.1.3. Contexto de Algoritmo

Existem mais algumas definições que podem modificar o contexto do algoritmo - estas podem ser especificadas no nível superior do teste:

  • project - irá carregar um ficheiro de projeto QGIS especificado antes de executar o algoritmo. Se não for especificado, o algoritmo será executado com um projeto em branco

  • project_crs - substitui o projeto CRS predefinido - por exemplo, EPSG:27700

  • ellipsoid - substitui a elipsóide do projeto predefinido utilizado para as medições, por exemplo, GRS80

6.1.4. A executar os testes localmente

ctest -V -R ProcessingQgisAlgorithmsTest

ou um dos valores seguintes listados na fonte:CMakelists.txt <python/plugins/processing/tests/CMakeLists.txt>