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.
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
- https://github.com/qgis/QGIS/tree/master/tests/src/python Python unit tests of QGIS. It is very useful as documentation on how to use classes and functions and how to test them.
- https://github.com/qgis/QGIS/blob/master/python/testing/mocked.py Mocked QgisInterface
- https://github.com/opengisch/QgisModelBaker/tree/master/QgisModelBaker/tests Example of test implementations using qgis.testing in the QGIS Model Baker project
- https://github.com/opengisch/qfieldsync/tree/master/qfieldsync/tests Example of test implementations using qgis.testing in the QFieldsync project
- https://github.com/qgis/QGIS/tree/master/.docker Documentation to the QGIS docker image and how to use it to run tests inside a real instance of QGIS.
- https://github.com/boundlessgeo/qgis-testing-environment-docker Docs to create a docker container with a real QGIS instance (now part of QGIS)
- https://github.com/opengisch/plugin_ci Scripts to do automated testing and deployment for QGIS plugins