Viper
Viper

Reputation: 505

How to close a QFileDialog.getOpenFileName in QTest?

I am developing an application in Python 3.5, using PyQt4, and I want to test it with unit test using QTest. So far I managed to do whatever I want with QTest except for this problem :

My window as a folder button, when you click it, a File Dialog open to choose a folder (classic). This is (more or less) written like that in my code :

self.file_dialog = QtGui.QFileDialog()
[...]    
tmp_path = self.file_dialog.getOpenFileName(self, caption='toto', filter="*.csv")

My problem is that in QTest, I can't find a way to close this dialog when it shows up.

I already tried calling its close() or reject() method (directly or with a single shot timer), but none of these seems to work...

Is there any solution I didn't thought of ? I wouldn't mind closing all windows if that's necessary (but I can't do that either)

Thanks !

Upvotes: 4

Views: 761

Answers (1)

Stefano Gariazzo
Stefano Gariazzo

Reputation: 26

As far I have understood, you should not use the static methods (such as getOpenFileName) in tests, because you cannot control the opening of the dialog window. You should instead use the constructor, set the options you need and then call self.file_dialog.exec_() only in the real code (not in the tests).

Upvotes: 1

Related Questions