Debian Codegraph @

docs/source/traitsui_user_manual/testing/discussions/compatibility_pyface_testing.rst

raw
1 Compatibility with Pyface test utilities
2 ========================================
3
4 |UITester| is intended to be compatible with Pyface's |ModalDialogTester|,
5 (for testing with modal dialogs) and |GuiTestAssistant| (for general
6 GUI event loop handling in tests).
7
8 .. _testing-with-modal-dialogs:
9
10 Testing with modal dialogs
11 --------------------------
12
13 When a test involves a modal dialog, |ModalDialogTester| will be needed.
14 |UITester| can be used together, for example, to launch the modal dialog
15 which then gets closed by |ModalDialogTester|::
16
17 from pyface.constant import OK
18 from pyface.toolkit import toolkit_object
19
20 from traitsui.testing.api import MouseClick, UITester
21
22 ModalDialogTester = toolkit_object(
23 "util.modal_dialog_tester:ModalDialogTester"
24 )
25
26 tester = UITester()
27 with tester.create_ui(demo) as ui:
28
29 simple_button = tester.find_by_id(ui, "simple")
30
31 def click_simple_button():
32 simple_button.perform(MouseClick())
33
34 modal_tester = ModalDialogTester(click_simple_button)
35 modal_tester.open_and_run(lambda x: x.click_button(OK))
36 assert modal_tester.dialog_was_opened
37
38 But if you try to modify or inspect GUI states using |UITester| while the
39 dialog is opened, you should set the |UITester.auto_process_events|
40 attribute to false for those operations. Otherwise the ModalDialogTester and
41 UITester will enter a deadlock that blocks forever.
42
43 Example::
44
45 def when_opened(modal_dialog_tester):
46 ui_tester = UITester(auto_process_events=False)
47 ui_tester.find_by_id(ui, "button").perform(MouseClick())
48
49 modal_dialog_tester = ModalDialogTester(callable_to_open_dialog)
50 modal_dialog_tester.open_and_run(when_opened)
51
52 In the above example, ``ui`` is an instance of |UI| that has been obtained
53 elsewhere in the test. Note that you can instantiate as many |UITester| objects
54 as you need.
55
56 Using UITester and GuiTestAssistant
57 -----------------------------------
58
59 |GuiTestAssistant| is a more general tool dealing with GUI processing in tests.
60 |UITester|, on the other hand, is a more specific tool for testing GUI
61 components managed by TraitsUI. The two can be used together in tests.
62 |GuiTestAssistant| has been around before |UITester| is introduced.
63
64 Since various methods on |UIWrapper| (such as |UIWrapper.perform| and
65 |UIWrapper.inspect|) automatically request GUI events to be processed, where
66 they are used entirely for modifying and inspecting GUI states, some previous
67 usage of |GuiTestAssistant| features may no longer be necessary.
68
69 .. include:: ../substitutions.rst