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

Martin Dobias wonder.sk at gmail.com
Tue Jan 10 12:08:16 EST 2012


> On Thu, Mar 3, 2011 at 10:40 AM, Martin Dobias <wonder.sk at gmail.com> wrote:
>
>> The only difference when running unit-tests (i.e. standalone apps) is
>> that you cannot access QGIS interface (qgis.utils.iface) since there
>> is no QGIS running. But that should not pose a big problem. In case
>> you really need access to that interface you can create your own
>> implementation of QgisInterface class and assign it to
>> qgis.utils.iface.
>>
>> Regards
>> Martin
>
> Martin do you (or anyone else) have example code for creating an iface
> instance from a standalone python / QGIS app?

Hi Tim

no I do not have a working example. In general it should be something like this:

import qgis.utils
import qgis.gui

class MyQgisInterface(qgis.gui.QgisInterface):
  def __init__(self):
    QgisInterface.__init__(self)
  def mapCanvas(self):
    return FOO
  def mainWindow(self):
    return BAR
  def activeLayer(self):
    return BAZ
  # ... more methods ...

# use our interface instance
qgis.utils.iface = MyQgisInterface()

You will have to provide implementation of all the abstract virtual
functions defined in QgisInterface. Luckily most of them are not used
by plugins, so you may provide just implementations not
doing/returning anything - depending on how precisely would you like
to imitate QGIS environment.

Regards
Martin


More information about the Qgis-developer mailing list