CMake Build Type specified Building
Solved
General and Desktop
-
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, thenfind_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)
-
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.
-
Hi,
Don't mix your test code within your application code. Tests should be in a dedicated folder.