Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

mock qml import



  • Hi,
    I'd like to know if it's possible to mock qml import.

    In a qml file I have this:
    import Operations 1.0

    This package is regstered in code like it :
    qmlRegisterType(AdditionModel, "Operations", 1, 0, "AdditionModel")

    AdditionModel is QAbstractModel but written in python and it works well.

    The thing is about testing. There is no python testrunner so I have to run qml tests via C++ but then I'cant register my custom type in tests.
    So I would need to mock this package Operations 1.0 in tests but I don't know how I can achieve this. A mock library in qml ? create a operation qml package in tests (I tried but failed this one) ? other ?

    Thank for help

    Jimmy


  • Lifetime Qt Champion

    Hi,

    What about pytest-qt ?



  • Yes pytest-pyqt is great for the python part, but it doesn"t handle qml.
    QML is very good supported in pyside2, only some few features are missing. Sadly the testrunner is one them.
    I can mock many things in qml which make it testable anyway but I'm stuck on mocking the import Operation 1.0


  • Lifetime Qt Champion

    Can you show how you are doing the mocking in C++ ?



  • That's the thing, I'cant :-) I have no knowledge in c++.

    to run my tests I just copy pasted some things I found/utogenerated by qt creator

    //main.cpp
    #include <QtQuickTest/quicktest.h>
    QUICK_TEST_MAIN(qmltests)
    
    //qml_tests.pro
    CONFIG += warn_on qml_debug  qmltestcase
    
    TEMPLATE = app
    
    SOURCES += \
        main.cpp
    
    RESOURCES += \
        ../../src/qml.qrc
    

    And and I run my tests with it and it works.
    Here is a log of the tests :

    2020-05-11T07:13:20.3049587Z ##### running: qmake -o /home/runner/work/MyCartable/MyCartable/build/qml_tests/Makefile tests/qml_tests/qml_tests.pro -spec linux-g++ CONFIG+=debug CONFIG+=qml_debug #####
    2020-05-11T07:13:20.3050377Z Info: creating stash file /home/runner/work/MyCartable/MyCartable/build/qml_tests/.qmake.stash
    2020-05-11T07:13:20.3051327Z ##### finished: qmake -o /home/runner/work/MyCartable/MyCartable/build/qml_tests/Makefile tests/qml_tests/qml_tests.pro -spec linux-g++ CONFIG+=debug CONFIG+=qml_debug  ==>>  OK  with return code 0 #####
    2020-05-11T07:13:20.3052018Z ##### running: make -C build/qml_tests #####
    2020-05-11T07:13:20.3052660Z make: Entering directory '/home/runner/work/MyCartable/MyCartable/build/qml_tests'
    2020-05-11T07:13:20.3054112Z g++ -c -pipe -g -Wall -Wextra -D_REENTRANT -fPIC -DQUICK_TEST_SOURCE_DIR='"/home/runner/work/MyCartable/MyCartable/tests/qml_tests"' -DQT_QML_DEBUG -DQT_QMLTEST_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_TESTLIB_LIB -DQT_CORE_LIB -DQT_TESTCASE_BUILDDIR='"/home/runner/work/MyCartable/MyCartable/build/qml_tests"' -I../../tests/qml_tests -I. -I../../../Qt/5.14.1/gcc_64/include -I../../../Qt/5.14.1/gcc_64/include/QtQuickTest -I../../../Qt/5.14.1/gcc_64/include/QtWidgets -I../../../Qt/5.14.1/gcc_64/include/QtGui -I../../../Qt/5.14.1/gcc_64/include/QtQml -I../../../Qt/5.14.1/gcc_64/include/QtNetwork -I../../../Qt/5.14.1/gcc_64/include/QtTest -I../../../Qt/5.14.1/gcc_64/include/QtCore -I. -isystem /usr/include/libdrm -I../../../Qt/5.14.1/gcc_64/mkspecs/linux-g++ -o main.o ../../tests/qml_tests/main.cpp
    2020-05-11T07:13:20.3055277Z /home/runner/work/MyCartable/Qt/5.14.1/gcc_64/bin/rcc -name qml ../../src/qml.qrc -o qrc_qml.cpp
    2020-05-11T07:13:20.3056696Z g++ -c -pipe -g -Wall -Wextra -D_REENTRANT -fPIC -DQUICK_TEST_SOURCE_DIR='"/home/runner/work/MyCartable/MyCartable/tests/qml_tests"' -DQT_QML_DEBUG -DQT_QMLTEST_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_TESTLIB_LIB -DQT_CORE_LIB -DQT_TESTCASE_BUILDDIR='"/home/runner/work/MyCartable/MyCartable/build/qml_tests"' -I../../tests/qml_tests -I. -I../../../Qt/5.14.1/gcc_64/include -I../../../Qt/5.14.1/gcc_64/include/QtQuickTest -I../../../Qt/5.14.1/gcc_64/include/QtWidgets -I../../../Qt/5.14.1/gcc_64/include/QtGui -I../../../Qt/5.14.1/gcc_64/include/QtQml -I../../../Qt/5.14.1/gcc_64/include/QtNetwork -I../../../Qt/5.14.1/gcc_64/include/QtTest -I../../../Qt/5.14.1/gcc_64/include/QtCore -I. -isystem /usr/include/libdrm -I../../../Qt/5.14.1/gcc_64/mkspecs/linux-g++ -o qrc_qml.o qrc_qml.cpp
    2020-05-11T07:13:20.3061628Z g++ -Wl,-rpath,/home/runner/work/MyCartable/Qt/5.14.1/gcc_64/lib -Wl,-rpath-link,/home/runner/work/MyCartable/Qt/5.14.1/gcc_64/lib -o qml_tests main.o qrc_qml.o   /home/runner/work/MyCartable/Qt/5.14.1/gcc_64/lib/libQt5QuickTest.so /home/runner/work/MyCartable/Qt/5.14.1/gcc_64/lib/libQt5Widgets.so /home/runner/work/MyCartable/Qt/5.14.1/gcc_64/lib/libQt5Gui.so /home/runner/work/MyCartable/Qt/5.14.1/gcc_64/lib/libQt5Qml.so /home/runner/work/MyCartable/Qt/5.14.1/gcc_64/lib/libQt5Network.so /home/runner/work/MyCartable/Qt/5.14.1/gcc_64/lib/libQt5Test.so /home/runner/work/MyCartable/Qt/5.14.1/gcc_64/lib/libQt5Core.so -lGL -lpthread   
    2020-05-11T07:13:20.3062393Z make: Leaving directory '/home/runner/work/MyCartable/MyCartable/build/qml_tests'
    2020-05-11T07:13:20.3063038Z ##### finished: make -C build/qml_tests  ==>>  OK  with return code 0 #####
    2020-05-11T07:13:20.3063755Z ##### running: /home/runner/work/MyCartable/MyCartable/build/qml_tests/qml_tests #####
    2020-05-11T07:13:20.3064445Z QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-runner'
    2020-05-11T07:13:20.3064842Z ********* Start testing of qmltests *********
    2020-05-11T07:13:20.3065487Z Config: Using QtTest library 5.14.1, Qt 5.14.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 5.3.1 20160406 (Red Hat 5.3.1-6))
    2020-05-11T07:13:20.3066025Z PASS   : qmltests::MovingText::initTestCase()
    2020-05-11T07:13:20.3071585Z PASS   : qmltests::MovingText::test_do_nothing_if_no_truncated()
    2020-05-11T07:13:20.3072198Z PASS   : qmltests::MovingText::test_init()
    2020-05-11T07:13:20.3072486Z PASS   : qmltests::MovingText::test_start_animation()
    2020-05-11T07:13:20.3072779Z PASS   : qmltests::MovingText::test_start_animation_and_stop()
    2020-05-11T07:13:20.3073060Z PASS   : qmltests::MovingText::cleanupTestCase()
    2020-05-11T07:13:20.3073633Z PASS   : qmltests::PageButton::initTestCase()
    2020-05-11T07:13:20.3073932Z PASS   : qmltests::PageButton::test_bground()
    2020-05-11T07:13:20.3074280Z PASS   : qmltests::PageButton::test_init_and_text()
    <<-CUT for bievity->>
    2020-05-11T07:13:22.6969723Z PASS   : qmltests::RecentsRectangle::initTestCase()
    2020-05-11T07:13:22.6970035Z PASS   : qmltests::RecentsRectangle::test_init()
    2020-05-11T07:13:22.6970294Z PASS   : qmltests::RecentsRectangle::cleanupTestCase()
    2020-05-11T07:13:22.6970551Z Totals: 139 passed, 0 failed, 0 skipped, 0 blacklisted, 7166ms
    2020-05-11T07:13:22.6970810Z ********* Finished testing of qmltests *********
    2020-05-11T07:13:22.6971074Z ##### finished: /home/runner/work/MyCartable/MyCartable/build/qml_tests/qml_tests  ==>>  OK  with return code 0 #####
    

    I can't import a file in tests where import Operations 1.0 stands.


  • Lifetime Qt Champion

    Take a look at the Executing C++ Before QML Tests part of the QtQuick Test documentation. I think that is what you are looking for.


Log in to reply