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.0k 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 Offline
    D Offline
    DavidM29
    wrote on last edited by
    #1

    Hello,

    I'm quite new to Qt and C++, I used to code in Java. I would like to add unit tests to my project but I'm not really sure that is possible.
    I tried to find a way to do that but I only found a way to create a new projet dedicated to tests.
    In Java the unit test are part of the project and I can run them directly from the project.

    Can you please clear things up to me about Unit tests and Qt project ?

    • Should I make a separate project ?

    • If yes, does it means that I'll need to duplicated every single class of my project ?

    • What are your tips/adivces to start unit tests in C++/Qt ?

    Pablo J. RoginaP 1 Reply Last reply
    1
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      The Qt build system, qmake, has a concept of "subprojects".

      So, you create a master project file with following contents:

      TEMPLATE = subdirs
      
      SUBDIRS = app tests
      

      Where "app" and "tests" are directories in the same dir where your .pro file is. Inside, there should be normal project files (with names the same as directory name), one will be your app, and the other will contain tests.

      To get easier access to same defines, files etc. in both subprojects, you can put the common definitions in a .pri file, that you then include in project files.

      I know this all sounds complicated (it is), but it's not too hard, just takes a while to get used to. Feel free to check my sTimeline project for reference.

      (Z(:^

      D 1 Reply Last reply
      10
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        http://doc.qt.io/qt-5/qtest-overview.html
        http://doc.qt.io/qt-5/qtest-tutorial.html

        "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
        4
        • D DavidM29

          Hello,

          I'm quite new to Qt and C++, I used to code in Java. I would like to add unit tests to my project but I'm not really sure that is possible.
          I tried to find a way to do that but I only found a way to create a new projet dedicated to tests.
          In Java the unit test are part of the project and I can run them directly from the project.

          Can you please clear things up to me about Unit tests and Qt project ?

          • Should I make a separate project ?

          • If yes, does it means that I'll need to duplicated every single class of my project ?

          • What are your tips/adivces to start unit tests in C++/Qt ?

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @DavidM29 in addition to the good previous replies, you may want to look at Qt MQTT module for an example of how QTestlib is used in a real product.

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

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

            Thank you for the replies !

            I'll take a look a the link you gave me and come back to you if I do have any questions about it.

            @sierdzio I believe your project is going to help me ! Thank you

            1 Reply Last reply
            0
            • sierdzioS sierdzio

              The Qt build system, qmake, has a concept of "subprojects".

              So, you create a master project file with following contents:

              TEMPLATE = subdirs
              
              SUBDIRS = app tests
              

              Where "app" and "tests" are directories in the same dir where your .pro file is. Inside, there should be normal project files (with names the same as directory name), one will be your app, and the other will contain tests.

              To get easier access to same defines, files etc. in both subprojects, you can put the common definitions in a .pri file, that you then include in project files.

              I know this all sounds complicated (it is), but it's not too hard, just takes a while to get used to. Feel free to check my sTimeline project for reference.

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

              @sierdzio

              How did you made the tests .pro file ? Did you create a separated project ? Did you made it enterily yourself ?
              The .pro files are kind of a dark zone to me I never made any change on them and don't really know how it works.
              I do have the equivalent of the app dir at the moment.
              Should I make 2 new projects ? like one for the master and one for the tests ? How do I merge 2 project into one ?

              "To get easier access to same defines, files etc. in both subprojects, you can put the common definitions in a .pri file, that you then include in project files." Can you explain me a bit more on this ? I don't know what is a .pri file and I found 2 of them in your project without understanding them.

              That is lot of questions... Sorry about that but I would like to implement test like you did. It seems to be close to Java unit tests.
              I don't want to have a separate project where I duplicate my classes to make some tests with. I feel like it is very wrong to do that.

              Pablo J. RoginaP sierdzioS 2 Replies Last reply
              0
              • D DavidM29

                @sierdzio

                How did you made the tests .pro file ? Did you create a separated project ? Did you made it enterily yourself ?
                The .pro files are kind of a dark zone to me I never made any change on them and don't really know how it works.
                I do have the equivalent of the app dir at the moment.
                Should I make 2 new projects ? like one for the master and one for the tests ? How do I merge 2 project into one ?

                "To get easier access to same defines, files etc. in both subprojects, you can put the common definitions in a .pri file, that you then include in project files." Can you explain me a bit more on this ? I don't know what is a .pri file and I found 2 of them in your project without understanding them.

                That is lot of questions... Sorry about that but I would like to implement test like you did. It seems to be close to Java unit tests.
                I don't want to have a separate project where I duplicate my classes to make some tests with. I feel like it is very wrong to do that.

                Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by
                #7

                @DavidM29

                The .pro files are kind of a dark zone

                They are just plain text files, they don't bite :-)

                Seriously, you can create them from Qt Creator as general files, or create them externally with your favorite editor and place them in the proper/expected folder.

                You may want to read about project files with qmake.

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                3
                • D DavidM29

                  @sierdzio

                  How did you made the tests .pro file ? Did you create a separated project ? Did you made it enterily yourself ?
                  The .pro files are kind of a dark zone to me I never made any change on them and don't really know how it works.
                  I do have the equivalent of the app dir at the moment.
                  Should I make 2 new projects ? like one for the master and one for the tests ? How do I merge 2 project into one ?

                  "To get easier access to same defines, files etc. in both subprojects, you can put the common definitions in a .pri file, that you then include in project files." Can you explain me a bit more on this ? I don't know what is a .pri file and I found 2 of them in your project without understanding them.

                  That is lot of questions... Sorry about that but I would like to implement test like you did. It seems to be close to Java unit tests.
                  I don't want to have a separate project where I duplicate my classes to make some tests with. I feel like it is very wrong to do that.

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

                  @DavidM29 said in Qt Unit test in a project:

                  @sierdzio

                  How did you made the tests .pro file ? Did you create a separated project ? Did you made it enterily yourself ?

                  Yes, hand made.

                  The .pro files are kind of a dark zone to me I never made any change on them and don't really know how it works.

                  You should learn the syntax, and you should get to know them more. Keeping good project files is just as important part of C++ programming as writing the C++ code itself.

                  While you're at it, it is also good to learn cmake, as it has become the de facto standard in C++ world. That one I need to learn myself, too, finally.

                  I do have the equivalent of the app dir at the moment.
                  Should I make 2 new projects ? like one for the master and one for the tests ? How do I merge 2 project into one ?

                  Unfortunately the answer is "doesn't matter" ;-) No matter how you do it, what matters is the result. I recommend writing them by hand. If you currently have your main application project file, then this is super easy. Do this:

                  • close Qt Creator
                  • remove .pro.user file from your code directory
                  • add new dir: app and move your whole project there
                  • rename app/yourProject.pro to app/app.pro
                  • add new main file in your root dir: yourProject.pro
                  • in that file, paste the code I wrote in my previous post
                  • open yourProject.pro in Qt Creator

                  That's it. You now have a working subdirs project and you can start adding tests. Not sure, maybe even from within Qt Creator (I can't check now).

                  "To get easier access to same defines, files etc. in both subprojects, you can put the common definitions in a .pri file, that you then include in project files." Can you explain me a bit more on this ? I don't know what is a .pri file and I found 2 of them in your project without understanding them.

                  Since you say you don't know how regular .pro files work, you won't understand anything about .pri files either. The closest (and quite bad) analogy I can draw for you is that .pri is like a header file in C++: you can include it in many source files (.pro) and thus avoid copy pasting.

                  I suggest taking a look at your .pro files and trying to make sense of them. Or reading about qmake in some book (or documentation), then things will become clear quickly. qmake syntax is not hard at all.

                  That is lot of questions... Sorry about that but I would like to implement test like you did. It seems to be close to Java unit tests.
                  I don't want to have a separate project where I duplicate my classes to make some tests with. I feel like it is very wrong to do that.

                  Yep.

                  (Z(:^

                  D 1 Reply Last reply
                  7
                  • sierdzioS sierdzio

                    @DavidM29 said in Qt Unit test in a project:

                    @sierdzio

                    How did you made the tests .pro file ? Did you create a separated project ? Did you made it enterily yourself ?

                    Yes, hand made.

                    The .pro files are kind of a dark zone to me I never made any change on them and don't really know how it works.

                    You should learn the syntax, and you should get to know them more. Keeping good project files is just as important part of C++ programming as writing the C++ code itself.

                    While you're at it, it is also good to learn cmake, as it has become the de facto standard in C++ world. That one I need to learn myself, too, finally.

                    I do have the equivalent of the app dir at the moment.
                    Should I make 2 new projects ? like one for the master and one for the tests ? How do I merge 2 project into one ?

                    Unfortunately the answer is "doesn't matter" ;-) No matter how you do it, what matters is the result. I recommend writing them by hand. If you currently have your main application project file, then this is super easy. Do this:

                    • close Qt Creator
                    • remove .pro.user file from your code directory
                    • add new dir: app and move your whole project there
                    • rename app/yourProject.pro to app/app.pro
                    • add new main file in your root dir: yourProject.pro
                    • in that file, paste the code I wrote in my previous post
                    • open yourProject.pro in Qt Creator

                    That's it. You now have a working subdirs project and you can start adding tests. Not sure, maybe even from within Qt Creator (I can't check now).

                    "To get easier access to same defines, files etc. in both subprojects, you can put the common definitions in a .pri file, that you then include in project files." Can you explain me a bit more on this ? I don't know what is a .pri file and I found 2 of them in your project without understanding them.

                    Since you say you don't know how regular .pro files work, you won't understand anything about .pri files either. The closest (and quite bad) analogy I can draw for you is that .pri is like a header file in C++: you can include it in many source files (.pro) and thus avoid copy pasting.

                    I suggest taking a look at your .pro files and trying to make sense of them. Or reading about qmake in some book (or documentation), then things will become clear quickly. qmake syntax is not hard at all.

                    That is lot of questions... Sorry about that but I would like to implement test like you did. It seems to be close to Java unit tests.
                    I don't want to have a separate project where I duplicate my classes to make some tests with. I feel like it is very wrong to do that.

                    Yep.

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

                    @sierdzio
                    Thank you for all the details, I'll probably will have some more question but fisrt I have a bit of work of understanding evrything properly.

                    @Pablo-J-Rogina Thank you for the link on .pro files

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DavidM29
                      wrote on last edited by DavidM29
                      #10

                      @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 sierdzioS 2 Replies Last reply
                      1
                      • 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 Online
                        jsulmJ Online
                        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 Online
                              jsulmJ Online
                              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

                                          • Login

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