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. CMake Build Type specified Building

CMake Build Type specified Building

Scheduled Pinned Locked Moved Solved General and Desktop
cmakeqtesttestingunit testqt6
4 Posts 3 Posters 713 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.
  • K Offline
    K Offline
    Kevin470
    wrote on 18 Oct 2022, 13:33 last edited by Kevin470
    #1

    I am learning how to run unit tests with Qt Test.
    I see that it is possible to disable code from the main.cpp like below.

    #include "mainwindow.hpp"
    #include <QApplication>
    
    #ifdef QT_DEBUG
        #include <QTest>
    #endif
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
    #ifdef QT_DEBUG
        QTest::qExec(&w);
    #endif
        w.show();
        return a.exec();
    }
    
    

    Is it possible to specify what the CMake builds similarly?
    For example, if I am building with the Debug Kit, then

    find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Test)
    find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Test)
    target_link_libraries(WindowTest PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Test)
    
    

    And for Release,

    find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
    find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
    target_link_libraries(WindowTest PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
    
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on 18 Oct 2022, 14:15 last edited by mchinand
      #2
      if (CMAKE_BUILD_TYPE MATCHES Debug)
         find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Test)
         find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Test)
         target_link_libraries(WindowTest PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Test)
      else()
         find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
         find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
         target_link_libraries(WindowTest PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
      endif()
      

      Just as a note, you should run tests on release builds too.

      1 Reply Last reply
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 18 Oct 2022, 15:21 last edited by
        #3

        Hi,

        Don't mix your test code within your application code. Tests should be in a dedicated folder.

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

        K 1 Reply Last reply 19 Oct 2022, 06:24
        2
        • S SGaist
          18 Oct 2022, 15:21

          Hi,

          Don't mix your test code within your application code. Tests should be in a dedicated folder.

          K Offline
          K Offline
          Kevin470
          wrote on 19 Oct 2022, 06:24 last edited by
          #4

          @SGaist @mchinand Thank you so much. Unit testing is a new concept for me and I am trying to learn how to do it and best practices.

          1 Reply Last reply
          0

          2/4

          18 Oct 2022, 14:15

          • Login

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