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?
Forum Updated to NodeBB v4.3 + New Features

How to add tests to an existing project?

Scheduled Pinned Locked Moved Solved General and Desktop
qt testgoogle testtesting
19 Posts 4 Posters 4.7k Views 2 Watching
  • 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
    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

                      19/19

                      30 Jul 2021, 18:58

                      • Login

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