File: note_dialogs.rst

package info (click to toggle)
pytest-qt 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 644 kB
  • sloc: python: 4,144; makefile: 139
file content (62 lines) | stat: -rw-r--r-- 1,862 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
A note about Modal Dialogs
==========================

Simple Dialogs
--------------

For QMessageBox.question one approach is to mock the function using the `monkeypatch <https://docs.pytest.org/en/latest/monkeypatch.html>`_ fixture:

.. code-block:: python

    def test_Qt(qtbot, monkeypatch):
        simple = Simple()
        qtbot.addWidget(simple)

        monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.Yes)
        simple.query()
        assert simple.answer

Custom Dialogs
--------------

Suppose you have a custom dialog that asks the user for their name and age, and a form
that uses it. One approach is to add a convenience function that also has the nice
benefit of making testing easier, like this:

.. code-block:: python

   class AskNameAndAgeDialog(QDialog):
       @classmethod
       def ask(cls, text, parent):
           dialog = cls(parent)
           dialog.text.setText(text)
           if dialog.exec_() == QDialog.Accepted:
               return dialog.getName(), dialog.getAge()
           else:
               return None, None

This allows clients of the dialog to use it this way:

.. code-block:: python

   name, age = AskNameAndAgeDialog.ask("Enter name and age because of bananas:", parent)
   if name is not None:
       # use name and age for bananas
       ...

And now it is also easy to mock ``AskNameAndAgeDialog.ask`` when testing the form:

.. code-block:: python

    def test_form_registration(qtbot, monkeypatch):
        user = User.empty_user()
        form = RegistrationForm(user)

        monkeypatch.setattr(
            AskNameAndAgeDialog, "ask", classmethod(lambda *args: ("John", 30))
        )
        # Clicking on the button will call AskNameAndAgeDialog.ask in its slot.
        form.enter_info_button.click()

        assert user.name == "John"
        assert user.age == 30