Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

user interface - How to create a Qt-Quick Test

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...