I have gone through the documentation provided by Qt on TestCase, Qt Quick Test Reference Documentation, Ubuntu QML unit testing, Testing with qmltestrunner part 1 & 2, Writing and running qml testcases, How to create a Qt-Quick Test
All that I have found about it is:
Qmltestrunner is a tool used for unit testing. This tool allows to execute QML files as test cases. These files should contain test_functions. Qmltestrunner is an open-source project and its source code can be found from the github.
But there are few questions for which I'm looking out for answers:
qmltestrunner documentation? where can I find it? (Could not find wiki page for it)
Is qmltestrunner part of qt quick test framework?
What all dependencies are there for qmltestrunner?
Is there any proper example where I can find complete explanation about QML unit testing? qt quick test framework explains running-tests which I couldn't understand.
Thank you
Qt Test is a framework for unit testing Qt based applications and libraries. Qt Test provides all the functionality commonly found in unit testing frameworks as well as extensions for testing graphical user interfaces.
For a Qt test, select the GUI Application check box to create a Qt application. In the Test case name field, enter a name for the test case. For a Qt test, select the Requires QApplication check box to add the include statement for QApplication to the main. cpp file of the project.
qmltestrunner.exe -h
may help you. Most options are described in Qt Test Overview.QUICK_TEST_MAIN(xxx)
and a .pro file that contains CONFIG += qmltestcase
, and build this project to run your QML unit tests. The output binary file of this project is (almost the same as) qmltestrunner.Qt5Core.dll
, Qt5Gui.dll
, Qt5Network.dll
, Qt5Qml.dll
, Qt5Quick.dll
, Qt5QuickTest.dll
, Qt5Test.dll
, Qt5Widget.dll
. And some extra modules for your QML files if needed (ex. QtQuick Controls 2)TestCase
describes how to write a unit tests in QML. To run the file, simply run qmltestrunner.exe -input C:\My\Testing\File\Path\tst_myComponentTest.qml
in command line.Here's simple step-by-step example about how to write a QML component with unit tests. For example, assume that we have a ExpandButton
that expands when it is clicked:
//ExpandButton.qml
import QtQuick 2.7
import QtQuick.Controls 1.2
Button {
width: 50; height: 50
onClicked: { width = 100; }
}
To test this behavior, write a tst_ExpandButton.qml
:
import QtQuick 2.7
import QtTest 1.0
Item {
width: 800; height: 600
ExpandButton {
id: expandButton
anchors.centerIn: parent
}
TestCase {
name: "ExpandButton"; when: windowShown
function test_clickToExpand() {
var widthBeforeClick = expandButton.width;
mouseClick(expandButton);
var widthAfterClick = expandButton.width;
verify(widthBeforeClick < widthAfterClick);
}
}
}
Now we have two QML files, ExpandButton.qml
and tst_ExpandButton.qml
. Run the unit test with qmltestrunner.exe -input D:\aaa\bbb\tst_ExpandButton.qml
and you can see the result:
********* Start testing of qmltestrunner *********
Config: Using QtTest library 5.7.0, Qt 5.7.0 (i386-little_endian-ilp32 shared (dynamic) release build; by MSVC 2015)
PASS : qmltestrunner::ExpandButton::initTestCase()
PASS : qmltestrunner::ExpandButton::test_clickToExpand()
PASS : qmltestrunner::ExpandButton::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 13ms
********* Finished testing of qmltestrunner *********
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