Christian Kalkhoff
Christian Kalkhoff

Reputation: 192

How to test signal-slot connections with qtest

I am just starting out with QT and would like to have unit tests. I found a blueprint for a project structure that supports having tests outside my application.

For my first test I wanted to have a click on the "Quit" menu point, checking that the close action of QMainWindow is really called.

Coming from Java with all its mocking frameworks, here is what I would like to do:

  1. Simulate Click on Menu (or key press)
  2. Click on Quit Action (or key press)
  3. Verify that signal "triggered()" of QAction was emitted
  4. Verify that slot "close" of QMainWindow was called

I read the qtest tutorial, KDE documentation and other stuff on the net but I am not able to express that using qtest (yet).

I found Qt UI testing: How to simulate a click on a QMenuBar item using QTest? which describes a similar thing, but even there only 1, 2, 3 are covered (And I was not even able to get them to work).

So how can I write such a test case? Is that even possible?

Upvotes: 4

Views: 8520

Answers (1)

Gusch5
Gusch5

Reputation: 137

For testing signal, the QSignalSpy class can be used http://doc.qt.io/qt-5/qsignalspy.html

But you want to test the whole application - not a single unit. For that a UI testing tool like squish or ranorex (both commercial) is way better suited.

Upvotes: 4

Related Questions