Search code examples
qtunit-testingqmake

how to unit test a Qt application with QtTest using subdirs


I found a few other hits when searching for this particular problem, but i cant get my setup to work with my unit test project(s).

project info: ( optional reading)

I am in the beginning of my final undergraduate project, where I am to create an IDE for the language Promula and the the verification tool Spin. The professor I am doing this with expects to use it in his classes and for future undergraduates to keep improving. It is also meant to be a open source project everyone can contribute to.

Spin tool

my folder struture:

QSpin
-QSpin.pro
-QSpinApplication
--QSpinapplication.pro
--QSpinApplication.pri
--include
---Workspace
----*.h
--src
---Workspace
----*.cpp
-QSpinApplicationTests
--QSpinApplicationTests.pro
--WorkspaceGroupTests
---WorkspaceGroupTests.pro
----some cpp testfile

My QSpinApplication compiles and runs without any problems. The import of what i have so can be imported.

#include <Workspace/(file_name).h> 

However in my test project WorkspaceGroupTests i want to include headers in the same way, but i cant get it to work. Beside that i assume the test project need to know where the cpp files are. If i dont use any of the application headers the test project compiles and runs with the default test case. at the moment i am using a default shadow build with its root folder in the QSpin folder.

Any idea of how i get my test project working correctly, to actual be able to test my application classes?

QSpin.pro:

TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += \
    QSpinApplication\QSpinApplication.pro \
    QSpinApplicationTests

QSpinApplication.pro:

QT += quick qml gui
CONFIG += c++11

DEFINES += QT_DEPRECATED_WARNINGS
SOURCES +=src/main.cpp
include(QSpinApplication.pri)
RESOURCES += qml.qrc

qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

QSpinApplication.pri:

#//DEPENDPATH += $$PWD
INCLUDEPATH += ./include
SOURCES +=src/workspace/workspaceview.cpp \
    src/workspace/groupitem.cpp \
    src/workspace/groupitemsplitter.cpp \
    src/workspace/workspacegroup.cpp \
    src/workspace/workspaceviewattached.cpp \
    src/workspace/workspaceviewiteminfo.cpp \
    src/workspace/workspacegrouplinknode.cpp

HEADERS += \
    include/workspace/workspaceview.h \
    include/workspace/groupitem.h \
    include/workspace/groupitemsplitter.h \
    include/workspace/workspacegroup.h \
    include/workspace/workspaceviewattached.h \
    include/workspace/workspace.h \
    include/workspace/workspaceviewiteminfo.h \
    include/workspace/workspacegrouplinknode.h

QSpinApplicationTests.pro:

TEMPLATE = subdirs
SUBDIRS += \
    WorkspaceGroupTests

WorkspaceGroupTests.pro:

QT += testlib
QT -= gui
DEPENDPATH+=./../../QSpinApplication
include(./../../QSpinApplication/QSpinApplication.pri)
CONFIG += qt console warn_on depend_includepath testcase
CONFIG -= app_bundle
TEMPLATE = app
SOURCES +=  tst_workspacegrouptests.cpp

Solution

  • Not sure if I understood well but in general, what I would advise is to create a separate structure for "lib" where you put logic code that can be tested, so default structure would look like that:

    Structure:
     ├───app.pro
     ├───lib.pro
     └───tests.pro 
    

    Then you put in your lib.pro:

    TARGET = NameOfTarget
    TEMPLATE = lib
    CONFIG += staticlib
    

    and add in your tests.pro, and in app.pro:

    LIBS += -L../lib -lNameOfTarget
    

    At end it should be possible without problems to include in app directory and in test:

    #include "anyHeaderFromLib.h" .