Search code examples
c++user-interfacetestingqt5qtestlib

How can I test a full Qt5 GUI using qtestlib?


Using qtestlib with my Qt5 C++ widgets application, how can I test my full GUI?

In the documentation for qtestlib, it is explained how I could test an indivdual QWidget by simulating keypresses etc, however this seems impossible to do for a full UI, because the individual widgets of my UI are hidden inside the automatically generated ui_XXX.h file.

So how would I go about doing this?


Solution

  • In your ui files you can give names to the widgets. You can then search through the children from your toplevel widget.

    For example to test that a Label reflects the characters entered into a LineEdit you could use:

    MainWindow win;
    
    QLineEdit *edit = win.findChild<QLineEdit *>("myLineEdit");
    QTest::sendKeys(edit, "Example");
    
    QLabel *label = win.findChild<QLabel *>("myLabel");
    QCOMPARE(label->text(), "Example");