Important: Please read the Qt Code of Conduct -

QTestLib, how can I (unit) test some parts of my project if my includes are not relative?

  • Hi,
    I'd like to start writing some unit tests on my project but the problem is that the includes in the whole project are not relative.
    The structure looks like this:


    There are subfolders and many files inside the folders.

    I'd like to add a tests folder under src where I'd define all my tests that would not be included in the
    So I've created something like this:


    The problem is that will include some files from the project itself and I've includes issues as the root of Test1 is not the same the one of the project.
    Basically, if in a project file there is this include

    #include "folder1/subfolder11/file1.h

    Then it wouldn't compile in my Test1. a ../../ is missing as we're 2 level up from the root...
    Any idea how I could do?

    I guess I could cheat and move the 2 level down in the src folder but I find it kind of messy as I will have around 20 or more tests...
    What would be the normal way to go?

    PS: I don't want to make relative #include in my whole project, I find it less readable than specifying the full path

    PS2: I've just tried to include in


    Same problem, I've more than 1000 includes that are not found...

  • Lifetime Qt Champion


    You should rather create .pri files that help setup your other projects. For example, setting up the include paths.

  • @SGaist said in QTestLib, how can I (unit) test some parts of my project if my includes are not relative?:


    You should rather create .pri files that help setup your other projects. For example, setting up the include paths.

    Well I've tried to use INCLUDEPATH but within my and I still have the same issue.
    Here is what I've done:

    INCLUDEPATH += $$PWD/../../
    SOURCES -= main.cpp
    SOURCES += \
        main.cpp \
    HEADERS += \

    Would it change anything if it was a pri file instead of a pro? I guess it would do the same no?
    The issue is that when I'm running qmake on the, it is complaining that it doesn't find the headers used in the project because in the cpp files my includes are not relative.
    Do you see what I mean?

    What should I do? In my project file add the INCLUDEPATH to $$PWD doesn't help...

    PS: the only solution that I've working for now is to put the in the root at the same level than the
    But as I said, it bothers me a bit cause I will have more than 20 unit tests and I don't want to pollute the root folder with all those pro files...
    It is ok to have one main that would run all the unit tests but I'd like to be able to have separate pro file for each test so I could easily just launch one if needed.

  • Lifetime Qt Champion

    While they are technically the same thing but a .pro file as its name suggest is for project while a .pri file is for inclusion.

    You an have for example a folder1.pri file that would look like:

    // Other useful settings

    And you include folder1.pri in your and other tests/projects that will use it

  • Well that's what I'm doing, I've included the file in my
    But when I run qmake on the it is complaining on the compilation of the files included in the
    Here is a small example of what I have:
    alt text

    So my is:

    QT -= gui
    CONFIG += c++11 console
    CONFIG -= app_bundle
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which as been marked deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    SOURCES += \
    HEADERS += \
        folder1/a.h \


    QT += testlib
    QT -= gui
    INCLUDEPATH += $$PWD/../..
    SOURCES -=  main.cpp
    CONFIG += qt console warn_on depend_includepath testcase
    CONFIG -= app_bundle
    TEMPLATE = app
    SOURCES +=  tst_test1.cpp

    I've those warnings:

    :-1: warning: Failure to find: folder1/a.h
    :-1: warning: Failure to find: folder2/b.h

    that are just warnings but in my real project I've much more than that (hundreds of thousands) and one recurrent error on the qrc file

    WARNING: Failure to find: hmi/GUI/widget/SimulationWidget.ui
    /opt/Qt/5.10.1/gcc_64/bin/rcc: File does not exist 'resources/resources.qrc'

    what shall I do to avoid that?

  • Lifetime Qt Champion

    Don't include your project's .pro file. It's a complete project definition so right after that you have to modify things unrelated to your tests.

Log in to reply