Qt

Create Qt Project with Qt Unit tests

The Qt Test philosophy is that every test case is an independent executable. The best way to run and manage them is creating a “parent” project.

First make sure that test view is enabled:

  • Help > About Plugins…
  • Select Utilities > AutoTest checkbox
  • Restart Qt Creator

Create project with subprojects:

  • File > New File or Project > Other Project > Subdirs Project
    • Name = Wereld2
    • Create in D:/Work
    • Application > Qt Console Application
    • Name = Application
    • Create in D:/Work/Wereld2
  • Right Mouse click project > New Subproject… > Other Project > Qt Unit Test
    • Name = TestFormatText
    • Create in D:/Work/Wereld2
    • Class name = FormatTextTest
    • Test slot = testCase1
  • Right Mouse click project > New Subproject… > Other Project > Qt Unit Test
    • Name = TestStellarClassification
    • Create in D:/Work/Wereld2
    • Class name = StellarClassificationTest
    • Test slot = testCase1

This will result in a Kit selector with 3 build configurations (Debug, Profile, and Release), each with 3 run configurations (Application, TestFormatText, TestStellarClassification). The Project panel shows the project hierarchy existing of a the parent project Wereld2 and 3 subprojects. The test panel shows the two test classes with their test slot.

Add source files or a library to the test project?

You can add either the source files or a library with the code to test to the test project (see Stack Overflow). An example in which the business logic is moved into a library can be found Stack Overflow. An example that include the source code can be found Stack Overflow and at GitHub.

Add source files

Source files can be added by:

  • Right mouse click on “Sources” in Projects panel
  • Select “Add Existing File…”

This will add a relative path in the .pro file.
The alternative is to edit the .pro file and add the manually:

APPSRCDIR = ../app
INCLUDEPATH += $$APPSRCDIR
SOURCES += formattexttest.cpp \
    $$APPSRCDIR/formattext.cpp

Delete subproject

  • Remove the line from the main .pro file where the subprojects are listed
  • run qmake
  • Delete subproject files

References