Exercise 3: fix a broken plugin - rnousia/example-plugin-for-quc2026 GitHub Wiki

Step 1: Check out a plugin repository with a bug

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.git

Download the latest commits and tags from the remote repository:

git fetch origin --tags

Reset your local repository to match the workshop starting point:

git reset --hard origin/main

Install dependencies:

uv sync

Run the tests with command line:

pytest test

Step 2: Install Plugin Reloader and test the example plugin

Open QGIS with the plugin loaded:

qpdt s

Install Plugin Reloader from the QGIS plugin repository (Plugins -> Manage and Install Plugins... -> Search for "Plugin reloader":

image

Try out the example plugin

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 painter tool icon.


Step 3: Checkout to a broken revision

Checkout to a version of the plugin having a bug with raster layers:

git checkout raster-bug

Note

Instructor will demonstrate a live demo of debugging at this point.


Step 4: Debugging with tests

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":

iamge

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.


Troubleshooting

The play button for running/debugging a test is missing

If you cannot start a test from an opened test file because the play button is missing:

image

Try refreshing tests via VS Code's testing panel:

image

Afterwards the button should be visible again:

image

Step 5: Debugging a bug causing QGIS to crash

Reset the previous changes

git reset --hard HEAD

Checkout to a version of the plugin having a bug causing QGIS to crash:

git checkout qgis-crash-bug

Investigate 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.

⚠️ **GitHub.com Fallback** ⚠️