Skip to content

Commit d4dc79f

Browse files
committed
Add teardown hook to close open dialogs after tests
1 parent 0f7c177 commit d4dc79f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

plotpy/tests/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import scipy
1414
import tifffile
1515
from guidata.env import execenv
16+
from qtpy import QtCore as QC
17+
from qtpy import QtWidgets as QW
1618

1719
import plotpy
1820

@@ -49,6 +51,20 @@ def pytest_runtest_setup(item):
4951
pytest.skip("Skipped in offscreen mode (requires display)")
5052

5153

54+
@pytest.hookimpl(tryfirst=True)
55+
def pytest_runtest_teardown(item, nextitem): # pylint: disable=unused-argument
56+
"""Run teardown after each test."""
57+
# This is necessary to close any open dialogs after each test because the
58+
# mechanism used to close them automatically in the test suite
59+
# (i.e., `exec_dialog`) does not work with some PyQt versions.
60+
QC.QCoreApplication.processEvents()
61+
qapp: QW.QApplication = QW.QApplication.instance()
62+
if qapp is not None:
63+
for widget in qapp.topLevelWidgets():
64+
if isinstance(widget, QW.QDialog) and widget.isVisible():
65+
widget.reject()
66+
67+
5268
@pytest.fixture(scope="session", autouse=True)
5369
def disable_gc_for_tests():
5470
"""Disable garbage collection for all tests in the session."""

0 commit comments

Comments
 (0)