How to create a Qt-Quick Test

I need to create Unit-Test.

But first I have to figure out what to do. The QtQuick2 application is written, and now I would like to run Unit-Tests with a graphical interface. What are the steps for Unit-Tests with a GUI? After reading the Qt documents, I could not create any ideas for starting the test.

Hope someone can help me.

Change I managed to run several tests after adding tst_button.qml and tst_test.cpp to my project (now there are comments in main.cpp). Is this correct, or should I create a new project just for tests? If so, which project is needed? And the last question: do I need to create my MainForm for button clicks, 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) 
+5
source share
1 answer

Testing and debugging contains two methods:

You can use Qt Test to test Qt Quick applications, but it's usually better when you need access to the C ++ API, which is not available in QML.

Am I just adding a * .qml file to my project and filling it with my code? If so, what do I need to do to start the test?

First you need to make the tests a separate project, if you do not plan to use qmltestrunner (I have no idea why this tool is not documented by Qt itself).

The Running Tests section of the Qt Quick Test documentation details how to pass the test and run it.


I managed to run several tests after adding tst_button.qml and tst_test.cpp to my project (main.cpp is now in the comments). Is this correct, or should I create a new project just for tests?

If your application is pure QML and is intended, for example, to run with qmlscene , then do it in order. However, if you intend to deploy / submit your application, you will probably need an executable file, which means creating separate projects for the application and tests.

If so, which project is needed?

You may have a SUBDIRS project SUBDIRS that your tests and the application itself can be immediately opened in Qt Creator. Something like that:

 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 button clicks, for example?

No. The .ui function is simply a format that allows Qt Creator to apply certain restrictions to simplify the development of Qt Quick UI with Qt Quick Designer. MainForm.ui.qml is just a convenience. If you already have an existing component in QML, you can instantiate it and test it.

+5
source

Source: https://habr.com/ru/post/1234010/


All Articles