Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test large Qt project

I have a fairly large Qt GUI project, which unfortunately lacks proper unit tests.

I'm planning to have a test project with a test source file for every class.

I digged through a lot of Qt's official docs, however there are some questions left:

  • How can I create a test suite? It seems all docs and tutorials cover only writing one test, but how do I bundle my unit tests efficiently?
  • How can I integrate the test execution into Qt Creator? Maybe running the tests as post-build step would be interesting?
  • Should I add all normal source files to the test project or is there a way to create a static lib?

Any pointers to tutorials covering hint testing of a larger Qt project are highly appreciated.

like image 747
Simon Avatar asked Feb 23 '26 06:02

Simon


1 Answers

I did not have a lot of time to do perfect unit testing in Qt. I did, however, figure out how to get the main plumbing working. I hope that this can help you, although you could probably do a better job than I did by studying the Writing Unit Tests guide.

How can I create a test suite? It seems all docs and tutorials cover only writing one test, but how do I bundle my unit tests efficiently?

You can organize "suites" by collecting unit tests (which are slots on objects) into objects and composing those objects.

test_example.pro

TARGET = test_example
TEMPLATE = app
CONFIG += testcase
QT += testlib core

... INCLUDEPATH, SOURCES, HEADERS, etc...

main.cpp

#include <QtTest/QtTest>
#include "MainTest.h"

// This macro introduces an event loop
QTEST_MAIN(MainTest)

MainTest.h

class MainTest : public QObject
{
  Q_OBJECT

private slots:

  // Each slot is a unit test
  void init()
  {
  }

  // Create a "test suite" by bundling tests into objects
  void runFirstSuite()
  {
    FirstSuiteUiTest first;
    first.test();  // runs 10 tests

    FirstSuiteIoTest second;
    second.test();  // runs 999 tests

    ...
  }

  void SecondSuite() 
  {
    QFAIL("This suite is not really a suite, and it always fails");
  }
};

You can also organize tests into physical file structures. I used a subdirs project as my root .pro file so I could build artifacts (dynamic and static archives, binaries) and run many collections of unit tests.

TEMPLATE = subdirs
CONFIG += ordered

SUBDIRS += src/core
SUBDIRS += src/some_dll
SUBDIRS += src/test/awesome_core_test_1 #Holds many suites
SUBDIRS += src/test/awesome_core_test_2 #Holds many other suites!

core.depends = core
awesome_core_test_1.depends = core
awesome_core_test_2.depends = core

So by organizing unit tests into object compositions and physical files, you can some granularity of organization. There may be better or different ways, but I am not sure.

How can I integrate the test execution into Qt Creator? Maybe running the tests as post-build step would be interesting?

Add CONFIG += testcase into your .pro file. This creates a build target. Invoke the build and tests by running make check

Should I add all normal source files to the test project or is there a way to create a static lib?

You can do either, just like in any other project.