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)
Writing a Test#include <QTest> class TestQString: public QObject { Q_OBJECT private slots: void toUpper(); }; Note: You need to include the QTest header and declare the test functions as private slots so the test framework finds and executes it. The QVERIFY() macro evaluates the expression passed as its argument.
For simple UI files such as this one, select File > New File or Project > Application (Qt Quick) > Qt Quick Application - Empty from within Qt Creator. Pressing the green Run button runs the application. You should see the text Hello, World! in the center of a red rectangle.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With