How to create a Qt-Quick Test

一笑奈何 提交于 2019-11-28 07:51:20

问题


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)

回答1:


Testing and Debugging lists two ways:

  • Qt Test (also known as testlib) - a framework for unit tests of C++ code
  • Qt Quick Test - a framework for unit tests of QML code

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.



来源:https://stackoverflow.com/questions/33210134/how-to-create-a-qt-quick-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!