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. Qt Unit test in a project
Forum Updated to NodeBB v4.3 + New Features

Qt Unit test in a project

Scheduled Pinned Locked Moved Solved General and Desktop
31 Posts 6 Posters 15.4k Views 4 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.
  • D DavidM29

    @sierdzio
    Hi,

    I'm done with the project architecture I think. Here is what I have :

    My project : 
    	|- MyProject.pro
    	| - app
    		|- Headers
    			| classToTest.h
    			| ...
    			| ...
    		|- Sources
    			| classToTest.cpp
    			| ...
    			| ...
    		|- Resources
    			|- font.qrc
    			|- image.qrc
    			|- qml.qrc
    	| - tests
    		|- test.pro
    		|- Sources
    			|- testOne.cpp
    

    Is that all good ? (I believe it is I'm able to launch the app properly)

    Now I wonder how should I include the "classToTest.h" inside my testOne.cpp in order to test all the methods of that class.
    Can you please help me with this ?

    PS : I took a look at how you did it on your timeline project and I saw that you made a test project for each class you test. Is that a good practice, you personal choice ? Can I have only the tests project where I have a cpp dedicated to each class I want to test ?

    Thank you in advance

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #11

    @DavidM29 said in Qt Unit test in a project:

    Now I wonder how should I include the "classToTest.h" inside my testOne.cpp

    You need to set http://doc.qt.io/qt-5/qmake-variable-reference.html#includepath in your test.pro
    But this is not enough - you will need to add the cpp file as well (http://doc.qt.io/qt-5/qmake-variable-reference.html#sources)

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    D 1 Reply Last reply
    3
    • D DavidM29

      @sierdzio
      Hi,

      I'm done with the project architecture I think. Here is what I have :

      My project : 
      	|- MyProject.pro
      	| - app
      		|- Headers
      			| classToTest.h
      			| ...
      			| ...
      		|- Sources
      			| classToTest.cpp
      			| ...
      			| ...
      		|- Resources
      			|- font.qrc
      			|- image.qrc
      			|- qml.qrc
      	| - tests
      		|- test.pro
      		|- Sources
      			|- testOne.cpp
      

      Is that all good ? (I believe it is I'm able to launch the app properly)

      Now I wonder how should I include the "classToTest.h" inside my testOne.cpp in order to test all the methods of that class.
      Can you please help me with this ?

      PS : I took a look at how you did it on your timeline project and I saw that you made a test project for each class you test. Is that a good practice, you personal choice ? Can I have only the tests project where I have a cpp dedicated to each class I want to test ?

      Thank you in advance

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #12

      @DavidM29 said in Qt Unit test in a project:

      I saw that you made a test project for each class you test. Is that a good practice, you personal choice ?

      These don't necessarily exclude each other ;-)

      It is my choice. Makes management of all these test subprojects a nightmare (a clear drawback), but it also keeps all components clearly separate (so for example if I were to remove a single class, it's trivial to remove the test as well). Also, it nicely coincides with "unit" part in "unit testing" - each element is tested as a separate entity. Which in turn helps to structure the code so that is is not heavily dependent on other classes and does not evolve into a spaghetti. Also, it plays nice with Qt Creator automated test runner.

      I don't know if it is the best solution, probably not.

      (Z(:^

      1 Reply Last reply
      3
      • jsulmJ jsulm

        @DavidM29 said in Qt Unit test in a project:

        Now I wonder how should I include the "classToTest.h" inside my testOne.cpp

        You need to set http://doc.qt.io/qt-5/qmake-variable-reference.html#includepath in your test.pro
        But this is not enough - you will need to add the cpp file as well (http://doc.qt.io/qt-5/qmake-variable-reference.html#sources)

        D Offline
        D Offline
        DavidM29
        wrote on last edited by
        #13

        Thank for the answers !

        @jsulm
        Why should I include the cpp file instead of the header which are the file we usualy use to reuse function from another class ? Is that because it is a different project ?

        @sierdzio
        Ok, what do you think about my idea of keeping only one tests project with multiple cpp file corresponding to each class I want to test ?

        jsulmJ sierdzioS 2 Replies Last reply
        0
        • D DavidM29

          Thank for the answers !

          @jsulm
          Why should I include the cpp file instead of the header which are the file we usualy use to reuse function from another class ? Is that because it is a different project ?

          @sierdzio
          Ok, what do you think about my idea of keeping only one tests project with multiple cpp file corresponding to each class I want to test ?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #14

          @DavidM29 said in Qt Unit test in a project:

          Why should I include the cpp file instead of the header which are the file we usualy use to reuse function from another class ? Is that because it is a different project ?

          Because you want to test the code there, right? Your test will be an executable and header file alone is not enough (you will get "Undefined symbol" linker errors, just try).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • D DavidM29

            Thank for the answers !

            @jsulm
            Why should I include the cpp file instead of the header which are the file we usualy use to reuse function from another class ? Is that because it is a different project ?

            @sierdzio
            Ok, what do you think about my idea of keeping only one tests project with multiple cpp file corresponding to each class I want to test ?

            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #15

            @DavidM29 said in Qt Unit test in a project:

            Thank for the answers !

            @jsulm
            Why should I include the cpp file instead of the header which are the file we usualy use to reuse function from another class ? Is that because it is a different project ?

            C++ compiler needs to compile your class to use it in a test. And in order to compile, it needs the cpp file.

            @sierdzio
            Ok, what do you think about my idea of keeping only one tests project with multiple cpp file corresponding to each class I want to test ?

            Sounds good.

            (Z(:^

            1 Reply Last reply
            3
            • D Offline
              D Offline
              DavidM29
              wrote on last edited by
              #16

              Thank you to all of you it seems that I'm all set. My first try seems to works nicely.

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #17

                Nice! Happy coding :-)

                (Z(:^

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  HemantSuryawanshi
                  wrote on last edited by HemantSuryawanshi
                  #18

                  I want to do the same thing but for CMAKE project. Anyone have idea? @sierdzio

                  sierdzioS 1 Reply Last reply
                  1
                  • H HemantSuryawanshi

                    I want to do the same thing but for CMAKE project. Anyone have idea? @sierdzio

                    sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #19

                    @HemantSuryawanshi said in Qt Unit test in a project:

                    I want to do the same thing but for CMAKE project. Anyone have idea? @sierdzio

                    include(CTest)
                    add_subdirectory(tests)
                    enable_testing()
                    

                    :-) You can see some example code here: https://github.com/milosolutions/mwizardtemplate/blob/master/CMakeLists.txt

                    (Z(:^

                    1 Reply Last reply
                    3
                    • H Offline
                      H Offline
                      HemantSuryawanshi
                      wrote on last edited by
                      #20

                      I did all the things that you guys suggested and create a subdirectory project using CMake build system. Project is created successfully and then I create a class name Calculator in my src project. Now I am trying to access the methods from Calculator class into tests project to perform unit testing. But the compiler not able to find that methods in Calculator class.
                      I also added following line for linking purpose, but still its not working

                      target_include_directories(UnitTest PRIVATE ${CMAKE_SOURCE_DIR}/src)
                      

                      The compiler giving me following errors,

                      tests/CMakeFiles/UnitTest.dir/tst_unittest.cpp.obj: In function `UnitTest::test_add()':
                      C:/Hemant/Unit_Testing/Testing_subdir/tests/tst_unittest.cpp:31: undefined reference to `Calculator::Calculator(QObject*)'
                      C:/Hemant/Unit_Testing/Testing_subdir/tests/tst_unittest.cpp:32: undefined reference to `Calculator::add(int, int)'
                      tests/CMakeFiles/UnitTest.dir/tst_unittest.cpp.obj:tst_unittest.cpp:(.rdata$.refptr._ZTV10Calculator[.refptr._ZTV10Calculator]+0x0): undefined reference to `vtable for Calculator'
                      

                      Did I need to add something extra for the linking of these two subdirectories in my CMakeLists.txt files . Please help @sierdzio @jsulm

                      jsulmJ 1 Reply Last reply
                      0
                      • H HemantSuryawanshi

                        I did all the things that you guys suggested and create a subdirectory project using CMake build system. Project is created successfully and then I create a class name Calculator in my src project. Now I am trying to access the methods from Calculator class into tests project to perform unit testing. But the compiler not able to find that methods in Calculator class.
                        I also added following line for linking purpose, but still its not working

                        target_include_directories(UnitTest PRIVATE ${CMAKE_SOURCE_DIR}/src)
                        

                        The compiler giving me following errors,

                        tests/CMakeFiles/UnitTest.dir/tst_unittest.cpp.obj: In function `UnitTest::test_add()':
                        C:/Hemant/Unit_Testing/Testing_subdir/tests/tst_unittest.cpp:31: undefined reference to `Calculator::Calculator(QObject*)'
                        C:/Hemant/Unit_Testing/Testing_subdir/tests/tst_unittest.cpp:32: undefined reference to `Calculator::add(int, int)'
                        tests/CMakeFiles/UnitTest.dir/tst_unittest.cpp.obj:tst_unittest.cpp:(.rdata$.refptr._ZTV10Calculator[.refptr._ZTV10Calculator]+0x0): undefined reference to `vtable for Calculator'
                        

                        Did I need to add something extra for the linking of these two subdirectories in my CMakeLists.txt files . Please help @sierdzio @jsulm

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #21

                        @HemantSuryawanshi said in Qt Unit test in a project:

                        Did I need to add something extra for the linking of these two subdirectories in my CMakeLists.txt files

                        You need to add source files (*.cpp) you want to test.
                        As you can see from the errors you are missing the source file containing Calculator definition.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          HemantSuryawanshi
                          wrote on last edited by
                          #22

                          @jsulm are you trying to say that I also need to add the calculator class into tests project? I already have the Calculator class implemented into src project.

                          jsulmJ 1 Reply Last reply
                          0
                          • H HemantSuryawanshi

                            @jsulm are you trying to say that I also need to add the calculator class into tests project? I already have the Calculator class implemented into src project.

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #23

                            @HemantSuryawanshi said in Qt Unit test in a project:

                            are you trying to say that I also need to add the calculator class into tests project?

                            No, you need to add a reference to that file to your test project, something like:

                            add_executable(${PROJECT_NAME}
                                PATH_TO/calculator.cpp
                                tst_unittest.cpp
                                ...
                            )
                            

                            If the code you want to test compiles to a lib you could instead add a dependency to this lib.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • H Offline
                              H Offline
                              HemantSuryawanshi
                              wrote on last edited by
                              #24

                              @jsulm Its working now. Thanks for response

                              1 Reply Last reply
                              0
                              • H Offline
                                H Offline
                                HemantSuryawanshi
                                wrote on last edited by
                                #25

                                Hi, I also want to include Qt Quick Test in same project. I added required directives into project. but I have one problem that I already have the main definition and it will not take another main definition. Please tell me how cam I add

                                QUICK_TEST_MAIN(example)
                                

                                this code into main function? @jsulm

                                jsulmJ 1 Reply Last reply
                                0
                                • H HemantSuryawanshi

                                  Hi, I also want to include Qt Quick Test in same project. I added required directives into project. but I have one problem that I already have the main definition and it will not take another main definition. Please tell me how cam I add

                                  QUICK_TEST_MAIN(example)
                                  

                                  this code into main function? @jsulm

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #26

                                  @HemantSuryawanshi said in Qt Unit test in a project:

                                  I have one problem that I already have the main definition

                                  Are you trying to test a cpp file containing main()?
                                  Why?
                                  There should not be anything to test. If there is something you want to test, then move that functionality in functions/methods outside of that source file.

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  H 1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @HemantSuryawanshi said in Qt Unit test in a project:

                                    I have one problem that I already have the main definition

                                    Are you trying to test a cpp file containing main()?
                                    Why?
                                    There should not be anything to test. If there is something you want to test, then move that functionality in functions/methods outside of that source file.

                                    H Offline
                                    H Offline
                                    HemantSuryawanshi
                                    wrote on last edited by
                                    #27

                                    @jsulm No I want to test qml file which is present in my src project. And I want to integrate both cpp testing and qml testing into in single project which is tests. thats why I want to integrate that macro in main function. How can I do that, any idea?

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • H HemantSuryawanshi

                                      @jsulm No I want to test qml file which is present in my src project. And I want to integrate both cpp testing and qml testing into in single project which is tests. thats why I want to integrate that macro in main function. How can I do that, any idea?

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #28

                                      @HemantSuryawanshi said in Qt Unit test in a project:

                                      How can I do that, any idea?

                                      Make sure you have only one main() in your test project.

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      H 1 Reply Last reply
                                      0
                                      • jsulmJ jsulm

                                        @HemantSuryawanshi said in Qt Unit test in a project:

                                        How can I do that, any idea?

                                        Make sure you have only one main() in your test project.

                                        H Offline
                                        H Offline
                                        HemantSuryawanshi
                                        wrote on last edited by
                                        #29

                                        @jsulm

                                        #include <QtTest>
                                        #include <QDebug>
                                        #include <tst_unittest.cpp>
                                        #include <tst_userdata.cpp>
                                        
                                        #include <QtQuickTest/quicktest.h>
                                        QUICK_TEST_MAIN(example)
                                        
                                        int main(int argc, char** argv)
                                        {
                                           int failedTests = 0;
                                           failedTests += QTest::qExec(new UnitTest, argc, argv);
                                           failedTests += QTest::qExec(new TestUserData, argc, argv);
                                        
                                           if(failedTests > 0){
                                               qDebug() << "total number of failed tests: " << failedTests;
                                           }else{
                                               qDebug() << "all tests passed :)";
                                           }
                                           return failedTests;
                                        }
                                        
                                        

                                        This is my main file. My project contains multiple unit tests including Qt test and Qt Quick test. In this I am getting error that redefinition of main function due to the QUICK_TEST_MAIN Macro.

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • H HemantSuryawanshi

                                          @jsulm

                                          #include <QtTest>
                                          #include <QDebug>
                                          #include <tst_unittest.cpp>
                                          #include <tst_userdata.cpp>
                                          
                                          #include <QtQuickTest/quicktest.h>
                                          QUICK_TEST_MAIN(example)
                                          
                                          int main(int argc, char** argv)
                                          {
                                             int failedTests = 0;
                                             failedTests += QTest::qExec(new UnitTest, argc, argv);
                                             failedTests += QTest::qExec(new TestUserData, argc, argv);
                                          
                                             if(failedTests > 0){
                                                 qDebug() << "total number of failed tests: " << failedTests;
                                             }else{
                                                 qDebug() << "all tests passed :)";
                                             }
                                             return failedTests;
                                          }
                                          
                                          

                                          This is my main file. My project contains multiple unit tests including Qt test and Qt Quick test. In this I am getting error that redefinition of main function due to the QUICK_TEST_MAIN Macro.

                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #30

                                          @HemantSuryawanshi said in Qt Unit test in a project:

                                          I am getting error that redefinition of main function due to the QUICK_TEST_MAIN Macro

                                          Of course, you have two main() functions now.
                                          I suggest you split your C++ and Qt Quick tests.

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          H 1 Reply Last reply
                                          0

                                          • Login

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