How to add tests to an existing project?
-
@VRonin
Am I able to run any test I want at any time?
E.g. All of the unit tests or just 1 of them ?
Is it doable like Java and Junit in the Intelij IDE?
What I mean is that the IDE has good support for JUnit, where I can click a button close to where I set breakpoints and run that particular test.
Also would just 1 test project be enough be multiple cpp test files?
It seems a little messy in terms of project structure to have 1 auto test project for each class/file?I imagined my test structure to be a little like
MyProject/tests/QtTests/
And inside here I can have a
windowTest.cpp
,canvasTest.cpp
andmapTest.cpp
. Along with aCMakeLists.txt
.
But when I tried to create mutltiple test projects, it's more likeMyProject/tests/QtTests/WidowClassTest MyProject/tests/QtTests/CanvasClassTest MyProject/tests/QtTests/MapClassTest
Each with their own
___Test.cpp
and aCMakeLists.txt
.
Is the 2nd way of doing things better? -
@jkwok678 said in How to add tests to an existing project?:
E.g. All of the unit tests or just 1 of them ?
Is it doable like Java and Junit in the Intelij IDE?In Qt Creator in the top left corner expand the combobox that says "projects" and select tests to run them
What I mean is that the IDE has good support for JUnit, where I can click a button close to where I set breakpoints and run that particular test.
No as far as I'm aware
It seems a little messy in terms of project structure to have 1 auto test project for each class/file?
Is the 2nd way of doing things better?Qt Test is designed to be used 1-project-per-test. You can build around it but, believe me, the results are sub-par.
But when I tried to create mutltiple test projects, it's more like
MyProject/tests/QtTests/WidowClassTest
MyProject/tests/QtTests/CanvasClassTest
MyProject/tests/QtTests/MapClassTestFolder structure has nothing to do with projects.
To test classesWindow
,Canvas
andMap
, you can create a folder structure like:- MyProject
- tests
- tst_window.cpp
- tst_canvas.cpp
- tst_map.cpp
- CMakeLists.txt
- tests
and the content of CMakeLists.txt would be:
cmake_minimum_required(VERSION 3.9) find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Test REQUIRED) find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Test Gui Widgets REQUIRED) macro(BasicTest TestName) set(targetName "tst_${TestName}") set(testProjectName "tst${TestName}") string(TOLOWER ${TestName} TestSourceFileName) add_executable(${targetName} "tst_${TestSourceFileName}.cpp") target_include_directories(${targetName} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) target_link_libraries(${targetName} PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Test Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::Widgets) set_target_properties(${targetName} PROPERTIES AUTOMOC ON AUTOUIC ON AUTORCC ON CXX_STANDARD 11 CXX_STANDARD_REQUIRED ON ) add_test(NAME ${testProjectName} WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" COMMAND $<TARGET_FILE:${targetName}>) endmacro() BasicTest(Window) BasicTest(Canvas) BasicTest(Map)
To add more tests just add 1 line at the end
- MyProject
-
@jkwok678 said in How to add tests to an existing project?:
What's that?
BasicTest
is the macro defined in the code snippet@VRonin said in How to add tests to an existing project?:
To test classes
Window
,Canvas
andMap
-
@jkwok678 said in How to add tests to an existing project?:
So is what you said earlier, with 1 test project, and 3 test files optimal?
This is not 1 test project. 1 CMakeLists.txt can create multiple projects, in this case I'm creating 1 project for each class to test, I'm just doing it in a single CMakeLists.txt file
-
@jkwok678 said in How to add tests to an existing project?:
So everytime I want to test another class, I should create a new autotest project with Qt Creator?
No. Say you now want to test the class
MyClass
. You'd add the fileMyProject/tests/tst_myclass.cpp
and appendBasicTest(MyClass)
to the snippet pasted above -
@jkwok678 Conceptually yes (Stackoverflow would say no because the technical terms you used are not very precise but high-level you got the concept)
P.S.
If you don't want unnecessary pain in the future with cross-platform support, keep your .c/.cpp/.h,/.hpp etc files lower-case only