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 use an auto test project to test another project
Forum Updated to NodeBB v4.3 + New Features

How to use an auto test project to test another project

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 582 Views 1 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 Offline
    J Offline
    jkwok678
    wrote on last edited by
    #1

    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 the map.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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You should show the content of your CMakefile.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        You should show the content of your CMakefile.

        J Offline
        J Offline
        jkwok678
        wrote on last edited by jkwok678
        #3

        @SGaist
        Here it is for my window.h, but same problem

        cmake_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)
        
        
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Why are you just linking QtTest ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          0
          • SGaistS SGaist

            Why are you just linking QtTest ?

            J Offline
            J Offline
            jkwok678
            wrote on last edited by
            #5

            @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"
            
            
            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              What's the main project? a library or an executable?

              "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
              0
              • VRoninV VRonin

                What's the main project? a library or an executable?

                J Offline
                J Offline
                jkwok678
                wrote on last edited by
                #7

                @VRonin
                Hmm, an executable, it's meant to be a GUI application.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #8

                  You have 2 solutions:

                  1. Add the source files of the main application to the test project (add_executable(WindowFunction tst_windowfunction.cpp ../window.h ../window.cpp))
                  2. 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

                  "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
                  2
                  • VRoninV VRonin

                    You have 2 solutions:

                    1. Add the source files of the main application to the test project (add_executable(WindowFunction tst_windowfunction.cpp ../window.h ../window.cpp))
                    2. 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

                    J Offline
                    J Offline
                    jkwok678
                    wrote on last edited by
                    #9

                    @VRonin

                    Hmmm, was this the intended way to build a project and tests from the devs?
                    If not how was it meant to be?

                    VRoninV 1 Reply Last reply
                    0
                    • J jkwok678

                      @VRonin

                      Hmmm, was this the intended way to build a project and tests from the devs?
                      If not how was it meant to be?

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      @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

                      "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

                      • Login

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