Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved How to add tests to an existing project?

    General and Desktop
    qt test google test testing
    4
    19
    1403
    Loading More Posts
    • 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
      jkwok678 last edited by

      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 Reply Quote 0
      • VRonin
        VRonin @jkwok678 last edited by VRonin

        @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 VRonin 3 Replies Last reply Reply Quote 2
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          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 Reply Quote 0
          • J
            jkwok678 last edited by

            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"
            
            VRonin 1 Reply Last reply Reply Quote 0
            • VRonin
              VRonin @jkwok678 last edited by VRonin

              @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 Reply Quote 1
              • J
                jkwok678 last edited by

                @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 Reply Quote 0
                • Christian Ehrlicher
                  Christian Ehrlicher Lifetime Qt Champion last edited by

                  @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 has to stay free or it will die.

                  J 1 Reply Last reply Reply Quote 3
                  • J
                    jkwok678 @Christian Ehrlicher last edited by jkwok678

                    @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 Reply Quote 0
                    • VRonin
                      VRonin last edited by

                      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 Reply Quote 2
                      • J
                        jkwok678 @VRonin last edited by jkwok678

                        @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?

                        VRonin 1 Reply Last reply Reply Quote 0
                        • VRonin
                          VRonin @jkwok678 last edited by VRonin

                          @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 VRonin 3 Replies Last reply Reply Quote 2
                          • J
                            jkwok678 @VRonin last edited by

                            @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 Reply Quote 0
                            • VRonin
                              VRonin @VRonin last edited by VRonin

                              @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 Reply Quote 0
                              • J
                                jkwok678 last edited by jkwok678

                                @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

                                VRonin 1 Reply Last reply Reply Quote 0
                                • VRonin
                                  VRonin @jkwok678 last edited by

                                  @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 Reply Quote 3
                                  • J
                                    jkwok678 @VRonin last edited by jkwok678

                                    @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?

                                    VRonin 1 Reply Last reply Reply Quote 0
                                    • VRonin
                                      VRonin @jkwok678 last edited by

                                      @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 Reply Quote 0
                                      • J
                                        jkwok678 @VRonin last edited by

                                        @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?

                                        VRonin 1 Reply Last reply Reply Quote 0
                                        • VRonin
                                          VRonin @jkwok678 last edited by

                                          @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 Reply Quote 0
                                          • J
                                            jkwok678 @VRonin last edited by jkwok678

                                            This post is deleted!
                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post