如何创建 Qt-Quick 测试 [英] How to create a Qt-Quick Test

查看:24
本文介绍了如何创建 Qt-Quick 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have to create a Unit-Test.

But first, I´ve to get clear what to do. There is a QtQuick2-App written and now I would like to do Unit-Tests with the GUI. What are the steps for Unit-Tests with GUI? After reading the Qt-documents, I could not create any ideas for starting with the test.

Hope somebody can help me.

Edit: I was able to run some tests, after adding tst_button.qml and tst_test.cpp to my Project (main.cpp is in comments now). Is this the right way, or should I create a new project just for the Tests? If yes, what kind of project is needed? And the last question: Do I need to build up my MainForm for pressing buttons for example?

tst_button.qml

import QtQuick 2.4
import QtTest 1.0

Rectangle{
    id: myRec
    property var myMainForm: null

    TestCase{
        name:"ButtonClick"
        when:windowShown

        function test_init(){
           var createMyWindow = "import QtQuick 2.0; MainForm{id:myForm}"
           var myMainForm = Qt.createQmlObject(createMyWindow,myRec)
            myRec.myMainForm = myMainForm
        }
      }
  }

tst_test.cpp

#include <QtQuickTest/quicktest.h>
QUICK_TEST_MAIN(test)

解决方案

Testing and Debugging lists two ways:

You can use Qt Test for testing Qt Quick applications, but that's generally better for when you need access to C++ API that isn't available in QML.

Do I just add a *.qml file to my project and fill it with my code? If yes, what do I have to do to start the test?

You'll first need to make the tests a separate project, unless you're planning on using qmltestrunner (I have no idea why that tool isn't documented by Qt itself).

The Running Tests section of Qt Quick Test's documentation details how to get a test up and running.


I was able to run some tests, after adding tst_button.qml and tst_test.cpp to my Project (main.cpp is in comments now). Is this the right way, or should I create a new project just for the Tests?

If your application is pure QML and only intended to be run with qmlscene, for example, then doing it that way is fine. However, if you intend to deploy/ship your application, you'll probably need to have an executable, which means making separate projects for the application and the tests.

If yes, what kind of project is needed?

You could have a SUBDIRS project, so that your tests and the application itself can all be opened at once in Qt Creator. Something like this:

myapp.pro
app/
    main.cpp
    app.pro
    resources.qrc
    main.qml
tests/
    tests.pro
    data/
        tst_stuff.qml

And the last question: Do I need to build up my MainForm for pressing buttons for example?

No. The .ui feature is just a format that allows Qt Creator to enforce certain constraints to make it easier to design Qt Quick UIs with Qt Quick Designer. MainForm.ui.qml is therefore just a convenience. If you already have an existing component in QML, you can create instances of that and test it.

这篇关于如何创建 Qt-Quick 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆