This documentation describes how to run and write tests for the BTAA QGIS Plugin. The testing strategy relies on pytest, pytest-cov, and pytest-mock, along with ruff for code formatting and linting. This matches the testing patterns used in the backend FastAPI application.
Before running the tests, ensure you have installed the testing dependencies on your environment. You can install them with pip via the pyproject.toml file.
cd qgis-plugin
python -m pip install -e ".[dev]"We use a Makefile to simplify running tests and linters. It mirrors the commands in the backend/ directory.
Make sure you are in the qgis-plugin directory:
-
Run tests with coverage check:
make testThis runs
pytestand generates both a terminal coverage report and an HTML coverage report (incoverage_html_report/). The tests will fail if line coverage is below 50% (default). -
Run tests without coverage check (debugging):
make test-no-coverage
This runs
pytest -vwithout any coverage constraints, showing detailed output for each test. -
Check formatting and linting:
make lint
Runs
ruff check .to highlight linting errors. -
Fix formatting automatically:
make format
Runs
ruff format .followed byruff check --fix .. Code should generally be formatted before making commits. -
Check formatting for CI:
make lint-check
Runs in CI checking mode. Fails if formatting is incorrect.
The test suite is located in the tests/ directory.
Because QGIS plugins rely heavily on qgis and PyQt components that require a running QGIS instance, we utilize unittest.mock to mock these modules in tests/conftest.py. This allows the tests to run quickly as standard Python tests without spinning up the QGIS application locally.
When writing tests:
- Use standard
pytestfixtures for common setup. - Any new QGIS or PyQt modules imported by your source files must be added to the mocks in
tests/conftest.pyif they are not already. - Store unit tests in files starting with
test_within thetests/directory. - Patch side effects, like network calls via
requests, using python'sunittest.mock.patch.