【问题标题】:Testing Qt application with Qt Test使用 Qt Test 测试 Qt 应用程序
【发布时间】:2020-05-21 11:28:22
【问题描述】:

我查看了 5 个 Qt 测试示例,包括一个关于 GUI 事件的示例,但这些示例太简单了。

我想通过启动程序来测试我的程序,模拟一些点击,并检查这些点击已更改的实例变量的值。

我认为下面的这个测试是不合逻辑的:a.exec() 阻塞线程直到程序关闭,当程序关闭时,我认为 w 已被删除(或者稍后会被删除?)。

那么如何编写系统/GUI测试呢?

我的测试:

void LaunchProgramTest::LaunchProgramTestFunction() {
    QApplication a(argc, argv);
    MainWindow *w = new MainWindow();

    w->show();

    a.exec();

    int testResult = w->myTestFunction();

    qDebug() << testResult; //Prints big numbers like "-17891602" or "1753770528" as if testResult was not initialized

    QVERIFY2(testResult == 3, "Incorrectly changed");
}

在 mainWindow.h 我声明了一个变量:

int testValue;

Mainwindow.cpp 是程序主 GUI 的类。在我添加的构造函数中

testValue = 2;

然后在我写的事件执行的函数中

void MainWindow::on_actionTest_clicked() {
    testValue = 3;
}

【问题讨论】:

    标签: qt system-testing


    【解决方案1】:

    enter code hereSo)) 你需要加QTest,加.pro

    QT  += testlib
    

    和

    #include <QTest>
    

    我将展示我的 MousePress 实现示例,其余的你可以自己做))

    struct EventMK
        {
            int type;
            QString m_widPath;
            int _timer;
            int width;
            int height;
            QPoint p;
            QPoint g;
            int button;
            int buttons;
            int modifiers;
            int _key;
            QString text;
            void print(){
                qDebug()<<"{ \n"
                   <<"type "<< type<<","<<"\n"
                   <<"Widget_Path "<< m_widPath<<","<<"\n"
                   <<"Timer "<< _timer<<","<<"\n"
                   <<"Width "<< width<<","<<"\n"
                   <<"Height "<< height<<","<<"\n"
                   <<"Pos_x "<< p.x()<<","<<"\n"
                   <<"Pos_y "<< p.y()<<","<<"\n"
                   <<"Global_x "<< g.x()<<","<<"\n"
                   <<"Global_y "<< g.y()<<","<<"\n"
                   <<"Button "<< button<<","<<"\n"
                   <<"Buttons "<< buttons<<","<<"\n"
                   <<"Modifiers "<< modifiers<<","<<"\n"
                   <<"Key "<< _key<<","<<"\n"
                   <<"Text "<< text<<"\n"
                   <<"}\n";
    
            }
        };
    
    QWidget * _getWidget(EventMK ev)
    {
        QString wname = ev.m_widPath;
        QStringList wpath = wname.split ( "/" );
        return QWidgetUtils::getAWidget(&wpath);
    }
    
    void _postExecution(EventMK ev, QWidget *widget)
    {
        if (widget){
    
            //set focus
            QWidgetUtils::setFocusOnWidget(widget);
    
            //end simulation
            widget->setUpdatesEnabled ( true );
            widget->update();
        }
    }
    
    
    QPoint adaptedPosition(EventMK ev, QWidget *w)
    {
        if (w == nullptr)
            return QPoint(ev.p.x(), ev.p.y());
    
        int orig_w = ev.width;
        int orig_h = ev.height;
        int curr_w = w->width();
        int curr_h = w->height();
    
        int new_x = ev.p.x() * curr_w / orig_w;
        int new_y = ev.p.y() * curr_h / orig_h;
    
        return QPoint(new_x, new_y);
    }
    

    及功能实现

    void executeMousePressEvent(EventMK ev)
    {
        QWidget* widget = _getWidget(ev);
        if ( widget == nullptr )
        {
            qDebug()<<"error: "<<__LINE__<<__FUNCTION__;
            return;  
        }
    
       // _preExecutionWithMouseMove(ev, widget);
        if (widget){
            QTest::mousePress ( widget, (Qt::MouseButton)ev.button , 
                                        (Qt::KeyboardModifier) ev.modifiers, 
                                         adaptedPosition(ev,widget));
    
        }
    
        _postExecution(ev, widget);
    }
    

    现在要填充 struct EventMK ,您需要从 MouseButtonPress 事件中填充它。 这是我的例子

     bool eventFilter(QObject *obj, QEvent *event)
        {
            ///
            /// process control
            ///
    
            //window events
            if (event->type() == QEvent::KeyPress)
            {
                handleKeyPressEvent(obj, event);
            }
            //mouse events
            else if (event->type() == QEvent::MouseButtonPress)
            {
                handleMousePressEvent(obj, event);
            }
            else if (event->type() == QEvent::MouseButtonRelease)
            {
                handleMouseReleaseEvent(obj, event);
            }
            else if (event->type() == QEvent::MouseButtonDblClick)
            {
                handleMouseDoubleEvent(obj, event);
            }
            else if (event->type() == QEvent::Wheel)
            {
                handleWheelEvent(obj, event);
            }
            //keyboard events
            else if (event->type() == QEvent::Close)
            {
                handleCloseEvent(obj, event);
            }
    
            ///the event should continue to reach its goal...
            return false;
        }
    

    和

    void handleMousePressEvent(QObject *obj, QEvent *event)
      {
            QWidget *widget = isValidWidget(obj);
            if (!widget){
                return;
            }        
            QMouseEvent *me = dynamic_cast< QMouseEvent*> ( event );
            //create the event
            if (widget != nullptr){
                EventMK evkm;
    
                evkm.type = QOE_MOUSE_PRESS;    // set your type
                evkm._timer = _timer.restart(); // add your QElapsedTimer
                evkm.m_widPath = QWidgetUtils::getWidgetPath(widget);
                evkm. width  = widget->width();
                evkm. height = widget->height();
    
                QPoint p ( me->pos() );
                QPoint g = widget->mapToGlobal ( p );
                evkm. p  = p;
                evkm. g = g;
    
                evkm. button  = me->button();
                evkm. buttons = me->buttons();
                evkm. modifiers = me->modifiers();
    
                evkm.print();
            }
            //send event if EventMK is valid
    
        }
    

    所以,事实证明我们可以编写一个场景并运行你想要的,谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 2016-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多