Skip to content

Selecting pyqt version in individual test, as opposed to for the whole test suite. #577

Closed as not planned
@ahobeost

Description

@ahobeost

As part of a very large project, we would like to incrementally move from pyqt5 to pyqt6.
The solution for backwards compatibility allows this to work, just not for testing it seems).

The following two tests demonstrate what I would like to achieve and show a known issue (#437).
The issue here, is that qtbot produces a specific Qapplication (either version 5 or version 6).

class TestMessageBox:
    def testCreate(self, qtbot):
        print("I am before message box")

        from PyQt5 import QtWidgets as _qtw
        qtbot.addWidget(_qtw.QMessageBox())
        print("I am after message box")
        assert False

    def testCreatePyQt6(self, qtbot):
        print("I am before message box")
        from PyQt6 import QtWidgets as _qtw

        qtbot.addWidget(_qtw.QMessageBox())
        print("I am after message box")
        assert False

One of these (or both) will fail, depending on which api is set in the pytest.ini file:
qt_api=pyqt5

The other will fail with the following error code:
Process finished with exit code -1073740791 (0xC0000409)

It would be great if qtbot would provide a Qapplication tailored to the desired version, for example.
qt.bot.set_version('pyqt5')

And then the tests would be:

class TestMessageBox:
    def testCreate(self, qtbot):
        print("I am before message box")

        from PyQt5 import QtWidgets as _qtw
        qt.bot.set_version('pyqt5')

        qtbot.addWidget(_qtw.QMessageBox())
        print("I am after message box")
        assert False

    def testCreatePyQt6(self, qtbot):
        print("I am before message box")

        from PyQt6 import QtWidgets as _qtw
        
        qt.bot.set_version('pyqt5')
        qtbot.addWidget(_qtw.QMessageBox())
        print("I am after message box")
        assert False

The following workaround also exists, which is not compatible with qtbot:

def testCreatePyQt6(self, qtbot):
        print("I am before message box")

        from PyQt6 import QtWidgets as _qtw
        
        app = _qtw.QApplication([])
        qtbot.addWidget(_qtw.QMessageBox())
        print("I am after message box")
        assert False

Naive implementation:

class Qtbot:
    def set_version(self, api: str):
        from pytestqt.qt_compat import qt_api
        qt_api.set_qt_api(api)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions