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 project 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/advice's to start unit tests in C /Qt ?
CodePudding user response:
you can use Google Test
, I recommend you to see this link.
Qt Creator integrates the following testing frameworks for unit testing applications and libraries:
Boost.Test Catch2 test framework Google C Testing Framework Qt Test framework
Also, this YouTube Video is about qt googletest overview :
https://www.youtube.com/watch?v=hMriR-fFkZo
CodePudding user response:
Building and running unit tests means to build another executable (typically a console app). So you have to set up your build so that you are able to build another executable alongside your main executable. I feel must warn you, if you are a beginner with C , this will be VERY hard, you will spend probably several sleepless nights and your eyes will be swollen with tears of desperation and you will start to hate C . C is hard. Incomparably harder than Java.
There are a multiple options, two of them seem to be the most convenient:
Create another project (PRO file if you are using QMAKE) and add your existing classes (those which you want to test) to it, then add some testing classes (those which contain the unit tests, have a look e.g. at QtTest framework) and then add
main.cpp
which would call these unit tests. You should put your unittesting classes and unittestingmain.cpp
to a directory separated from the normal classes which you share with the main application in order not to mix these up and to avoid name clashes. In other words, you just will have two projects next to each other. The main drawback is that you will need to add every class which you want to test into two projects - them main app and the test app projects. You can remedy this problem by using a shared project data file (PRI file if you are using QMAKE). Another drawback is the build time - to build the main application and the test application, you will need to build the main classes twice, in each project separately.Another way is to split your project into three parts. You will put all the classes you want to test into a static library. Then you add a main application project which will link this static library. And then you add a unittest application project which will also link to this static library. So you will have three projects - one static library and two applications, one main application, the other will be the unit test application. The advantage of this approach is that once you set everything up, the management of such project will be much easier and build time will be faster because the major part of the application will be in the static library which will be built only once and then reused in the main app and in the unittest app. To set up such a build use SUBDIRS, see https://wiki.qt.io/SUBDIRS_-_handling_dependencies