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. Adding a Unit Test for Existing CT Creator Application
Forum Updated to NodeBB v4.3 + New Features

Adding a Unit Test for Existing CT Creator Application

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 818 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.
  • R Offline
    R Offline
    Radio1985
    wrote on 23 Aug 2023, 09:56 last edited by
    #1

    Hi All,

    I have an existing QT Creator application.
    It consists of some of my business logic related logic Classes and also QT UI related classes.
    Now I want to add some unit tests to test my Classes.

    How should I add unit test cases? Should I create a new Test Case from project->Add New and then add a test frame work (ex: Google Test, QT Quick Test, Boost Test ..etc)?

    I could also add an Auto Test Project. But I can create this only outside my current project. I am not sure how to link this to my existing project. Coz I need to include the test project into my same git version control and should be able to access my Class header files.

    In general what is the best approach for adding unit tests? Can we use Google Test to test QT ui related classes or should I use QT Test Framework?

    Thank you

    S 1 Reply Last reply 23 Aug 2023, 18:20
    0
    • R Radio1985
      23 Aug 2023, 09:56

      Hi All,

      I have an existing QT Creator application.
      It consists of some of my business logic related logic Classes and also QT UI related classes.
      Now I want to add some unit tests to test my Classes.

      How should I add unit test cases? Should I create a new Test Case from project->Add New and then add a test frame work (ex: Google Test, QT Quick Test, Boost Test ..etc)?

      I could also add an Auto Test Project. But I can create this only outside my current project. I am not sure how to link this to my existing project. Coz I need to include the test project into my same git version control and should be able to access my Class header files.

      In general what is the best approach for adding unit tests? Can we use Google Test to test QT ui related classes or should I use QT Test Framework?

      Thank you

      S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 Aug 2023, 18:20 last edited by
      #2

      Hi,

      It depends on your requirements. Since you are using Qt for your application, then making use of the QtTest module means that you do not have an additional external dependency.

      That said, for the inclusion of tests, if using qmake, you can use the SUBDIRS template, create all application components in a library in one project, the application itself in another one and finally all the tests in a third project so you can ensure that all components of your library are working as expected. You can also do higher level tests such as integration tests and not just unit tests.

      Another way is for the library to also be a SUBDIRS project and put there the unit tests for the library and the integration tests a level higher.

      This latter option allows you to keep a better separation especially if you create several libraries (for example one that contains only widgets and another one business logic that you use in one or more applications built in your project).

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

      R 1 Reply Last reply 24 Aug 2023, 08:00
      0
      • S SGaist
        23 Aug 2023, 18:20

        Hi,

        It depends on your requirements. Since you are using Qt for your application, then making use of the QtTest module means that you do not have an additional external dependency.

        That said, for the inclusion of tests, if using qmake, you can use the SUBDIRS template, create all application components in a library in one project, the application itself in another one and finally all the tests in a third project so you can ensure that all components of your library are working as expected. You can also do higher level tests such as integration tests and not just unit tests.

        Another way is for the library to also be a SUBDIRS project and put there the unit tests for the library and the integration tests a level higher.

        This latter option allows you to keep a better separation especially if you create several libraries (for example one that contains only widgets and another one business logic that you use in one or more applications built in your project).

        R Offline
        R Offline
        Radio1985
        wrote on 24 Aug 2023, 08:00 last edited by
        #3

        @SGaist
        Thanks for the reply. At the moment I already have an existing QT Creator ui application project. In this project I have added few new .cpp files with my classes.
        Starting from here, I want to add a unit test project. In this case I first created a subproject using QT creator and then added an automated test project.
        Now how can I access all my classes in my main application project to create the test cases?

        Thanks!

        S 1 Reply Last reply 24 Aug 2023, 20:09
        0
        • R Radio1985
          24 Aug 2023, 08:00

          @SGaist
          Thanks for the reply. At the moment I already have an existing QT Creator ui application project. In this project I have added few new .cpp files with my classes.
          Starting from here, I want to add a unit test project. In this case I first created a subproject using QT creator and then added an automated test project.
          Now how can I access all my classes in my main application project to create the test cases?

          Thanks!

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 24 Aug 2023, 20:09 last edited by
          #4

          The cleanest and simple way is to refactor your project as suggested above.

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

          R 1 Reply Last reply 25 Aug 2023, 05:44
          0
          • S SGaist
            24 Aug 2023, 20:09

            The cleanest and simple way is to refactor your project as suggested above.

            R Offline
            R Offline
            Radio1985
            wrote on 25 Aug 2023, 05:44 last edited by Radio1985
            #5

            @SGaist
            Hi Thanks for the reply. So I created a sub project called (TestSubDir) uisng QT creator where my Main application (QTWidgetTest) and my unit test project (AutoTest) are inside this sub project. My Project structure looks as below.
            715564b6-cade-4372-a5ba-c657b2f82e28-image.png

            Below pictures shows the three .pro files created by Qt creator automatically.
            ec37a370-8b60-453f-a3f0-f5779bfe432c-image.png

            59cc77fa-c6a3-42e2-a63c-f67b0e8c32bc-image.png

            c9b24081-1516-4c70-bd3f-f5ff493e51b9-image.png

            Now I wanted to test one of my functions in myclass.h file. I have included myclass.h file as shown above in my first picture. When I tried to run the test project I get the following error.

            ef98b066-d9a8-4a2f-9378-3bac3d7beef7-image.png

            To solve the above issue, I added my myclass.cpp and myclass.h file in AUtoTest.pro file as below. This helped to compile my test application and my test ran as well. But as you can see below, it actually automatically creates a copy of my QTWidgetTest myclass.h and myclass.cpp files inside AuotoTest project.

            392ef572-af11-4f83-a60f-c41bc483a798-image.png

            This was generated automatically. Is this what it should be happen? Or am I doing it incorrectly, is there is a better way of integrating the my QTWidgetTest project with the test project?

            Thank a lot!!

            S 1 Reply Last reply 25 Aug 2023, 21:22
            0
            • R Radio1985
              25 Aug 2023, 05:44

              @SGaist
              Hi Thanks for the reply. So I created a sub project called (TestSubDir) uisng QT creator where my Main application (QTWidgetTest) and my unit test project (AutoTest) are inside this sub project. My Project structure looks as below.
              715564b6-cade-4372-a5ba-c657b2f82e28-image.png

              Below pictures shows the three .pro files created by Qt creator automatically.
              ec37a370-8b60-453f-a3f0-f5779bfe432c-image.png

              59cc77fa-c6a3-42e2-a63c-f67b0e8c32bc-image.png

              c9b24081-1516-4c70-bd3f-f5ff493e51b9-image.png

              Now I wanted to test one of my functions in myclass.h file. I have included myclass.h file as shown above in my first picture. When I tried to run the test project I get the following error.

              ef98b066-d9a8-4a2f-9378-3bac3d7beef7-image.png

              To solve the above issue, I added my myclass.cpp and myclass.h file in AUtoTest.pro file as below. This helped to compile my test application and my test ran as well. But as you can see below, it actually automatically creates a copy of my QTWidgetTest myclass.h and myclass.cpp files inside AuotoTest project.

              392ef572-af11-4f83-a60f-c41bc483a798-image.png

              This was generated automatically. Is this what it should be happen? Or am I doing it incorrectly, is there is a better way of integrating the my QTWidgetTest project with the test project?

              Thank a lot!!

              S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 25 Aug 2023, 21:22 last edited by
              #6

              It did not generate anything. Take a look at the actual content of your project folders.

              What you are seeing is the Project view, it shows what your current project is using.

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

              R 1 Reply Last reply 29 Aug 2023, 07:58
              0
              • S SGaist
                25 Aug 2023, 21:22

                It did not generate anything. Take a look at the actual content of your project folders.

                What you are seeing is the Project view, it shows what your current project is using.

                R Offline
                R Offline
                Radio1985
                wrote on 29 Aug 2023, 07:58 last edited by
                #7

                @SGaist
                Thanks, yeah it does not generate new files.

                1 Reply Last reply
                0

                1/7

                23 Aug 2023, 09:56

                • Login

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