3

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.

Simon
  • 1,616
  • 2
  • 17
  • 39

2 Answers2

3

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.

0

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?

In "Qt unit test class" each private slot is a unit test. Not exists other standard ways for adding new tests, but exist non documentary qmake feature. You can read about in this forum thread

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

You can add CONFIG+=testcase test project and this will generate additional make target - check. Of course, you can add this build step anywhere. Also, you can read about here - Writing_Unit_Tests

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

You can create static or dynamic lib with sources and link them to test project, or you can add sources to test project like here

By the way, you can watch example of unit testing with Qt here

Community
  • 1
  • 1
synacker
  • 1,722
  • 13
  • 32
  • This is the right answer. I would only add the possibility of including the library files to the test project (for example, doing `SOURCES += ../lib/*cpp`) as an alternative to testing the library API. – Яois Mar 23 '15 at 21:04
  • 2
    `In Qt unit test class the each private slots is a unit test` - say what? – dtech Mar 23 '15 at 21:05
  • 1
    You don't say "the each" - just "each" or "each of the". I know it is not an "English language" site but still, English is primary here and yours needs polishing. I suspect you do literal translation from your native language, but that is just not proper English. I am still not entirely sure what most of the sentences in that answer say, I will have to "guess". – dtech Mar 23 '15 at 21:10
  • 2
    @ddriver yes, my english level not enough for you, but I'm trying. Thank you for attention to my post. – synacker Mar 23 '15 at 21:16