Exercise 3: fix a broken plugin - rnousia/example-plugin-for-quc2026 GitHub Wiki
Run the following commands in Vs Code terminal to get the setup for the next exercise.
Add the workshop repository as the remote origin (run git init first if not done already):
git remote add origin https://github.com/rnousia/example-plugin-for-quc2026.gitDownload the latest commits and tags from the remote repository:
git fetch origin --tagsReset your local repository to match the workshop starting point:
git reset --hard origin/mainInstall dependencies:
uv syncRun the tests with command line:
pytest testOpen QGIS with the plugin loaded:
qpdt sInstall Plugin Reloader from the QGIS plugin repository (Plugins -> Manage and Install Plugins... -> Search for "Plugin reloader":
Some data has been added to a geopackage found in the repository's folder example. Add it to the map canvas and test what happens if you use the example map tool called painter tool
.
Checkout to a version of the plugin having a bug with raster layers:
git checkout raster-bugNote
Instructor will demonstrate a live demo of debugging at this point.
Tip
Keyboard shortcuts for debugger:
F5 -> Continue to next breakpoint
F10 -> Step to next line of code
F11 -> Step into a function
Try to catch the same bug using debugger and unit tests. Use test\map_tool\test_painter_tool.py::test_paint_layer_at_location_does_nothing_if_raster_clicked as a starting point.
For a Vs Code to discover tests,
A test can be debugged by right-clicking the play button left from the test function and selecting "Debug Test":
Note
Remember to add a breakpoint first on some line of the test (this can be done by clicking left of the line numbers -> red dot appears)
The test is intentionally incomplete. Use the debugger to check which QgsPointXY to pass to the function to replicate the bug with the test.
Hint if you got stuck
See the existing test in the same file `test_paint_layer_at_location_restores_previous_tool_if_tool_was_selected` and how it is using `painter_tool._paint_layer_at_location(at_location)`
When you get the test to fail, try to come up a solution to fix the bug. The test should pass when the plugin is fixed. Use Plugin Reloader to test your changes to the plugin code without needing to restart QGIS.
If you cannot start a test from an opened test file because the play button is missing:
Try refreshing tests via VS Code's testing panel:
Afterwards the button should be visible again:
Reset the previous changes
git reset --hard HEADCheckout to a version of the plugin having a bug causing QGIS to crash:
git checkout qgis-crash-bugInvestigate the plugin behaviour first by using the plugin in QGIS and the by running the tests in a command line. Try to fix the bug.
Hint if you got stuck
See the method _repaint_layer. It is using renderer's own symbol in line 107 and then resetting renderer in 110...
New symbol can be created with QgsSymbol.defaultSymbol(layer.geometryType()).
After fixing the bug, restart QGIS with qpdt s and check it works properly now.