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

QML testing



  • Hello,

    I am trying to unit test QML for the first time. I want to test a component that exists in my application. am following the advice in the documentation to use createTemporaryQmlObject but I am getting an error when the test runs that my component is not a type.

    I have put my skeleton QML test in the same directory structure as my existing C++ unit tests (in a new subdirectory) so I have something roughly like this:

    app/
          qml/
                ...
                MyComponent.qml
                ...
    ...
    tests/
        ...
        TestQML/
              TestMyComponent/
                    TestMyComponent.pro
                    tst_mycomponent.cpp
                    tst_MyComponent.qml
    

    If I only put simple Javascript in my tst_MyComponent.qml file (i.e. I don't yet try to reference MyComponent), everything builds and runs OK. I assume that the problem is that I am not properly specifying MyComponent.qml as a dependency of the test project.

    I have tried setting IMPORTPATH += $$PWD/../../../app/qml in my TestMyComponent.pro file. I have also tried specifying the file path as the third argument of createTemporaryQmlObject but neither is working. However, I don't know what format that file path should be in. Is it sufficient to specify it as a relative path "../../../app/qml/IterationAxis.qml"?

    If I specify the IMPORTPATH only I get this error:

    `file:///C:/Qt/5.9.6/msvc2017_64/qml/QtTest/undefined:1:21: MyComponent is not a type`
    

    If I specify the path argument only, I get this error:

    `file:///C:/Qt/app/qml/MyComponent .qml:1:21: MyComponent is not a type`
    

    I have also tried including the file under test in the test project explicitly via "Add existing files..." in QT creator. This adds it as a relative path in DISTFILES in the test project's .pro file. This gives me the error:

    `file:///C:/Qt/5.9.6/msvc2017_64/qml/QtTest/undefined:1:21: MyComponent is not a type`
    

    Any suggestions please?



  • This StackOverflow reply suggests a layout very similar to what I have so I guess I am not trying to do something unreasonable.

    Essentially, the question comes down to: how do I ensure that my QML files under test can be found when the test runs?


Log in to reply