Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set the include path for test subprojects? (QtTest)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    idlefrog
    wrote on 9 Feb 2018, 07:49 last edited by idlefrog 2 Sept 2018, 11:22
    #1

    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 ?

    J 1 Reply Last reply 9 Feb 2018, 08:23
    0
    • I idlefrog
      9 Feb 2018, 07:49

      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 ?

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 9 Feb 2018, 08:23 last edited by
      #2

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

      INCLUDEPATH += $$PWD/MyApp
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • I Offline
        I Offline
        idlefrog
        wrote on 9 Feb 2018, 09:13 last edited by idlefrog 2 Sept 2018, 10:36
        #3

        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.

        J 1 Reply Last reply 9 Feb 2018, 09:47
        0
        • I idlefrog
          9 Feb 2018, 09:13

          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.

          J Online
          J Online
          jsulm
          Lifetime Qt Champion
          wrote on 9 Feb 2018, 09:47 last edited by
          #4

          @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 :-)

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • I Offline
            I Offline
            idlefrog
            wrote on 9 Feb 2018, 10:37 last edited by idlefrog 2 Sept 2018, 10:59
            #5

            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.
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 9 Feb 2018, 22:10 last edited by
              #6

              Hi,

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • I Offline
                I Offline
                idlefrog
                wrote on 13 Feb 2018, 11:30 last edited by
                #7

                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?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 13 Feb 2018, 21:17 last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    idlefrog
                    wrote on 15 Feb 2018, 10:42 last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      idlefrog
                      wrote on 10 Jan 2022, 16:04 last edited by idlefrog 1 Dec 2022, 10:13
                      #10

                      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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved