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

application changes persist through tests, qtbot.mouseClick() not working on sample test application #346

Closed xstasi closed 3 years ago

xstasi commented 3 years ago

Apologies in advance for reporting two issues at the same time, but I figured it would be messier to have two tracked issues with mostly identical contents.

Application changes persist through tests

This is the main issue that I am encountering on the application I am developing. The application automatically stores UI changes through QSettings, for this reason I have made a fixture with autouse=True that unsets $HOME so that no configuration is saved. I have been able to confirm through strace that no configuration is saved to disk by QSettings this way.

Furthermore I have made a fixture with a function scope that instances the window, yields and then closes it. Regardless, when qtbot clicks something, the effect of that click is brought to the subsequent tests. A test toggles an option, which causes another test that expects the application to be in its default state to fail.

I have created a PoC file with a simple application containing just a checkbox that saves state through QSettings, and two tests that click the same checkbox: test.py.txt

qtbot.mouseClick() not working on sample test application

While it works on the real application I am developing, qtbot seems to not work in the PoC that I made for this bug report even though I cannot see any substantial difference in the two codes.

The sample application has a handler linked to the checkbox that logs on a file whenever it's triggered. This never happens when qtbot sends the click, but when I do it in the stopForInteraction() segment it works as it should.

Chances are I am overlooking something stupid, but despite my best efforts I cannot see anything that would warrant the malfunction.


Even though I cannot get qtbot to work properly, I could still reproduce the first issue - when I click the checkbox during the stopForInteraction() of the first test, the checkbox state is preserved on the second stopForInteraction() part.

The-Compiler commented 3 years ago

Also see #323 re the first issue - I don't think that's anything specific to pytest-qt, and I doubt there's anything I could do about this. You will need to take care of cleaning up global state between tests.

xstasi commented 3 years ago

@The-Compiler do you have any suggestions on how to do it? I tried calling del on the widget and qtbot in the teardown, QApplication.quit(), even running pytest-forked, nothing seems to help.

nicoddemus commented 3 years ago

@xstasi,

Perhaps you can clear the QSettings object explicitly between tests? Untested:

@pytest.fixture(autouse=True)
def clear_settings() -> None:
    yield
    QSettings().clear()
adamgranthendry commented 3 years ago

I am also having trouble with qtbot.mouseClick() and would appreciate help on this problem too!

nicoddemus commented 3 years ago

Hi @adamgranthendry

@The-Compiler's comment still applies here:

Also see #323 re the first issue - I don't think that's anything specific to pytest-qt, and I doubt there's anything I could do about this. You will need to take care of cleaning up global state between tests.

Your problem might be different however, feel free to open a separate issue describing it.

Closing this one as not really actionable on pytest-qt's part.

adamgranthendry commented 3 years ago

@nicoddemus, @xstasi, & @The-Compiler, please see the answers to my StackOverflow questions as they may help you and future users with the same problem(s):

  1. pytest-qt Function mouseMove() Not Working

  2. pytestqt.mouseMove to menubar menu widget moves to wrong place

nicoddemus commented 3 years ago

Thanks for the links!