[Qgis-developer] How to debug and unit test Python plugins outside QGIS (e.g. in Ecplise IDE + PyDev)

Alex Mandel tech_dev at wildintellect.com
Wed Mar 2 21:26:28 EST 2011


On 03/02/2011 03:57 PM, Stefan Keller wrote:
> Hi,
> 
> I'm new to writing Python plugins for QGIS and I like to debug and
> unit test theses outside QGIS runtime but don't see any instructions
> on how to do this.
> 
> Of course, the ultimate test of any plugin is to run it inside QGIS.
> But I'd like do that outside QGIS for example in Eclipse IDE (+ PyDev)
> in order to get code-completion, to debug it and to write unit tests
> on it.
> 
> The problem is that one gets "ImportError: No module named ..." with
> mostly PyQt4/qgis modules which (of course) cannot be imported.
> 
> This post mentions "Forced builtin libs" and reports a similar
> problem: http://www.osgeo.org/pipermail/qgis-developer/2010-February/009182.html
> ... and this PyDev documentation explains "Forced builtin libs" and
> "Predefined completions":
> http://pydev.org/manual_101_interpreter.html#id1
> 
> I can't imagine that I'm the first trying to debug and unit test
> Python plugins for QGIS.
> 
> Can anybody give me a hand on this?
> 
> Yours, S.

I've been meaning to write this up for some time. The only way I've
found to Debug a QGIS plugin is using a debugger hook in python. While I
have only tested this method with SPE's Winpdb (No Win does not stand
for MS Windows - SPE is python cross platform) debugger it theoretically
should be possible with other tools if they support the same kind of
debugging method.

Basically I insert something like:
import rpdb2; rpdb2.start_embedded_debugger("testing")

just below the main part of my plugin:
class MetaEditPlugin:
    def __init__(self,iface):
        #Only turn on to use winpdb debugger
        import rpdb2; rpdb2.start_embedded_debugger("testing")

Then the procedure is to start QGIS, Launch the plugin, switch to SPE's
winpdb, then do File attach, put in the same password "testing".
It will then show you the running apps you can connect to, once you
select and connect the plugin will continue to run.
At this point you can set breakpoints and catch errors. If/When it stops
at such a point you can interact with the app in a way similar to
Python's pdb sending eval and exec snippets, and inpecting all your
variables in the list.

Make sure to comment out that line for production use otherwise your
plugin will always stall and wait for the debug to connect.

Basically I think this falls under the term "Remote Debugging" if you
want to try and find ways to do it with other apps.

I'm not sure there is any other practical method as most plugins rely on
the QGIS built in stuff to be in an active session, so I wouldn't waste
too much time trying to do it without QGIS running at all.

Enjoy,
Alex


More information about the Qgis-developer mailing list