diff --git a/README.md b/README.md index 41ec7e1..dd2749b 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ markers can be used. to ensure that they are cleaned properly if they are used but not added to the `QgsProject`. This is only needed with layers with other than memory provider. ```python - # conftest.py of start of a test file + # conftest.py or start of a test file import pytest from pytest_qgis.utils import clean_qgis_layer from qgis.core import QgsVectorLayer @@ -74,11 +74,13 @@ markers can be used. configure [`QgsApplication`](https://qgis.org/pyqgis/master/core/QgsApplication.html). With QGIS >= 3.18 it is also used to patch `qgis.utils.iface` with `qgis_iface` automatically. + > Be careful not to import modules importing `qgis.utils.iface` in the root of conftest, because the `pytest_configure` hook has not yet patched `iface` in that point. See [this issue](https://github.com/GispoCoding/pytest-qgis/issues/35) for details. + ### 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. +* `--qgis_disable_init` can be used to prevent QGIS (QgsApplication) from initializing. Mainly used in internal testing. ### ini-options diff --git a/src/pytest_qgis/pytest_qgis.py b/src/pytest_qgis/pytest_qgis.py index dbe67de..4f336e0 100644 --- a/src/pytest_qgis/pytest_qgis.py +++ b/src/pytest_qgis/pytest_qgis.py @@ -22,6 +22,7 @@ import os.path import shutil import sys +import tempfile import time import warnings from collections import namedtuple @@ -30,7 +31,6 @@ from unittest import mock import pytest -from _pytest.tmpdir import TempPathFactory from qgis.core import Qgis, QgsApplication, QgsProject, QgsRectangle, QgsVectorLayer from qgis.gui import QgisInterface as QgisInterfaceOrig from qgis.gui import QgsGui, QgsLayerTreeMapCanvasBridge, QgsMapCanvas @@ -92,6 +92,7 @@ _IFACE: Optional[QgisInterface] = None _PARENT: Optional[QtWidgets.QWidget] = None _AUTOUSE_QGIS: Optional[bool] = None +_QGIS_CONFIG_PATH: Optional[Path] = None try: _QGIS_VERSION = Qgis.versionInt() @@ -153,6 +154,8 @@ def qgis_app(request: "SubRequest") -> QgsApplication: if not sip.isdeleted(_CANVAS) and _CANVAS is not None: _CANVAS.deleteLater() _APP.exitQgis() + if _QGIS_CONFIG_PATH and _QGIS_CONFIG_PATH.exists(): + shutil.rmtree(_QGIS_CONFIG_PATH) @pytest.fixture(scope="session") @@ -280,13 +283,12 @@ def qgis_show_map( def _start_and_configure_qgis_app(config: "Config") -> None: - global _APP, _CANVAS, _IFACE, _PARENT + global _APP, _CANVAS, _IFACE, _PARENT, _QGIS_CONFIG_PATH settings: Settings = config._plugin_settings # type: ignore # Use temporary path for QGIS config - tmp_path_factory = TempPathFactory.from_config(config, _ispytest=True) - config_path = tmp_path_factory.mktemp("qgis-test") - os.environ["QGIS_CUSTOM_CONFIG_PATH"] = str(config_path) + _QGIS_CONFIG_PATH = Path(tempfile.mkdtemp(prefix="pytest-qgis")) + os.environ["QGIS_CUSTOM_CONFIG_PATH"] = str(_QGIS_CONFIG_PATH) if not settings.qgis_init_disabled: _APP = QgsApplication([], GUIenabled=settings.gui_enabled)