Important: Please read the Qt Code of Conduct -

How to set the include path for test subprojects? (QtTest)

  • Hello all,
    The frog is stuck!

    This should be an easy one!
    So in my software project I have one main executable. It is made up of various components, in various sub directories.
    Each sub directory has a test project.

    Here's the structure:

    //Directory Structure
         |                      |--->util
         |                      |     |--->test
         |                      |
         |                      |--->test
         |                  |--->test
         |               |--->test
         |               |--->test

    So my intent is to have a project (in UnitTesting) that compiles all my tests, and run them together.
    However here's the problem:
    Each test Makefile uses a include path relative to the test directory.

    This means that if I try to compile the test ins MyApp/models/test, it does not know where to find the headers in MyApp/common. Instead every header would have to be referenced as '../../common', and then the headers in common would then also need to changed! For example, in my models directory I would have for example:


    //Paths relative to location of <Root>/MyApp/
    #include "common/scan.h"
    #include "common/util.h"
    #include "common/base/io.h"
    class ScanDrive

    But the test in models/test does not have the include path <Path>/MyApp/
    So it can never find "common/base/io.h"

    Instead the compiler looks for the file "<Path>/MyApp/models/test/common/base/io.h"

    Well, I did have the idea of setting a new variable in the called INCLUDE_ROOT that would be shared by all the test subprojects. But setting it in does not mean the subprojects share it.

    Ideally all the test projects should have the include path:

    How do you get all the subproject test projects to share this common include path?

    Any ideas ?

  • Lifetime Qt Champion

    @idlefrog Why don't you simply define INCLUDEPATH in


  • So UnitTesting has the path MyApp/UnitTesting
    My files looks like this:


    SUBDIRS += \
    INCLUDEPATH += $$PWD/../
    message("Path: ["$${INCLUDEPATH}"]")

    <> in 'models/test'

    Qt += testlib
    Qt -=gui
    message("Path : ["$${INCLUDEPATH}"]")
    TARGET = tst_testModelA
    CONFIG += console testcase
    CONFIG -= app_bundle
    TEMPLATE = app
    SOURCES += tst_testModelA.cpp
    HEADERS +=\

    Running qmake on my file,
    It outputs for

    Project MESSAGE: Path: [<root>/UnitTests/..]


    Project MESSAGE: Path: []

    So INCLUDEPATH is not shared between the root project, and the subprojects.

  • Lifetime Qt Champion

    @idlefrog said in How to set the include path for test subprojects? (QtTest):

    message("Path : ["$$INCLUDEPATH}"]")

    Please take a closer look at this line - you have a bug there :-)

  • Just a typo!..corrected.
    Looking at the compile line I see the include path is just the current directory:

    g++ -c -I. -o tst_testModelA.o tst_testModelA.cpp
    tst_testModelA.cpp:<line> error: common/base/io.h: No such file of directory.

  • Lifetime Qt Champion


    For that kind of common stuff, you can use a .pri file that you include in your tests .pro file.

  • Thanks for the response SGaist. It has helped!!!!
    I think you are suggesting that:

    • The UnitTests directory should have a common.pri file.
    • The common.pri would contain:
      INCLUDEPATH += ../
      Which points to the root path of MyApp
    • The project file models/test/ would contain:
      include(<path to common.pri>/common.pri)

    That works, so thanks for the pointer, although is there some sort hand way to write <path to common.pri>? e.g.
    include ($$TOP_LEVEL_PRO_FILE_PATH/common.pri)
    rather than
    include (../../../../../../../../../../../common.pri)
    for the those deeply set test sub projects :)

    It means that sub directories can't be moved easily without having to rewrite all the files.
    Or have I got myself in a muddle here?

  • Lifetime Qt Champion

    One way is to have a .pri file that you include in all your tests that is at the root folder of your tests. In this one you can include other .pri files so that you can move things around more easily.

  • There was some reference about setting a top source variable, but that did not work.

    As found out some years ago... you have to compile twice(for me)/(or 3 times?) to get the top directory values!

    So in the end I decided to keep it simple.
    By just adding three extra lines in each unit test sub project:

    SRC_PATH = <path to root source code>

    Ok, the path to the root can end up with a log of '../../../../' etc, but at least it works! ;)
    Thanks SGaist for your suggestions :)

    And the directory structure was more like this:

            |         |
            |         util.h util.cpp
            |         matrix.h matrix.cpp
              |          |
              |          |          
              |          |---------util
              |          |          |
              |          | (the unit test project)
              |          |--------matrix
              |          |          |
              |          | the unit test project)

    Then all I do is type in the test directory containing
    make check and all the sub projects just run.
    In truth I got the idea by looking at GitHub and seeing how Qt source code is tested.

  • Update: Using the Qt5 solution for accessing the root source tree path works very well.
    This means that each sub test project has access to the source root directory via a simple variable, ie. $$top_srcdir

Log in to reply