C++ unit testing with Qt Test – part 1 – introduction

This tutorial is an introduction to C++ unit testing with Qt Test. A working example is discussed and analysed in detail. Full qmake project and C++ source code are provided.

C++ unit testing with Qt Test

Qt Test is a framework for C++ unit testing. It is part of Qt, which means it includes features to test Qt GUIs and other Qt elements like signals, but it can also be used to test plain (no Qt) C++ code.

In this tutorial I am going to explain how to use Qt Test for testing a C++ class. That will involve creating a project, defining the unit test and using the different macros available to test code.

This is the first post of a series dedicated to Qt Test. The posts of this series are:

Setting up the project

The idea behind Qt Test is that each test case needs to be an independent executable and needs its own project.

The quickest way to create a project is using the “Auto Test Project” template, which is listed in the “Other Project” group of the “New Project” dialog.

Creating an Auto Test Project in Qt Creator for C++ unit testing with Qt Test

The wizard will guide you through the setup of the project. In particular the Details section will let you specify several

6 Comments

  1. Mli Mber

    I have an extensive test suite in Qt Test. If I had to do it over again, I would use Gtest or Catch or similar for the superior testing infrastructure at use QTest for things like mouseButtonClick().

    Reply
    1. Alex

      Why, what’s wrong with Qt Test?

      Reply
      1. Max Mustermann

        1234567890

        Reply
  2. Daniele

    Hi, i need a help. I’m trying to test, with google test, my c++ project wich use Qt. I have a Q_OBJECT class, derived like this : ( class Observer is an abstract class )

    // Register.h

    class Register : public QMainWindow, public Observer {
    Q_OBJECT
    Register(QWidget* parent = nullptr)
    /….more code here /
    }

    My issue is when i try to write the TEST function
    in RegisterTest.cpp class :

    //RegisterTest.cpp

    TEST( testcaseName, testName) {
    …test body..

    could you suggest please, here, how to write a Test for the constructor or in general how to istance tha class Register ?

    }

    thank you for your help,
    regards
    Daniele

    Reply
  3. Hareen Laks

    Thanks for your article.
    I was wondering what is the difference between Qt Unit Test Project and Qt Autotest Project.

    I’ve checked them before. The default code on the IDE is different. But functionalities are more or less same.

    Then I was wondering why there are two selections.

    Reply
    1. Davide Coppola (Post author)

      The differences are explained in part 2 of this series.

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *