How to use an auto test project to test another project
-
I currently have created a normal project with all my source code at the root.
At the root I've also created a test project.
But when I try to test classes that I've made at the root.
I am unable to do so because I am able to use#include "../map.h"
, for some reason I am unable to find#include <QWidget>
in themap.h
file.
My test project is at
MyProject/Qt_Tests
while my classes that I want to test are at
MyProject
I am currently using CMake.
Is there something in CMake I need to add so that I can access the files in the main project. -
Hi,
You should show the content of your CMakefile.
-
@SGaist
Here it is for my window.h, but same problemcmake_minimum_required(VERSION 3.5) project(WindowFunction LANGUAGES CXX) find_package(QT NAMES Qt5 Qt6 COMPONENTS Test REQUIRED) find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Test REQUIRED) find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED) find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) enable_testing() add_executable(WindowFunction tst_windowfunction.cpp) add_test(NAME WindowFunction COMMAND WindowFunction) target_link_libraries(WindowFunction PRIVATE Qt${QT_VERSION_MAJOR}::Test)
-
Why are you just linking QtTest ?
-
@SGaist
Ah I gotcha.
I fixed the last bit with
target_link_libraries(WindowFunction PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
.
But now I have
undefined reference to Window::Window()
Qt creator autocompletion seems to be able to find my functions however.
#include "../window.h" void WindowFunction::test_case1() { Window win; win.convertDistances(); } QTEST_APPLESS_MAIN(WindowFunction) #include "tst_windowfunction.moc"
-
You have 2 solutions:
- Add the source files of the main application to the test project (
add_executable(WindowFunction tst_windowfunction.cpp ../window.h ../window.cpp)
) - Split your main project in 2:
- A static library with all your classes
- An executable that contains only the
main.cpp
and links to the static lib
This way you can have the test project link to the static library too.
My recommendation is the second option. You can see a very basic, commented, example of that here
- Add the source files of the main application to the test project (
-
@jkwok678 said in How to use an auto test project to test another project:
Hmmm, was this the intended way to build a project and tests from the devs?
QtCreator itself uses option 2 for its tests
From KDE (KDE and Qt are tightly related and developers often overlap)
Example of option 1 : https://github.com/KDE/ktexteditor
Example of option 2 https://github.com/KDE/korganizer