Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How do I unit test my code?



  • I am very, very new to C++ and even newer to QT Creator. Literally installed it yesterday.

    Despite my best efforts, I cannot figure out how to run unit tests on my project.

    My project was created within QT Creator by going to File -> New File or Project -> QT Console Application

    This created the following:

    * FirstProject
    | -> FirstProject.pro
    | -> * Sources
         |-> main.cpp
    

    Here's the contents of FirstProject.pro:

    QT -= gui
    
    CONFIG += c++11 console
    CONFIG -= app_bundle
    DEFINES += QT_DEPRECATED_WARNINGS
    
    SOURCES += \
            main.cpp
    
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    Here's the contents of main.cpp:

    #include <QCoreApplication>
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <QDebug>
    #include <vector>
    
    integer calculate(){
        return (4 + 3);
    };
    
    int main(int argc, char *argv[]){
        QCoreApplication a(argc, argv);
    
        calculate();
    
        return a.exec();
    }
    

    How should I test that calculate returns 7? What files do I create? Once I've created a file containing my test code, how do I include my calculate function in the test? Should it be inside a class for easy access? How do I run these tests from within QTCreator? I don't mind what testing framework, QtTest seems fine.

    Documentation such as this https://doc.qt.io/qt-5/qtest-overview.html does not go over it step by step, does not cover how to add tests to an existing project.





  • @pablo-j-rogina I have three main problems. (1) how should the directory structure of my project look (2) how do I include the project files in the test files (3) how do I run the tests. I.e. what button do I physically click in QT Creator

    Your link details how to write individual test files rather than set up a project for testing. I am a complete beginner, though I have PHP experience.

    If I could see a youtube video of someone setting up a project for unit testing in QT Creator I'm sure I'd be fine. Or if someone could spell it out ;)


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should follow the structure like done in the Qt sources.

    Use a SUBDIRS project so you can build everything separately correctly and also have the targets available to run from Qt Creator or from the command line.



  • @sgaist Thankyou. I've done this...but how do I include the class header from one subproject into the tests from the other subproject? Is it done in one of the .pro files?


  • Lifetime Qt Champion

    The most simple is to create a .pri file that handles the file part (for example SOURCES and HEADERS variables) and that you can include in both the test and main project .pro files.


Log in to reply