5

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:

  1. qmltestrunner documentation? where can I find it? (Could not find wiki page for it)

  2. Is qmltestrunner part of qt quick test framework?

  3. What all dependencies are there for qmltestrunner?

  4. 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

Community
  • 1
  • 1
1218GG
  • 393
  • 3
  • 11

2 Answers2

5
  1. 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.
  2. 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.
  3. 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)
  4. 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 *********
mcchu
  • 3,309
  • 1
  • 20
  • 19
  • Is there a way for a test to run the existing app, perform some interaction with the app and then do some asserts? Here, you have demonstrated how to test a button, a very small segment of a project. – AndroC Apr 23 '19 at 10:13
  • 1
    With qmltestrunner, you can use [`tryCompare`](https://doc.qt.io/qt-5/qml-qttest-testcase.html#tryCompare-method) or [`SignalSpy.wait`](https://doc.qt.io/qt-5/qml-qttest-signalspy.html#wait-method) in your test cases. The assertion will wait until the expected condition is reached. But if you want to verify an existing app, find other test framework instead. qmltestrunner is designed for a very small segment of a project – mcchu Apr 25 '19 at 02:20
  • When run qmltestrunner tst_compare.qml in qt5/qtdeclarative/tests/auto/qmltest/selftests I received the error: module "QtTest" is not installed import QtTest 1.1 ^ module "QtQuick" is not installed import QtQuick 2.0 ^ Can you help me with it? – Trung Nguyen Oct 12 '21 at 04:22
1
mani deepak
  • 401
  • 1
  • 4
  • 15
  • Thank you for your reply. But if you notice I have already mentioned that, i have gone through all these documentations read each and every word of it. And my question is about qmltestrunner. If you could help me for the same then it would be great. – 1218GG Oct 14 '16 at 04:04
  • I wrote my qmltestrunner codes using the above documentation. For your 2nd que, ans is yes. For 3rd que, can you be more specific? For 4th question, I used the same provided in the example (http://doc.qt.io/qt-5/qml-qttest-testcase.html) – mani deepak Oct 14 '16 at 05:25