Skip to main content

A pytest plugin for testing QField qml plugins

Project description

pytest-qfield

A pytest plugin for testing QField QML plugins. This plugin uses pytest-qgis and QGIS behind the hood since QField does not have a python api.

Features

Fixtures

The following fixtures are provided by pytest-qfield:

  • qfield_bot: A QFieldBot instance to interact with the QField QML environment.
  • qfield_iface: A stub implementation of the QField application interface (iface in QML).
  • qfield_new_project: Initializes a new QField project.
  • main_window_qml_path: Path to the QML file used for the QField main window. Can be overridden to use a custom QML main window.

Stubs and Overriding Fixtures

pytest-qfield provides several stub fixtures that are automatically injected into the QML engine's context. You can override these fixtures in your conftest.py to provide custom behavior or extended versions of the stub classes.

Available Stubs

The following stub fixtures correspond to objects available in the QField QML context:

Fixture Name QML Context Property Description
qfield_iface iface QField application interface.
qgs_project_stub qgisProject QgsProject instance.
qfield_platform_utilities_stub platformUtilities Platform-specific utilities.
qfield_string_utils_stub StringUtils String utility functions.
qfield_layer_utils_stub LayerUtils Layer utility functions.
qfield_feature_utils_stub FeatureUtils Feature utility functions.
qfield_geometry_utils_stub GeometryUtils Geometry utility functions.
qfield_qml_extra_context_properties (various) Dictionary of extra context properties to inject.

How to Override

To override a stub or another fixture, define it in your project's conftest.py:

# conftest.py
import pytest
from pytest_qfield.stub_interface.qfield_stubs import QFieldAppInterfaceStub

class CustomIface(QFieldAppInterfaceStub):
    def some_custom_method(self):
        return "custom value"

@pytest.fixture
def qfield_iface(qgis_iface):
    return CustomIface(qgis_iface)

You can also use qfield_qml_extra_context_properties to inject additional objects into the QML context:

@pytest.fixture
def qfield_qml_extra_context_properties():
    return {
        "myCustomObject": MyCustomObject()
    }

Other overridable fixtures include:

  • main_window_qml_path: Override to use a different QML file as the main window shell.
  • register_qfield_resources: Override to register your own compiled Qt resources (.qrc).
  • register_qfield_types: Override to register additional QML types.
  • register_qgis_types: Override to register additional QGIS-related QML types.

QFieldBot

The qfield_bot fixture provides several methods to help testing:

  • load_plugin(qml_file): Loads a QField plugin QML file.
  • open_project(qfield_project_file): Opens a QField project file.
  • show_window(): Shows the QField main window.
  • get_item(object_name): Finds a QML item by its objectName.
  • click_item(item): Simulates a mouse click on a QML item.
  • load_js_function(js_file, function_name, params): Loads a JavaScript function from a file for direct testing.

Examples

Installation

You must have QGIS >= 4.0 installed to use this plugin.

Install with pip or uv to a python environment that is aware of system QGIS libraries. You can create one with qgis-venv-creator.

pip install pytest-qfield
# uv add --dev pytest-qfield

Configure QField imports path

The plugin needs access to QField source code for its QML imports (typically QField/src/qml/imports).

First clone QField source code somewhere and checkout the v4.0.6 tag and then set it either with an environment variable or a pytest.ini value.

QFIELD_IMPORTS_DIR=/absolute/path/to/QField/src/qml/imports pytest
# pyproject.toml
[tool.pytest.ini_options]
qfield_imports_dir = /absolute/path/to/QField/src/qml/imports

Development environment

This project uses uv to manage python packages. Make sure to have it installed first.

  • Clone QField source code somewhere and checkout the v4.0.6 tag
  • Copy .env.example to .env and fill the missing values
  • Create a venv that is aware of system QGIS libraries: uv venv --system-site-packages. Make sure to use same Python executable as QGIS.
# Activate the virtual environment
$ source .venv/bin/activate
# Install dependencies
$ uv sync
# Install pre-commit hooks
$ prek install
# Run tests
$ uv run pytest

Updating dependencies

uv lock --upgrade

Release process

Releases are made automatically upon push to main branch using Python Semantic Release.

Contributing

Contributions are very welcome.

Inspirations

License

Distributed under the terms of the GNU GPL v2.0 license, "pytest-qfield" is free and open source software.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_qfield-0.2.2.tar.gz (20.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_qfield-0.2.2-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

Details for the file pytest_qfield-0.2.2.tar.gz.

File metadata

  • Download URL: pytest_qfield-0.2.2.tar.gz
  • Upload date:
  • Size: 20.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pytest_qfield-0.2.2.tar.gz
Algorithm Hash digest
SHA256 680411d21e6a83d74a358b2b5e9642832a26b2e37912fa9bf3970b56f6a6a65c
MD5 a6014a696dfc8ae0615168168d8188cb
BLAKE2b-256 fe9382424e7caef2b0e8a3f5be4e1bb6c06d51e9a9fe1e3d253dc14588ecf323

See more details on using hashes here.

File details

Details for the file pytest_qfield-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: pytest_qfield-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 25.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pytest_qfield-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b9db83bdd71535e7d9f9abe331ab3452d0c0cd47b108d0971c609af00141643d
MD5 e24745e4a5c9a8ffbc248b4823d5ab19
BLAKE2b-256 4a43289974971e9f8a587b7bbb2d636f341f119f4624e6fcd9fe68e784a8804a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page