Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is qmltestrunner?

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

like image 215
1218GG Avatar asked Oct 13 '16 08:10

1218GG


People also ask

What is a QT Test?

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.

How do you write a test case in Qt?

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.


2 Answers

  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 *********
like image 85
mcchu Avatar answered Sep 22 '22 14:09

mcchu


  • http://doc.qt.io/qt-5/qtquick-qtquicktest.html This will help in understanding qmltest runner
  • http://doc.qt.io/qt-5/qml-qttest-signalspy.html This will help in understanding signal spy which is used to catch signals
  • http://doc.qt.io/qt-5/qml-qttest-testcase.html This will help in writing each test case
like image 38
mani deepak Avatar answered Sep 22 '22 14:09

mani deepak