- Unfortunately there's no documentation for qmltestrunner (I cannot one). If you only want to know how to use it,
qmltestrunner.exe -h
may help you. Most options are described in Qt Test Overview.
- Yes. Qt Quick Test Reference Documentation - Running Tests says you need a .cpp file that contains
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.
- To run qmltestrunner (in Windows with Qt 5.7, for example), you need at least the following modules:
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 *********