Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Linking Unit Test to MyProject
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Linking Unit Test to MyProject

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nielske87
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Nielske87
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • N Offline
            N Offline
            Nielske87
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved