Frage

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.

War es hilfreich?

Lösung

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().

Andere Tipps

@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();
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top