/qgis_plugins_test_demo

Example of the different existing solutions to test qgis plugins

Primary LanguagePython

Plugins test

Using qgis.testing module

See https://github.com/qgis/QGIS/blob/master/python/testing/__init__.py.

Calling the start_app() function, a QgsApplication is started and all initialization code is called.

This solution is especially suitable for unit tests.

Virtually every QGIS feature that has a python API can be used except for operations directly related to the user interface.

For example, in the QGIS Model Baker project, https://github.com/opengisch/QgisModelBaker/tree/master/QgisModelBaker/tests this solution is used for unit testing.

Some examples are located in the qgis_testing directory of this repo.

The test_geometries.py module will create some geometries on a temporary layer and the test_print_layout.py module will load a QGIS project and generate a PDF with the layout manager.

To run the tests call

nosetests /qgis_testing

from the repo root.

Full QGIS in a docker

This is a way to run tests inside a complete running instance of QGIS.

See https://github.com/qgis/QGIS/tree/master/.docker for a complete documentation.

In the qgis_docker directory of this repo there are some examples of tests to be run with this solution.

To run the tests you need to first build the docker image

# Clone the QGIS repo
git clone QGIS/QGIS

# Build the docker image
docker build -t qgis/qgis:latest \
 --build-arg DOCKER_TAG=latest \
 -f .docker/qgis.dockerfile \
 .

Then run the docker container in which to perform the tests mounting the local directory with the tests inside the docker

# Run the docker container in which to perform the tests
docker run -d --name qgis -v /tmp/.X11-unix:/tmp/.X11-unix \
 -v /home/mario/workspace/repos/qgis_plugins_test_demo/qgis_docker/:/tests_directory \
 -e DISPLAY=:99 \
 qgis/qgis:latest

And finally you can launch the tests inside the docker container

# Run the tests in the docker QGIS
docker exec -it qgis sh -c "cd /tests_directory && qgis_testrunner.sh tests.test_geometries.run_all"

It is also possible to run a QGIS instance with GUI from this docker

# To run a QGIS instance with GUI
xhost +

docker run --rm -it --name qgis \
 -v /tmp/.X11-unix:/tmp/.X11-unix \
 -e DISPLAY=unix$DISPLAY \
 qgis/qgis:latest qgis

References