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. QTestLib, how can I (unit) test some parts of my project if my includes are not relative?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 672 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.
  • mbruelM Offline
    mbruelM Offline
    mbruel
    wrote on last edited by mbruel
    #1

    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:

    src
        folder1
        folder2
        project.pro
    

    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 project.pro.
    So I've created something like this:

    src
        folder1
        folder2
        project.pro
        tests
            Test1
                test1.pro
    

    The problem is that test1.pro 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 test1.pro 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 project.pro in test1.pro:

    include(../../project.pro)
    

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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

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

      mbruelM 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

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

        mbruelM Offline
        mbruelM Offline
        mbruel
        wrote on last edited by mbruel
        #3

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

        Hi,

        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 test.pro and I still have the same issue.
        Here is what I've done:

        INCLUDEPATH += $$PWD/../../
        include(../../project.pro)
        SOURCES -= main.cpp
        
        SOURCES += \
            main.cpp \
            Test.cpp
        
        HEADERS += \
            Test.h
        

        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 test.pro, 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 test.pro in the root at the same level than the project.pro.
        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 test.pro 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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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:

          INCLUDEPATH += $$PWD
          // Other useful settings
          

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

          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
          3
          • mbruelM Offline
            mbruelM Offline
            mbruel
            wrote on last edited by
            #5

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

            So my project.pro 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.
            DEFINES += QT_DEPRECATED_WARNINGS
            
            # 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
            
            INCLUDEPATH += $$PWD
            
            SOURCES += \
                    main.cpp 
            
            HEADERS += \
                folder1/a.h \
                folder2/b.h
            

            the Test1.pro

            QT += testlib
            QT -= gui
            
            INCLUDEPATH += $$PWD
            INCLUDEPATH += $$PWD/../..
            
            include(../../proInclude.pro)
            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?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              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
              3

              • Login

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