[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/
      +++ myproject.pro
      ++++ controllers/
      +++++ heating/
      +++++ heatingcontroller.cpp
      +++++ heatingcontroller.h
      +++++ heatingonoffcontroller.cpp
      +++++ heatingonoffcontroller.h

    ++ UnitTestProject/
    +++ trunk/
    +++ unittestproject.pro
    ++++ controllers/
    +++++ heating/
    +++++ ut_heatingonoff.cpp
    +++++ ut_heatingonoff.h

    unittestproject.pro looks like this:

    @QT += testlib
    QT -= gui

    TARGET = tst_unittestproject
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app

    INCLUDEPATH +=
    ../../MyProject/trunk/

    DEPENDPATH +=
    ../../MyProject/trunk/

    SOURCES +=
    tst_unittestproject.cpp
    heating/ut_heatingonoff.cpp

    HEADERS +=
    tst_unittestproject.h
    heating/ut_heatingonoff.h

    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
    {

    CPPUNIT_TEST_SUITE(utHeatingOnOffController);
    CPPUNIT_TEST(testTestTest);
    CPPUNIT_TEST_SUITE_END();
    

    public:
    void setUp(void);
    void tearDown(void);

    private:
    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;
    }

    CPPUNIT_TEST_SUITE_REGISTRATION(utHeatingOnOffController);@

    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

    Hi,

    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 UnitTestProject.pro?


  • Lifetime Qt Champion

    Use the lib template and in your unit test add

    @
    LIBS +=
    -Lpath_where_the_lib_file_from_MyProject_can_be_found
    -lMyProject
    @

    and you should be good



  • YESYESYES!!!
    It works!!!
    Thanks SGaist!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.