GispoCoding / pytest-qgis

A pytest plugin for testing QGIS python plugins
GNU General Public License v2.0
29 stars 8 forks source link

Can't use addToolBar with qgis_iface #19

Closed jmkerloch closed 2 years ago

jmkerloch commented 2 years ago

Describe the bug I'm using qgis_iface fixture to initialize my plugin

def test_1loadplugin(qgis_app, qgis_iface):
    qgis_app.initQgis()
    plugin = Plugin(qgis_iface)
    plugin.initGui()

But when I add a toolbar with my plugin I get this error

self = <pytest_qgis.qgis_interface.QgisInterface object at 0x7f0ac330ddc0>, name = <PyQt5.QtWidgets.QToolBar object at 0x7f0b3344faf0>

    def addToolBar(self, name: str) -> QToolBar:
        """Add toolbar with specified name.

        :param name: Name for the toolbar.
        :type name: str
        """
>       toolbar = QToolBar(name, parent=self._mainWindow)
E       TypeError: 'parent' is an unknown keyword argument

To Reproduce Steps to reproduce the behavior:

  1. Create a plugin with toolbar add

self.iface.addToolBar(self.new_toolbar)

  1. Create a pytest with pytest-qgis plugin use and plugin initialization

Expected behavior qgis_iface fixture should allow mocking of toolbar add.

Environment (please complete the following information):

Additional context I have upgraded my pytest-qgis installation with new version 1.3.0

JaumeFigueras commented 2 years ago

Hi, I ran into the same problem, but with @Joonalai help we solved it. I do not initialize plugins the way you exemplified. I run the same procedure as QGIS does, calling qgis.util functions. You can check my code at https://github.com/JaumeFigueras/GisFIRE-SpreadSimulation/tree/master/test It is "tuned" to my config of multiple plugins but I think is easy enough to understand. Check the test and the two fixtures I wrote. Hope this helps.

Joonalai commented 2 years ago

Thank you for the issue! It seems that the addToolBar method of QgisInterface supports also QToolBar objects as first argument. The stub implementation should be fixed to take this into account.

Joonalai commented 2 years ago

As a side note, you don't have to initialize QGIS, since it is done automatically.

def test_1loadplugin(qgis_app, qgis_iface):
    qgis_app.initQgis()
    plugin = Plugin(qgis_iface)
    plugin.initGui()
vicente23 commented 2 years ago

Hi,

I encountered the same problem when I upgraded pytest-qgis to the version 1.3.0.

The problem was not present in version 1.2.0.

Joonalai commented 2 years ago

Thank you for letting me know, I will fix this ASAP.