Introduction to Qt Test

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.

Creating a Test
Creating a test, subclass QObject and add one or more private slots in your test class. Each private slot is a test function in your test. In addition, you can define the following private slots that are not treated as test functions. When present, they will be executed by the testing framework and can be used to initialize and clean up either the entire test or the current test function.

  • 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.

Every test should leave the system in a usable state, so it can be run repeatedly. Cleanup operations should be handled in cleanupTestCase(), so they get run even if the test fails.

QTest Namespace
Qt Test Framework provides QTest namespace and QAbstractItemModelTester, QSignalSpy classes to facilities various functions and declarations for unit testing. 

  • 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.
Finally....
I am so exhausted with lots of theory. So lets break it here and we meet in the next session with some interesting examples for better understanding. Bye...