I am trying to create unit tests for a practice Qt Quick QML project on Qt 5.12 using Qmake configuration on ubuntu and I am running into problem on how to make my test files able to find and import my custom components in order to test for them.

In my Tests subdir, the .pro file looks like this:

CONFIG += warn_on qmltestcase testlib

INCLUDEPATH = $$PWD/../app

IMPORTPATH += $$PWD/../app

QML_IMPORT_PATH += $$PWD/../app

TEMPLATE = app

TARGET = tst_example

DISTFILES += \
    tst_MoviePlayer.qml

SOURCES += \
    main.cpp

As you can see, I tried INCLUDEPATH, IMPORTPATH, and QML_IMPORT_PATH to point at the parent-directory/app folder where the custom component .qml is at.

The structure is like this:

|- MyProject.pro
    | - app
        |- main.qml
        |- main.pro
        |- main.cpp
        |- controller.cpp
        |- controller.h
        |- MoviePlayer.qml
        |- qml.qrc
    | - tests
        |- test.pro
        |- main.cpp
        |- tst_MoviePlayer.qml

controller.h & controller.cpp contain some simple backend logic that MoviePlayer.qml uses.

When I go in tst_MoviePlayer.qml and try this:

TestCase {
    function test_MoviePlayer() {
        var moviePlayer = createTemporaryQmlObject("import MoviePlayer 1.0; MoviePlayer {}", this)
    }
}

It says MoviePlayer module is not installed. How do I properly make it so that my test subdir files can properly find the custom component on the other folder?

I also tried just createTemporaryQmlObject("MoviePlayer {}", this) and get "MoviePlayer is not a type" error so it for sure is not able to find it somehow.

EDIT: I added a .qrc file with the following structure and I was able to create a MoviePlayer type inside my tests subdir, however I still cannot createTemporaryQmlObject because it still says 'MoviePlayer is not a type' even though I can declare a MoviePlayer type just fine in the very same file?

<RCC>
    <qresource prefix="/">
        <file>tst_MoviePlayer.qml</file>
        <file alias="MoviePlayer.qml">../app/MoviePlayer.qml</file>
    </qresource>
</RCC>
0

There are 0 best solutions below