Unit testing
Unit testing is an automated process for testing an individual module, class, or method in your application. Unit testing finds problems early in the development cycle. This includes both bugs in the programmer's implementation and flaws or missing parts of the specification for the unit.
Unit testing in Qt
Qt comes with a built-in unit testing module, which we can use by adding the testlib
keyword to our project file (.pro
):
QT += core gui testlib
After that, add the following header to our source code:
#include <QtTest/QtTest>
Then, we can start testing our code. We must declare our test functions as private slots. Other than that, the class must also inherit from the QOBject
class. For example, I created two text functions called testString()
and testGui()
, like so:
private slots: void testString(); void testGui();
The function definitions look something like this:
void MainWindow::testString() { QString text = "Testing"; QVERIFY(text.toUpper() == "TESTING...