2

In my PyQt app I have a main menu, which has the following structure:

Main Menu
|___
    |
    Language
    |       |
    |        Russian
    |       |
    |        English
    Exit    

I want to perform a click on Russian to test if the language is changed. I have a link to language_menu in GUI

[...somewhere in MainWindow...]
self.language_menu = QtGui.QMenu()
[....]

so

from PyQt4 import QtGui, QtCore
from PyQt4.QTest import QTest
from gui import MainWindow

class TestMainWindow(unittest.TestCase):

    def setUp(self):
        self.app = QtGui.QApplication([])
        self.ui = MainWindow()

    def tearDown(self):
        self.app.deleteLater()

    def test_translation(self):
        menu = self.ui.language_menu
        rus_lang = menu.actions()[0]
        QTest.mouseClick(rus_lang, QtCore.Qt.LeftButton)

but it tells me that

argument 1 has unexpected type 'QAction'

How do I do this? Is it even possible?

GriMel
  • 2,272
  • 5
  • 22
  • 40

2 Answers2

2

You can use rus_lang.trigger() or rus_lang.toggle() to activate the menu item.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
0

i think ekhumoro's answer is wrong because it misses the point of the test, rus_lang.trigger() isnt interacting with the gui.

for me QTest.mouseClick(rus_lang, QtCore.Qt.LeftButton) works fine with QButton maybe QAction has diffrent API.

ניר
  • 1,204
  • 1
  • 8
  • 28