Is it possible for QTestLib to display the GUI it is testing as it runs?
Asked Answered
D

2

9

The use case is, I have a Qt app, and I would like to automate user-style testing of it; that is, I'd like to use keyClicks(), mouseClick(), and so on, but I would like for the Qt application window to actually be displayed while this is happening.

The issue I'm having right now is that using QTestLib involves using the QTEST_MAIN macro instead of defining main myself, so I never get an opportunity to show() the widgets being tested. So, another way to word this question is, is there a way to use QTestLib on an application that is using its main function?

I know Squish and probably Testability Driver are capable of this, but if it is possible to get this functionality without using extra tools, then that would be ideal.

Dolor answered 14/1, 2014 at 23:27 Comment(2)
Sound an awful lot like what Squish already accomplishes. Just an observation...Teratology
I know Squish already does this, and I said that in the last sentence of the review. The key problem is that there is zero chance of a budget being approved that includes a several-thousand-dollar Squish license.Dolor
D
9

Figured out how to do this. Makes Squish totally unnecessary, but it requires source code access.

In your test class, store a pointer to the QApplication and any widgets you want to test. For ease of use, store a pointer to your application's QMainWindow. Then, either instantiate your test class with pointers to the widgets you plan on testing, or use window->findChild() to grab any element you need. Keep in mind that you will need to call app->processEvents() after everything. Call it after showing the widget so that all of the child widgets appear. Call it after interacting with a widget so that the interaction is actually processed on the GUI. If you need things to be slow enough to watch, use QTest::qSleep().

Dolor answered 22/1, 2014 at 14:11 Comment(1)
I'm trying to do exactly that, can you please share your code or part of it?Colan
K
0

@KelvinS. This is my snippet of code following @VGambit method, which tries to test adding a log to itemview.

#include <QApplication>
#include <QWidget>
#include <QtTest/QtTest>
#include "guimain.h"`
#include "xlogview.h"`

class TestLogView:public QObject
{
    Q_OBJECT
    public:
        void set_mainwindow(QWidget * qw);
    public slots:
        void startTest();
    private:
        QWidget * m_qw ;

    private slots:
        void addItem();
};
void TestLogView::startTest()
{
    QTest::qExec(this);
}

void TestLogView::set_mainwindow(QWidget * qw)
{
    m_qw = qw;
}
void TestLogView::addItem()
{
    XLogView * test_logview= m_qw->findChild<XLogView*>();
    bool ret = test_logview->addLog("new log");
    QVERIFY (ret == true);
}
#include "main.moc"
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    GUIMain window;
    window.show();
    app.processEvents();
    TestLogView test;
    test.set_mainwindow(&window);
    QTimer::singleShot(1000, &test, SLOT(startTest()));
    return app.exec();
}
Kowtko answered 7/10, 2017 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.