Unit testing of shared library



  • I'm trying to create a test for shared library that I am creating. But without success.
    I'm getting the error: /usr/bin/ld: cannot find -ldynamicui
    I know that is probably a problem with the project files, but do not know what is.

    @
    #main .pro

    TEMPLATE = subdirs

    SUBDIRS +=
    tests
    dynamicui
    @

    @
    #-------------------------------------------------

    Project created by QtCreator 2014-06-17T14:48:09

    #-------------------------------------------------

    #dynamicui .pro

    QT += widgets

    QT -= core gui

    TARGET = dynamicui
    TEMPLATE = lib

    DEFINES += DYNAMICUI_LIBRARY

    SOURCES += dynamicui.cpp

    HEADERS += dynamicui.hpp
    dynamicui_global.hpp

    unix {
    target.path = /usr/lib
    INSTALLS += target
    }
    @

    @
    #-------------------------------------------------

    Project created by QtCreator 2014-06-17T14:31:36

    #-------------------------------------------------

    #tests .pro

    QT += widgets testlib

    QT -= gui

    TARGET = dynamicuitest
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app

    SOURCES += dynamicuitest.cpp
    main.cpp
    ../dynamicui/dynamicui.cpp
    DEFINES += SRCDIR=\"$$PWD/\"

    HEADERS +=
    dynamicuitest.hpp
    ../dynamicui/dynamicui.hpp

    win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../dynamicui/release/ -ldynamicui
    else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../dynamicui/debug/ -ldynamicui
    else:unix:!macx: LIBS += -L$$OUT_PWD/../dynamicui/ -ldynamicui

    INCLUDEPATH += $$PWD/../dynamicui
    DEPENDPATH += $$PWD/../dynamicui
    @

    @
    // dynamicui_global.hpp
    #ifndef DYNAMICUI_GLOBAL_HPP
    #define DYNAMICUI_GLOBAL_HPP

    #include <QtCore/qglobal.h>

    #if defined(DYNAMICUI_LIBRARY)

    define DYNAMICUISHARED_EXPORT Q_DECL_EXPORT

    #else

    define DYNAMICUISHARED_EXPORT Q_DECL_IMPORT

    #endif

    #endif // DYNAMICUI_GLOBAL_HPP
    @

    @
    // dynamicui.hpp

    #ifndef DYNAMICUI_HPP
    #define DYNAMICUI_HPP

    #include "dynamicui_global.hpp"
    #include <QtWidgets/QWidget>
    #include <QtUiTools/QUiLoader>
    #include <QString>
    #include <QFile>

    class DYNAMICUISHARED_EXPORT DynamicUi
    {
    public:
    explicit DynamicUi();
    ~DynamicUi();
    QWidget* createUi(const QString, QWidget* = 0);
    };

    #endif // DYNAMICUI_HPP
    @

    @
    #include "dynamicui.hpp"

    DynamicUi::DynamicUi() // Constructor
    {
    }

    DynamicUi::~DynamicUi() // Destructor
    {
    }
    QWidget* DynamicUi::createUi(const QString url, QWidget *parent)
    {
    QUiLoader loader;
    QFile ui_file(url);

    ui_file.open(QFile::ReadOnly);
    QWidget* widget = loader.load(&ui_file, parent); // Load .ui file and return pointer for object created
    ui_file.close();

    return widget;
    }
    @

    @
    #ifndef DYNAMICUITEST_H

    define DYNAMICUITEST_H

    #include <QString>
    #include <QtTest>
    #include "dynamicui.hpp"

    class DynamicUiTest : public QObject
    {
    Q_OBJECT

    public:
    DynamicUiTest();

    private Q_SLOTS:
    void testCreateUi();
    //void testCreateUi_data();
    };
    #endif
    @

    @
    #include "dynamicuitest.hpp"

    DynamicUiTest::DynamicUiTest()
    {
    }

    void DynamicUiTest::testCreateUi()
    {
    DynamicUi lui;
    QString url = "/home/matheus/Dropbox/Projetos/C++/TecTracker/TecTrackerApp/Tests/gui/test.ui";
    QCOMPARE(true, lui.createUi(url)->isWidgetType());

    //QFETCH(QString, data);
    }

    /void LoadUiTest::testCreateForm_data()
    {
    QTest::addColumn<QString>("data");
    QTest::newRow("0") << QString();
    }
    /
    @

    @
    #include "dynamicuitest.hpp"
    #include <QtWidgets/QApplication>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    DynamicUiTest tests;
    QTest::qExec(&tests, argc, argv);

    return app.exec();
    }
    @



  • Where is the library located?
    Are you sure that it is in $$OUT_PWD/../dynamicui/ ?



  • I used the Add Library function of Qt Creator, this project file was automatically generated.
    The project is divided into subdir, DynamicUi.pro, dynamicui.pro and tests.pro.
    The test project is a unit test that tests the library. I believe we should first compile the library and then compile the test. The project file of test must point to the folder where it is the compiled binary library. But do not know how to get the pro.



  • In the main pro file you listed test subdir first.
    Try to put dynamicui first and add ordered to CONFIG

    @
    SUBDIRS +=
    dynamicui \
    tests

    CONFIG += ordered
    @



  • ok, it worked

    finally, explain to me these lines of tests.pro:

    @
    DEFINES += SRCDIR=\"$$PWD/\"

    win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../dynamicui/release/ -ldynamicui
    else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../dynamicui/debug/ -ldynamicui
    else:unix:!macx: LIBS += -L$$OUT_PWD/../dynamicui/ -ldynamicui

    INCLUDEPATH += $$PWD/../dynamicui
    DEPENDPATH += $$PWD/../dynamicui
    @

    Read the documentation on creating libraries, but it was unclear.


Log in to reply
 

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