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:
- C++ unit testing with Qt Test – part 1 – introduction
- C++ unit testing with Qt Test – part 2 – advanced testing
- GUI unit testing with Qt Test – part 1 – introduction
- GUI unit testing with Qt Test – part 2 – advanced testing
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.
The wizard will guide you through the setup of the project. In particular the Details section will let you specify several
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().
Why, what’s wrong with Qt Test?
1234567890
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
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.
The differences are explained in part 2 of this series.