File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -475,6 +475,43 @@ from ``pytest.ini`` in some tests by using a mark with the same name:
475
475
def test_foo (qtlog ):
476
476
do_something()
477
477
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
+
478
515
Reference
479
516
=========
480
517
You can’t perform that action at this time.
0 commit comments