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
    MyApp------------------->common
         |                      |--->util
         |                      |     |--->test
         |                      |
         |                      |--->test
         |
         |------------>algorithm
         |                  |--->test
         |
         |------------->models
         |               |--->test
         |
         |------------>views
         |               |--->test
         |
    UnitTesting
    

    So my intent is to have a UnitTesting.pro 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:

    <scandrive.h>

    //Paths relative to location of <Root>/MyApp/MyApp.pro
    
    #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"

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

    Ideally all the test projects should have the include path:
    <Root-Path>/MyApp/

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

    Any ideas ?


  • Moderators

    @idlefrog Why don't you simply define INCLUDEPATH in UnitTesting.pro?
    Like

    INCLUDEPATH += $$PWD/MyApp
    


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

    <UnitTests.pro>

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

    <TestModels.pro> 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 +=\
                ../common/io.h
    

    Running qmake on my UnitTesting.pro file,
    It outputs for UnitTests.pro

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

    For TestModels.pro

    Project MESSAGE: Path: []
    

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


  • Moderators

    @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

    Hi,

    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/test.pro 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 test.pro 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>
    INCLUDEPATH += $$SRC_PATH
    DEPENDPATH += $$SRC_PATH
    

    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:

    //structure
    App
      |----src--------------
            |             
            |---------common
            |         |
            |         util.h util.cpp
            |         matrix.h matrix.cpp
            |---------algorithm
            |---------models
            |---------views
            testing 
              |---------common
              |          |
              |          |          
              |          |---------util
              |          |          |
              |          |      util.pro (the unit test project)
              |          |--------matrix
              |          |          |
              |          |          matrix.pro( the unit test project)
              |         common.pro
              |         
              |---------algorithm
              |---------models
              |---------views
              |             
              |
              testing.pro
                             
    

    Then all I do is type in the test directory containing testing.pro
    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.