25

There is a nice resource on unit testing for QGIS. There is also a post on this, Writing automated tests for QGIS plugins, which requires the use of docker and relies on QGIS 2 plugin - QGIS Tester.

The progress on QGIS 3 is related to two plugins: 1) Tester and 2) QGIS Core Tests.

How this all ties up with the final screen of QGIS Plugin Builder once we activate the Unit Tests checkbox? Where to start when we want to unit test the developed plugins?

enter image description here

5
  • 2
    Is there a directory named test in your newly created plugin directory ? Jan 5, 2021 at 18:01
  • 1
    Yes, there is such a folder. We have also investigated two plugins: 1) QField Sync & 2) QGIS Core Tests. It appears that the QField Sync plugin did unit testing, and we are trying to understand the logic.
    – banbar
    Jan 6, 2021 at 12:39
  • 2
    if you have a test directory, so you can look inside the Makefile for make test or just do Python unittest. Jan 6, 2021 at 12:56
  • 1
    The files present under the test directory are 1) __init__.py, 2) qgis_interface.py, 3) tenbytenraster.asc, 4) tenbytenraster.asc.aux.xml, 5) tenbytenraster.keywords, 6) tenbytenraster.lic, 7) tenbytenraster.prj, 8) tenbytenraster.qml, 9) test_init.py, 10) test_qgis_environment.py, 11) test_resources.py, 12) test_save_attributes_dialog.py, 13) test_translations.py and 14) utilities.py. These were created by default. When I look at the tests folder of the QField Sync plugin, it looks much tidy with only 6 Python files and a data folder.
    – banbar
    Jan 6, 2021 at 15:44
  • 6
    So like I said, if you are under Linux distrib, in the plugin directory, in a terminal, you can run a make test, or just look in the Makefile under the test lines. See : g-sherman.github.io/Qgis-Plugin-Builder/#using-the-makefile . This will run typically Python unittest for all files begin with test_. Jan 6, 2021 at 15:50

1 Answer 1

2

If you are familiar with pytest, you could try using pytest-qgis plugin to write unit tests for QGIS. You don't need any of the boilerplate of starting the QGIS since it does it automatically.

Here is a sample test that checks that a layer has been added to the project:

def test_plugin(qgis_countries_layer):
    project = QgsProject.instance()
    project.addMapLayer(qgis_countries_layer)
    assert project.count() == 1
    assert project.mapLayersByName(qgis_countries_layer.name()) == [qgis_countries_layer]
   

Since pytest is not part of the Python standard library, it is suggested that you create a virtual environment for running the tests:

  1. python -m venv --system-site-packages .venv
  2. source .venv/bin/activate or .venv\Scripts\activate.bat on Windows
  3. pip install pytest-qgis
  4. Finally run pytest to run the test(s)

Disclaimer: I am one of the authors of pytest-qgis.

2
  • Thanks for your contribution Joonala. Will this work for plugins that have GUI elements? Aug 3 at 14:31
  • pytest-qgis works well with pytest-qt plugin, which is meant for interacting with Qt GUI elements. It can be used to handle automatic closing of dialogs, pressing buttons, waiting for qt signals and much more.
    – Joonala
    Aug 25 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.