Qt test empty window



  • Hi,
    I create simple GUI application (QMainWindow with QListWidget, QComboBox and QPushButton) and test cpp file:

    #include <QString>
    #include <QtTest>
    #include <QCoreApplication>
    
    #include "../App/mainwindow.h"
    
    #include <QComboBox>
    
    class TestsTest : public QObject
    {
        Q_OBJECT
    
    public:
        TestsTest();
    
    private Q_SLOTS:
        void testCase1();
    
    private:
        MainWindow* mainWindow_;
    };
    
    TestsTest::TestsTest()
    {
        mainWindow_ = new MainWindow();
        mainWindow_->show();
    }
    
    void TestsTest::testCase1()
    {
        QString comboBoxName = "comboBox";
        QComboBox* comboBox = mainWindow_->findChild<QComboBox*>(comboBoxName);
        if (comboBox)
        {
            QTest::qSleep(1000);
            QTest::mouseClick(comboBox, Qt::LeftButton);
            QTest::qSleep(1000);
        }
        else
        {
            QFAIL("invalid combobox name");
        }
    }
    
    QTEST_MAIN(TestsTest)
    
    #include "tst_teststest.moc"
    

    Test create empty window (no widgets) but QFAIL will not be executed. I debug code and QComboBox is found. I will record video from the test and it is not possible at the moment. I use Qt 5.6.0 MinGW 32-bit on Windows 7.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    From your description MainWindow should contain a combo box and in your test you create that MainWindow, so it does sound reasonable that the QComboBox is found, no ?



  • Yes, mainwindow contains combobox, but screenshot shows something else. Yes, this is only image, but I want create video record from testing. Is it normal? Screenshot from Qt 5.6 64bit linux.
    image


  • Moderators

    The problem could be that you have two QTest::qSleep(1000); calls which block the event loop.
    See here: http://doc.qt.io/qt-5/qtest.html#qSleep
    "Sleeps for ms milliseconds, blocking execution of the test. qSleep() will not do any event processing and leave your test unresponsive. Network communication might time out while sleeping. Use qWait() to do non-blocking sleeping."

    That means, your UI cannot be drown properly. Try to use qWait.



  • @jsulm Thank you, problem solved.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.