pytest-dev / pytest-qt

pytest plugin for Qt (PyQt5/PyQt6 and PySide2/PySide6) application testing
https://pytest-qt.readthedocs.io
MIT License
409 stars 70 forks source link

`qapp.processEvents()` Required for `qtbot.mouseMove` to Work #384

Closed adamgranthendry closed 3 years ago

adamgranthendry commented 3 years ago

System Info:

Windows 10 x64 Pro, Build 1909 Python: 3.8.10 pytest-qt: 4.0.2 Interface: PyQt5, version 5.15.4

Question:

I am trying to test (confirm really) that when my mouse hovers over a toolbar button that the statusbar message appears and is correct for the given button:

conftest.py:

from typing import Any, Callable, Generator, List, Sequence, Union

import pytest
import pytestqt
from pytest.qtbot import QtBot

from project.main import MainApp  # My GUI; subclass of QMainWindow

@pytest.fixture
def app(qtbot: QtBot) -> Generator[MainApp, None, None]:
    root = MainApp()
    root.show()
    qtbot.addWidget(root)

    yield root

test_view.py:

import pytest
from project.main import MainApp
from pytestqt.qt_compat import qt_api
from pytest.qtbot import QtBot

def test_toolbar_newbutton_hover(app: MainApp, qapp: QtBot, qtbot: QtBot):
    # Arrange
    new_button = app.toolbar.widgetForAction(app.new_action)
    qtbot.addWidget(new_button)

    # Act
    qtbot.mouseMove(new_button)
    qapp.processEvents()

    # Assert
    assert app.statusbar.currentMessage() == "Create a new project..."

Is there a reason why my above test does not work when I remove qapp.processEvents()? Interesting, regardless of whether or not I set app.setMouseTracking(True), the above does not work until I add the line qapp.processEvents(). When I add a 10 second delay with time.sleep(10) at the end of the Act sequence, I can see that no statusbar message appears when qapp.processEvents() is removed and the message appears when qapp.processEvents() is included.

This one has me scratching my head a bit, so I'm trying to understand the logic behind why this is occuring in case I need to add qapp.processEvents() to further tests:

No qapp.processEvents():

image

With qapp.processEvents():

image

adamgranthendry commented 3 years ago

Actually, this is no longer working now that I have updated my code to start the project window in full screen mode. For an update on my question, please see this SO post. It seems this issue has been part of a long-standing problem...

adamgranthendry commented 3 years ago

My code actually contained an error that was causing this problem. Please see my SO post for the solution.

nicoddemus commented 3 years ago

Thanks for coming back to share the solution @adamgranthendry. 👍