Closed Josdelsan closed 1 year ago
I managed to fix this changing the fixture aproach and using headless-gui to run the tests as pointed in #510.
The fixture now just return Window instance, also use addWidget
to properly handle the life cycle of the widget. Active dialog is accesed using activeModalWidget()
.
import pytest
from PyQt6.QtCore import Qt, QTimer
from PyQt6.QtWidgets import QApplication, QDialogButtonBox
from pyqt.__main__ import Window
@pytest.fixture(scope="function")
def app(qtbot):
window = Window()
window.show()
qtbot.addWidget(window)
with qtbot.waitExposed(window):
return window
def test_1(qtbot, app):
window: Window = app
dialog_handled = False
def handle_dialog():
dialog = QApplication.activeModalWidget()
while not dialog:
dialog = QApplication.activeModalWidget()
dialog.button_box.button(QDialogButtonBox.StandardButton.Ok).click()
nonlocal dialog_handled
dialog_handled = True
QTimer.singleShot(5, handle_dialog)
qtbot.mouseClick(window.button, Qt.MouseButton.LeftButton)
assert dialog_handled
Changes in the workflow file:
- name: Test with pytest and generate report
uses: aganders3/headless-gui@v1
with:
run: |
pytest --cov-report xml --cov
Both changes are necessary for the action to succeed.
I created some end to end test that work correctly in my local machine (Win11 22H). These tests use a fixture that manage the instances of
QApplication
and the mainQMainWidget
. The goal is to load the aplication and perform some setup actions. This fixture returns the instance ofQApplication
. In each test theQMainWidget
instance is accesed usingQApplication.activeWindow()
method.When tests are executed by GitHub action the
QApplication.activeWindow()
returns None. I tried returning theQMainWidget
instance but it looks like there is an error in its creation.I reproduced the issue creating an script that follows the application structure
Simple app with dialog:
Test:
For testing purposes I am using act in order to test locally the actions. To completly reproduce the failure using act some extra dependencies must be installed (xvfb libnss3 libxdamage1 libasound2). Workflow:
Here is the original workflow error from GitHub in the original repository.
I managed to partially solve the error including the fixture code inside the test. It only worked for one of the tests in the original repository (test_open_project). The other tests, as well as the example reproduction, completly freeze the console (both local and remote) when this "workaround" is applied.
test_open_project
test is the only one that does not deal with a custom QDialog but a QFileDialog with mocked features.I am new to the qt world so I am bit confused with the life cycle management of the widgets and dialog management when testing. The workaround for dialog testing was taken from #237 .
Here it is the list of python libraries used in the project: