A pytest plugin for testing QGIS python plugins
Project description
pytest-qgis
A pytest plugin for testing QGIS python plugins.
Features
This plugin makes it easier to write QGIS plugin tests with the help of some fixtures and hooks:
Fixtures
qgis_app
returns and eventually exits fully configuredQgsApplication
. This fixture is called automatically on the start of pytest session.qgis_canvas
returnsQgsMapCanvas
.qgis_parent
returns the QWidget used as parent of theqgis_canvas
qgis_iface
returns stubbedQgsInterface
qgis_new_project
makes sure that all the map layers and configurations are removed. This should be used with tests that add stuff toQgsProject
.qgis_processing
initializes the processing framework. This can be used when testing code that callsprocessing.run(...)
.qgis_version
returns QGIS version number as integer.qgis_world_map_geopackage
returns Path to the world_map.gpkg that ships with QGISqgis_countries_layer
returns Natural Earth countries layer from world.map.gpkg as QgsVectorLayer
Markers
qgis_show_map
lets developer inspect the QGIS map visually at the teardown of the test. NOTE: This marker is still experimental and layer order might differ if using layers with different coordinate systems. Full signature of the marker is:@pytest.mark.qgis_show_map(timeout: int = 30, add_basemap: bool = False, zoom_to_common_extent: bool = True, extent: QgsRectangle = None)
timeout
is the time in seconds until the map is closed.add_basemap
when set to True, adds Natural Earth countries layer as the basemap for the map.zoom_to_common_extent
when set to True, centers the map around all layers in the project.extent
is alternative tozoom_to_common_extent
and lets user specify the extent asQgsRectangle
Check the marker api documentation and examples for the ways markers can be used.
Utility tools
clean_qgis_layer
decorator found inpytest_qgis.utils
is a decorator which can be used withQgsMapLayer
fixtures to ensure that they are cleaned properly if they are used but not added to theQgsProject
. This is only needed with layers with other than memory provider.# conftest.py of start of a test file import pytest from pytest_qgis.utils import clean_qgis_layer from qgis.core import QgsVectorLayer @pytest.fixture() @clean_qgis_layer def some_layer() -> QgsVectorLayer: return QgsVectorLayer("layer_file.geojson", "some layer")
Hooks
pytest_configure
hook is used to initialize and configureQgsApplication
. With QGIS >= 3.18 it is also used to patchqgis.utils.iface
withqgis_iface
automatically.
Command line options
--qgis_disable_gui
can be used to disable graphical user interface in tests. This speeds up the tests that use Qt widgets of the plugin.--qgis_disable_init
can be used to prevent QGIS (QgsApllication) from initializing. Mainly used in internal testing.
ini-options
qgis_qui_enabled
whether the QUI will be visible or not. Defaults toTrue
. Command line option--qgis_disable_gui
will override this.qgis_canvas_width
width of the QGIS canvas in pixels. Defaults to 600.qgis_canvas_height
height of the QGIS canvas in pixels. Defaults to 600.
Requirements
This pytest plugin requires QGIS >= 3.10 to work.
Installation
Install with pip
:
pip install pytest-qgis
Development environment
- Create a virtual environment and activate it.
pip install pip-tools
pip-sync requirements.txt requirements-dev.txt
Updating dependencies
pip-compile --upgrade
pip-compile --upgrade requirements-dev.in
Contributing
Contributions are very welcome.
License
Distributed under the terms of the GNU GPL v2.0
license, "pytest-qgis" is free and open source software.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pytest-qgis-1.1.1.tar.gz
(35.7 kB
view hashes)
Built Distribution
pytest_qgis-1.1.1-py3-none-any.whl
(919.3 kB
view hashes)
Close
Hashes for pytest_qgis-1.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8f33b391825e960c663c9d870c59d8837be463917ab30cd02d2bc625c6910a4 |
|
MD5 | 8d269474a17d8e5db5d51814f7c7d9d9 |
|
BLAKE2b-256 | 24fbc51eaac2e83659fac1d12947eb849d7dbbd0d660321795182dcdef21ccbd |