Skip to content

Commit 9fa04b5

Browse files
committed
Add warning in documentation about QApplication.exit()
Fixes #37
1 parent ce340a0 commit 9fa04b5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

docs/index.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,43 @@ from ``pytest.ini`` in some tests by using a mark with the same name:
475475
def test_foo(qtlog):
476476
do_something()
477477
478+
479+
A note about QApplication.exit()
480+
================================
481+
482+
Some ``pytest-qt`` features, most notably ``waitSignal`` and ``waitSignals``,
483+
depend on the Qt event loop being active. Calling ``QApplication.exit()``
484+
from a test will cause the main event loop and auxiliary event loops to
485+
exit and all subsequent event loops to fail to start. This is a problem if some
486+
of your tests call an application functionality that calls
487+
``QApplication.exit()``.
488+
489+
One solution is to *monkeypatch* ``QApplication.exit()`` in such tests to ensure
490+
it was called by the application code but without effectively calling it.
491+
492+
For example:
493+
494+
.. code-block:: python
495+
496+
def test_exit_button(qtbot, monkeypatch):
497+
exit_calls = []
498+
monkeypatch.setattr(QApplication, 'exit', lambda: exit_calls.append(1))
499+
button = get_app_exit_button()
500+
qtbot.click(button)
501+
assert exit_calls == [1]
502+
503+
504+
Or using the ``mock`` package:
505+
506+
.. code-block:: python
507+
508+
def test_exit_button(qtbot):
509+
with mock.patch.object(QApplication, 'exit'):
510+
button = get_app_exit_button()
511+
qtbot.click(button)
512+
assert QApplication.exit.call_count == 1
513+
514+
478515
Reference
479516
=========
480517

0 commit comments

Comments
 (0)