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

Stefan Keller sfkeller at gmail.com
Thu Mar 3 02:38:36 EST 2011


Many thanks also for the hint to standalone apps.!
I still hope there is a solution to unit test QGIS plugins.
This would make QGIS programmers even more happy :->

Yours, S.


2011/3/3 Alex Mandel <tech_dev at wildintellect.com>:
> It might be possible, I'm not super familiar with units tests yet.
> The big issue I see is that most plugins rely on the main QGIS app
> providing data to the plugin or interacting with the plugin. If you can
> design the test to provide what the unit test needs then it should be
> doable. After all you can build standalone python apps with the QGIS api.
>
> Thanks,
> Alex
>
> On 03/02/2011 10:13 PM, Stefan Keller wrote:
>> Hi Alex,
>>
>> Many thanks for your reply. I'll try this.
>>
>> But: So you say implicitly that its impossible to run unit tests
>> (PyUnit) on QGIS plugins?
>> I still hope there is a way to make PyGt4 module interface accessible
>> outside QGIS. At runtime its there, so it should be somehow possible
>> either by "Forced builtin libs" or by extracting the information by
>> hand with dummy stubs...?
>>
>> Yours, S.
>>
>> 2011/3/3 Alex Mandel <tech_dev at wildintellect.com>:
>>> 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
>>> _______________________________________________
>>> Qgis-developer mailing list
>>> Qgis-developer at lists.osgeo.org
>>> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>>>
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
>


More information about the Qgis-developer mailing list