Adding a Unit Test for Existing CT Creator Application
-
wrote on 23 Aug 2023, 09:56 last edited by
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
-
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
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).
-
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).
wrote on 24 Aug 2023, 08:00 last edited by@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!
-
@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!
The cleanest and simple way is to refactor your project as suggested above.
-
wrote on 25 Aug 2023, 05:44 last edited by Radio1985
@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.
Below pictures shows the three .pro files created by Qt creator automatically.
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.
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.
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!!
-
@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.
Below pictures shows the three .pro files created by Qt creator automatically.
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.
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.
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!!
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.
-
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.
1/7