Important: Please read the Qt Code of Conduct -

[SOLVED] Linking Unit Test to MyProject

  • Hello,

    I'm trying to make an Unit Test (using CppUnit) for MyProject.
    I will describe the situation first.

    I made 2 separated projects: MyProject and UnitTestProject.
    The hierarchy looks like this:

    • Basefolder/
      ++ MyProject/
      +++ trunk/
      ++++ controllers/
      +++++ heating/
      +++++ heatingcontroller.cpp
      +++++ heatingcontroller.h
      +++++ heatingonoffcontroller.cpp
      +++++ heatingonoffcontroller.h

    ++ UnitTestProject/
    +++ trunk/
    ++++ controllers/
    +++++ heating/
    +++++ ut_heatingonoff.cpp
    +++++ ut_heatingonoff.h looks like this:

    @QT += testlib
    QT -= gui

    TARGET = tst_unittestproject
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app



    SOURCES +=

    HEADERS +=

    unix|win32: LIBS += -lcppunit@

    In heatingonoffcontroller.h is a class named HeatingOnOffController
    In ut_heatingonoff.h I made a class like this:
    #include "controllers/heating/heatingcontroller.h"
    #include "controllers/heating/heatingonoffcontroller.h"

    class utHeatingOnOffController : public CppUnit::TestFixture


    void setUp(void);
    void tearDown(void);

    HeatingOnOffController *TestObjectHeatingOnOffController;

    In ut_heatingonoffcontroller.cpp I made an implementation like this:

    #include "controllers/heating/heatingcontroller.h"
    #include "controllers/heating/heatingonoffcontroller.h"
    #include "heating/ut_heatingonoffcontroller.h"

    using namespace CppUnit;
    using namespace std;

    void utHeatingOnOffController::testTestTest(void)


    void utHeatingOnOffController::setUp(void)
    TestObjectHeatingOnOffController = new HeatingOnOffController(NULL,0,0);

    void utHeatingOnOffController::tearDown(void)
    delete TestObjectHeatingOnOffController;


    The strange thing is that QtCreator recognizes HeatingOnOffController, but gives an error at this line:

    @TestObjectHeatingOnOffController = new HeatingOnOffController(NULL,0,0);@

    I get the following error:

    In function 'void utHeatingOnOffController::setUp(void)':
    undefined reference to 'HeatingOnOffController::HeatingOnOffController(QObject*,int,int)'
    collect2: error: Id returned 1 exit status

    I think it has something to do with the link between the 2 projects, but I don't know how to do that correct in the .pro files.
    Does anybody know what I'm doing wrong? I'm struggling with it for days!

  • Lifetime Qt Champion


    You don't link your unit test to your MyProject library

  • Hi,

    thanks for the reaction!
    That's what I thought, but how do I do that?
    Qt is kind of new to me.

    Btw: Is it an option for me to create an library file of MyProject and include that in

  • Lifetime Qt Champion

    Use the lib template and in your unit test add

    LIBS +=

    and you should be good

    It works!!!
    Thanks SGaist!

Log in to reply