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 do I unit test my code?
Forum Update on Monday, May 27th 2025

How do I unit test my code?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.6k Views
  • 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.
  • C Offline
    C Offline
    CheekiBreeki95
    wrote on 6 Sept 2019, 14:02 last edited by CheekiBreeki95 9 Jun 2019, 14:10
    #1

    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.

    P 1 Reply Last reply 6 Sept 2019, 14:38
    0
    • C CheekiBreeki95
      6 Sept 2019, 14:02

      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.

      P Offline
      P Offline
      Pablo J. Rogina
      wrote on 6 Sept 2019, 14:38 last edited by
      #2

      @cheekibreeki95 what about the Qt Test tutorial?

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      C 1 Reply Last reply 6 Sept 2019, 14:48
      1
      • P Pablo J. Rogina
        6 Sept 2019, 14:38

        @cheekibreeki95 what about the Qt Test tutorial?

        C Offline
        C Offline
        CheekiBreeki95
        wrote on 6 Sept 2019, 14:48 last edited by CheekiBreeki95 9 Jun 2019, 14:49
        #3

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 6 Sept 2019, 14:56 last edited by
          #4

          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.

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

          C 1 Reply Last reply 7 Sept 2019, 15:34
          1
          • S SGaist
            6 Sept 2019, 14:56

            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.

            C Offline
            C Offline
            CheekiBreeki95
            wrote on 7 Sept 2019, 15:34 last edited by
            #5

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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 7 Sept 2019, 18:24 last edited by
              #6

              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.

              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

              1/6

              6 Sept 2019, 14:02

              • Login

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