-
-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
behaviour with QMessageBox.question
and other modal dialogs
#18
Comments
Hi Benjamin, Well, this is an issue I find often at work with our xUnit code base. For def test_Qt(qtbot, mock):
simple = Simple()
qtbot.addWidget(simple)
mock.patch.object(QMessageBox, 'question', return_value=QMessageBox.Yes)
simple.query()
assert simple.answer (here I'm using pytest-mock, but using mock directly is fine too) But the problem remains when you want to test a customized One solution is to use a def test_dialog(qtbot):
dialog = Dialog()
qtbot.addWidget(dialog)
def interact():
qtbot.click(dialog.button)
QTimer.singleShot(500, interact)
simple.query() # blocks Another approach is to not call If you have some kind of Cheers, |
Hi, thanks for the I tried both of your suggestion for the a In any case, I am fine with Thanks again for your time and detailled answers. |
Thanks for the suggestion, I will add an example to the documentation (#19). 😄 About the example, I should have provided some code to make it clearer: Suppose you have a custom dialog that asks the user for their name and age, and a 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 as such: name, age = AskNameAndAgeDialog.ask("Enter name and age because of bananas:", parent)
if name is not None:
# use name and age for bananas Now it is easy to mock def test_form_registration(qtbot, mock):
form = RegistrationForm()
mock.patch.object(AskNameAndAgeDialog, 'ask', return_value=('Jonh', 30))
qtbot.click(form.enter_info()) # calls AskNameAndAgeDialog.ask
# test that the rest of the form correctly behaves as if
# user entered "Jonh" and 30 as name and age Hope that's clearer. If you have any other questions, please don't hesitate to ask them. :) Cheers |
I do not understand how to successfully test the behaviour of modal dialogs, and did not find an example in the documentation -- or anywhere else, for that matter.
Question
How to prevent modal dialogs from poping up in testing, and how do I simulate clicking on their buttons with
qtbot
?Sample file
What I tried
Because
self.question
is a QMessageBox.StandardButton, i.e. an enum, it has no method to allow clicking on it. I tried to set directlyself.question
toQMessageBox.Yes
, for instance, but obviously, this does not close the window.Also, adding
self.question
toqtbot
withaddWidget
does not work, as it is no real widget.Could this be due to a wrong design choice on my side? Should the opening of the question be in a separate function, and the rest of the logic would simply take as an input the result of the
question
. Or am I testing this the wrong way?Additional but (maybe) related issues
It is seems related to me, but when calling an
exec_
method on a customizedQtGui.QDialog
object, the same problem appears. I can not use the methodqtbot.mouseClick
on the object before clicking on it manually. Because the object pops out, and interrups the flow of the program.Of course, this is the whole point of such an object in the main application, but while testing, it looks like it messes things up. Feel free to ignore this part if you deem it too unrelated (I can also open another ticket).
The text was updated successfully, but these errors were encountered: