Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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:

    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...


  • Lifetime Qt Champion

    Hi,

    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?:

    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.


  • 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:

    INCLUDEPATH += $$PWD
    // Other useful settings
    

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



  • 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?


  • 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