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 to add tests to an existing project?

How to add tests to an existing project?

Scheduled Pinned Locked Moved Solved General and Desktop
qt testgoogle testtesting
19 Posts 4 Posters 4.5k 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.
  • J Offline
    J Offline
    jkwok678
    wrote on 28 Jul 2021, 19:00 last edited by
    #1

    Hi,

    I would like to use Qt test or google test.
    How does the folder structure look when a test is added to an existing project/ repository?
    Do I just add a new Auto test project?
    Are there any tutorials/ YouTube videos that explain the process of adding Qt tests/ google test to a Qt project using Qt Creator?

    1 Reply Last reply
    0
    • J jkwok678
      29 Jul 2021, 21:04

      @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 a CMakeLists.txt.
      But when I tried to create mutltiple test projects, it's more like

      MyProject/tests/QtTests/WidowClassTest
      MyProject/tests/QtTests/CanvasClassTest
      MyProject/tests/QtTests/MapClassTest
      

      Each with their own ___Test.cpp and a CMakeLists.txt.
      Is the 2nd way of doing things better?

      V Offline
      V Offline
      VRonin
      wrote on 30 Jul 2021, 09:25 last edited by VRonin
      #10

      @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/MapClassTest

      Folder structure has nothing to do with projects.
      To test classes Window, Canvas and Map, you can create a folder structure like:

      • MyProject
        • tests
          • tst_window.cpp
          • tst_canvas.cpp
          • tst_map.cpp
          • CMakeLists.txt

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      J V 3 Replies Last reply 30 Jul 2021, 09:57
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 28 Jul 2021, 19:08 last edited by
        #2

        Hi,

        Depending on how the project is structured you may have to move some stuff aground.

        In any case, there's often a dedicated tests subfolder than will contain more subfolders to keep your test code cleanly separated.

        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
        • J Offline
          J Offline
          jkwok678
          wrote on 29 Jul 2021, 08:52 last edited by
          #3

          Should I be adding the test code to my current classes?
          Or should I be using the autotest project?
          If I wanted to test my Window class method called convertMilesToMetres, should I do it this way with an auto test project?

          #include "window.h
          class WindowTest: public QObject
          {
              Q_OBJECT
          
          private:
            
          private slots:
              void initTestCase()
              {
                 Window win = new Window;
              }
          
              void testConverter()
              {
                  QCOMPARE(win.convertMilesToMetres(1), 1600); // compare two values
              }
          
          };
          
          QTEST_MAIN(WindowTest)
          #include "windowtest.moc"
          
          V 1 Reply Last reply 29 Jul 2021, 09:47
          0
          • J jkwok678
            29 Jul 2021, 08:52

            Should I be adding the test code to my current classes?
            Or should I be using the autotest project?
            If I wanted to test my Window class method called convertMilesToMetres, should I do it this way with an auto test project?

            #include "window.h
            class WindowTest: public QObject
            {
                Q_OBJECT
            
            private:
              
            private slots:
                void initTestCase()
                {
                   Window win = new Window;
                }
            
                void testConverter()
                {
                    QCOMPARE(win.convertMilesToMetres(1), 1600); // compare two values
                }
            
            };
            
            QTEST_MAIN(WindowTest)
            #include "windowtest.moc"
            
            V Offline
            V Offline
            VRonin
            wrote on 29 Jul 2021, 09:47 last edited by VRonin
            #4

            @jkwok678 said in How to add tests to an existing project?:

            Should I be adding the test code to my current classes?

            No, generally it's a bad idea. You could add special members that are needed for tests (e.g. getter methods for intermediate results you want to test) inside #ifdef blocks so you can exclude them in the deployed builds

            Or should I be using the autotest project?

            Yes, even more than one

            If I wanted to test my Window class method called convertMilesToMetres, should I do it this way with an auto test project?

            There is nothing magical inside tests, they are just normal C++ functions, treat them as such:

            #include "window.h"
            class WindowTest : public QObject
            {
                Q_OBJECT
            private slots:
                void testConverter()
                {
                    Window win;
                    QCOMPARE(win.convertMilesToMetres(1), 1600); // compare two values
                }
            };
            QTEST_MAIN(WindowTest)
            #include "windowtest.moc"
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1
            • J Offline
              J Offline
              jkwok678
              wrote on 29 Jul 2021, 13:14 last edited by
              #5

              @VRonin
              Even with this example.
              If I make a new Auto test project with this

              There is nothing magical inside tests, they are just normal C++ functions, treat them as such:
              
              #include "window.h"
              class WindowTest : public QObject
              {
                  Q_OBJECT
              private slots:
                  void testConverter()
                  {
                      Window win;
                      QCOMPARE(win.convertMilesToMetres(1), 1600); // compare two values
                  }
              };
              QTEST_MAIN(WindowTest)
              #include "windowtest.moc"
              

              How can I run it with Cmake on build?

              Also does it make sense to maybe not make a auto test poject, and just create a .cpp file with the exact same methods inside instead?

              1 Reply Last reply
              0
              • C Online
                C Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 29 Jul 2021, 13:16 last edited by
                #6

                @jkwok678 said in How to add tests to an existing project?:

                How can I run it with Cmake on build?

                You don't run autotests on build but afterwards with e.g. 'make test' or in a jenkins job.
                See also CMake documentation about add_test()

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                J 1 Reply Last reply 29 Jul 2021, 13:40
                3
                • C Christian Ehrlicher
                  29 Jul 2021, 13:16

                  @jkwok678 said in How to add tests to an existing project?:

                  How can I run it with Cmake on build?

                  You don't run autotests on build but afterwards with e.g. 'make test' or in a jenkins job.
                  See also CMake documentation about add_test()

                  J Offline
                  J Offline
                  jkwok678
                  wrote on 29 Jul 2021, 13:40 last edited by jkwok678
                  #7

                  @Christian-Ehrlicher
                  Hmm, so what exactly would I need to start automated testing?
                  Coming from a Java and JUnit background, all I had to do was make a test class, and I could create a test configuration and run that and all the tests in the test class would run whenever I liked. Is this possible with Qt Test?

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    VRonin
                    wrote on 29 Jul 2021, 16:27 last edited by
                    #8

                    If your are using CMake, the workflow is:

                    • call cmake
                      • this crates the makefile/projectfile that the compiler/linker can digest
                    • call make (cmake --build . for convenience)
                      • to actually build the projects
                    • call ctest
                      • this will run the tests that you added with add_test(). You can run specific tests by specifying the -r name_of_the_test argument

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    J 1 Reply Last reply 29 Jul 2021, 21:04
                    2
                    • V VRonin
                      29 Jul 2021, 16:27

                      If your are using CMake, the workflow is:

                      • call cmake
                        • this crates the makefile/projectfile that the compiler/linker can digest
                      • call make (cmake --build . for convenience)
                        • to actually build the projects
                      • call ctest
                        • this will run the tests that you added with add_test(). You can run specific tests by specifying the -r name_of_the_test argument
                      J Offline
                      J Offline
                      jkwok678
                      wrote on 29 Jul 2021, 21:04 last edited by jkwok678
                      #9

                      @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 a CMakeLists.txt.
                      But when I tried to create mutltiple test projects, it's more like

                      MyProject/tests/QtTests/WidowClassTest
                      MyProject/tests/QtTests/CanvasClassTest
                      MyProject/tests/QtTests/MapClassTest
                      

                      Each with their own ___Test.cpp and a CMakeLists.txt.
                      Is the 2nd way of doing things better?

                      V 1 Reply Last reply 30 Jul 2021, 09:25
                      0
                      • J jkwok678
                        29 Jul 2021, 21:04

                        @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 a CMakeLists.txt.
                        But when I tried to create mutltiple test projects, it's more like

                        MyProject/tests/QtTests/WidowClassTest
                        MyProject/tests/QtTests/CanvasClassTest
                        MyProject/tests/QtTests/MapClassTest
                        

                        Each with their own ___Test.cpp and a CMakeLists.txt.
                        Is the 2nd way of doing things better?

                        V Offline
                        V Offline
                        VRonin
                        wrote on 30 Jul 2021, 09:25 last edited by VRonin
                        #10

                        @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/MapClassTest

                        Folder structure has nothing to do with projects.
                        To test classes Window, Canvas and Map, you can create a folder structure like:

                        • MyProject
                          • tests
                            • tst_window.cpp
                            • tst_canvas.cpp
                            • tst_map.cpp
                            • CMakeLists.txt

                        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

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        J V 3 Replies Last reply 30 Jul 2021, 09:57
                        2
                        • V VRonin
                          30 Jul 2021, 09:25

                          @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/MapClassTest

                          Folder structure has nothing to do with projects.
                          To test classes Window, Canvas and Map, you can create a folder structure like:

                          • MyProject
                            • tests
                              • tst_window.cpp
                              • tst_canvas.cpp
                              • tst_map.cpp
                              • CMakeLists.txt

                          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

                          J Offline
                          J Offline
                          jkwok678
                          wrote on 30 Jul 2021, 09:57 last edited by
                          #11

                          @VRonin said in How to add tests to an existing project?:

                          BasicTest(Window)

                          What's that?
                          Is that the class name in tst_window.cpp?

                          1 Reply Last reply
                          0
                          • V VRonin
                            30 Jul 2021, 09:25

                            @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/MapClassTest

                            Folder structure has nothing to do with projects.
                            To test classes Window, Canvas and Map, you can create a folder structure like:

                            • MyProject
                              • tests
                                • tst_window.cpp
                                • tst_canvas.cpp
                                • tst_map.cpp
                                • CMakeLists.txt

                            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

                            V Offline
                            V Offline
                            VRonin
                            wrote on 30 Jul 2021, 10:00 last edited by VRonin
                            #12

                            @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 and Map

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              jkwok678
                              wrote on 30 Jul 2021, 10:07 last edited by jkwok678
                              #13

                              @VRonin
                              So is what you said earlier, with 1 test project, and 3 test files optimal?
                              MyProject - tests - tst_window.cpp, tst_map.cpp, tst_canvas.cpp

                              V 1 Reply Last reply 30 Jul 2021, 10:42
                              0
                              • J jkwok678
                                30 Jul 2021, 10:07

                                @VRonin
                                So is what you said earlier, with 1 test project, and 3 test files optimal?
                                MyProject - tests - tst_window.cpp, tst_map.cpp, tst_canvas.cpp

                                V Offline
                                V Offline
                                VRonin
                                wrote on 30 Jul 2021, 10:42 last edited by
                                #14

                                @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

                                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                ~Napoleon Bonaparte

                                On a crusade to banish setIndexWidget() from the holy land of Qt

                                J 1 Reply Last reply 30 Jul 2021, 11:24
                                3
                                • V VRonin
                                  30 Jul 2021, 10:42

                                  @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

                                  J Offline
                                  J Offline
                                  jkwok678
                                  wrote on 30 Jul 2021, 11:24 last edited by jkwok678
                                  #15

                                  @VRonin
                                  So everytime I want to test another class, I should create a new autotest project with Qt Creator?
                                  If it's not what would the process be if I wanted to test more than 1 class?

                                  V 1 Reply Last reply 30 Jul 2021, 11:43
                                  0
                                  • J jkwok678
                                    30 Jul 2021, 11:24

                                    @VRonin
                                    So everytime I want to test another class, I should create a new autotest project with Qt Creator?
                                    If it's not what would the process be if I wanted to test more than 1 class?

                                    V Offline
                                    V Offline
                                    VRonin
                                    wrote on 30 Jul 2021, 11:43 last edited by
                                    #16

                                    @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 file MyProject/tests/tst_myclass.cpp and append BasicTest(MyClass) to the snippet pasted above

                                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                    ~Napoleon Bonaparte

                                    On a crusade to banish setIndexWidget() from the holy land of Qt

                                    J 1 Reply Last reply 30 Jul 2021, 11:58
                                    0
                                    • V VRonin
                                      30 Jul 2021, 11:43

                                      @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 file MyProject/tests/tst_myclass.cpp and append BasicTest(MyClass) to the snippet pasted above

                                      J Offline
                                      J Offline
                                      jkwok678
                                      wrote on 30 Jul 2021, 11:58 last edited by
                                      #17

                                      @VRonin
                                      Ah, So it's like 1 auto test project when I start testing, and when I want to test more classes, just add a new MyClass.cpp file and add it to CmakeList?

                                      V 1 Reply Last reply 30 Jul 2021, 12:04
                                      0
                                      • J jkwok678
                                        30 Jul 2021, 11:58

                                        @VRonin
                                        Ah, So it's like 1 auto test project when I start testing, and when I want to test more classes, just add a new MyClass.cpp file and add it to CmakeList?

                                        V Offline
                                        V Offline
                                        VRonin
                                        wrote on 30 Jul 2021, 12:04 last edited by
                                        #18

                                        @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

                                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                        ~Napoleon Bonaparte

                                        On a crusade to banish setIndexWidget() from the holy land of Qt

                                        1 Reply Last reply
                                        0
                                        • V VRonin
                                          30 Jul 2021, 09:25

                                          @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/MapClassTest

                                          Folder structure has nothing to do with projects.
                                          To test classes Window, Canvas and Map, you can create a folder structure like:

                                          • MyProject
                                            • tests
                                              • tst_window.cpp
                                              • tst_canvas.cpp
                                              • tst_map.cpp
                                              • CMakeLists.txt

                                          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

                                          J Offline
                                          J Offline
                                          jkwok678
                                          wrote on 30 Jul 2021, 18:58 last edited by jkwok678
                                          #19
                                          This post is deleted!
                                          1 Reply Last reply
                                          0

                                          6/19

                                          29 Jul 2021, 13:16

                                          13 unread
                                          • Login

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