What is Unit Testing ?
Unit Testing is a process in which a way to test the small pieces of code that can be logically isolated from the system. Piece of code (known as unit) may be an individual function, method, procedure, module, or object. The purpose of task is to validate each unit of the software code performs as expected. Unit Testing is done during the development phase of an application by the developers.
What is Qt Test ?
Qt Test is a unit testing framework released from the Qt developers to test the applications or libraries based on Qt framework. It provides all the functionality commonly found in unit testing frameworks as well as extensions for testing graphical user interfaces.
Create Qt Test Project
Qt Creator facilitate the wizard to create the project for unit testing. You can use the "Auto Test Project" wizard to create the Qt Test project.
- Select File > New Project > Other Project > Auto Test Project > Choose to create a project with boilerplate code for a Qt test.
- In the Project and Test Information dialog, specify settings for the project and test:
- In the Test framework field, select Qt Test.
- For a Qt test, select the GUI Application check box to create a Qt application.
- In 'Test case name' field, enter the name of the test case.
- Select the 'Requires QApplication' check box to add the include statement for QApplication to the main.cpp file of the project.
- Select the 'Generate initialization and cleanup code' checkbox to add functions to your test that are executed by the testing framework to initialize and clean up the test.
- Select the build system to use for building the project: qmake, CMake, or Qbs.
Qt Creator creates the test project in the specified project directory. Edit the .cpp file to add private slots for each test function in your test.
- initTestCase() will be called before the first test function is executed.
- initTestCase_data() will be called to create a global test data table.
- cleanupTestCase() will be called after the last test function was executed.
- init() will be called before each test function is executed.
- cleanup() will be called after every test function.
- QTest - All public methods are in the QTest namespace.
- QSignalSpy - Enables introspection for Qt's signals and slots.
- QAbstractItemModelTester - Allows for non-destructive testing of item models.