I am trying to unit test my application but i wont get it work.
I habe 2 .pro files. One in my "main" project and one in "Unittest".
My Folder structure is :
offline_ip_adress_calculator
├───updater
│ └───updater
├───classes
│ ├───ip_calculator
│ └───threads
├───image
└───tests
└───Unittest
└───ip_calculator
Unittest .pro file:
TargetDirectory = C:\build\unit_test
CONFIG(debug, debug|release) {
DESTDIR = $$TargetDirectory\debug
OBJECTS_DIR = $$TargetDirectory\debug\.obj
MOC_DIR = $$TargetDirectory\debug\.moc
RCC_DIR = $$TargetDirectory\debug\.rcc
UI_DIR = $$TargetDirectory\debug\.ui
} else {
DESTDIR = $$TargetDirectory\release
OBJECTS_DIR = $$TargetDirectory\release\.obj
MOC_DIR = $$TargetDirectory\release\.moc
RCC_DIR = $$TargetDirectory\release\.rcc
UI_DIR = $$TargetDirectory\release\.ui
}
QT += testlib
QT += core
INSTALLS += target sources
CONFIG += c++11
TARGET = maintest
# Change this Path
INCLUDEPATH += X:/ip_adress_calculator/offline_ip_adress_calculator
TEMPLATE = app
SOURCES += \
qtestqstring.cpp \
ip_calculator/qttestcomboxone.cpp \
main.cpp
HEADERS += \
qtestqstring.h \
ip_calculator/qttestcomboxone.h
My unit test class:
#ifndef QTTESTCOMBOXONE_H
#define QTTESTCOMBOXONE_H
#include <QObject>
#include <QtTest/QTest>
#include "classes/ip_calculator/comboxone.h"
class QtTestComboxOne : public QObject
{
Q_OBJECT
private slots:
void QtTestCalculateIndustrialSwitchOne();
public:
explicit QtTestComboxOne(QObject *parent = 0);
signals:
public slots:
};
#endif // QTTESTCOMBOXONE_H
#include "qttestcomboxone.h"
QtTestComboxOne::QtTestComboxOne(QObject *parent) :
QObject(parent)
{
}
void QtTestComboxOne::QtTestCalculateIndustrialSwitchOne()
{
ComboxOne combox_one;
combox_one.setCommunicationNumber(1);
QCOMPARE(combox_one.calculate(QString("industrial_switch_1")), QString("10.0.20.2"));
}
ComboxOne class:
#ifndef COMBOXONE_H
#define COMBOXONE_H
#include "abstractipcalculator.h"
class ComboxOne : public AbstractIpCalculator
{
public:
ComboxOne();
QString calculate(QString);
private:
QString calculateOctetOne(QString);
QString calculateOctetTwo(QString);
QString calculateOctetThree(QString);
QString calculateOctetFour(QString);
};
#endif // COMBOXONE_H
COmboxOne cpp:
#include "comboxone.h"
ComboxOne::ComboxOne()
{
data_structure.enqueue(QPair<QString, QString>("industrial_switch_1", "Industrial Switch I"));
}
QString ComboxOne::calculate(QString name)
{
QString return_value;
return_value.append(calculateOctetOne(name));
return_value.append(".");
return_value.append(calculateOctetTwo(name));
return_value.append(".");
return_value.append(calculateOctetThree(name));
return_value.append(".");
return_value.append(calculateOctetFour(name));
return return_value;
}
QString ComboxOne::calculateOctetOne(QString name)
{
return [...];
}
QString ComboxOne::calculateOctetTwo(QString name)
{
[...]
return QString::number(temp_res);
}
QString ComboxOne::calculateOctetThree(QString name)
{
[...]
return QString::number(temp_res);
}
QString ComboxOne::calculateOctetFour(QString name)
{
[...]
return QString("10");
}
AbstractIpCalculator
#ifndef ABSTRACTIPCALCULATOR_H
#define ABSTRACTIPCALCULATOR_H
#include <QString>
#include <QDebug>
#include <QPair>
#include <QQueue>
class AbstractIpCalculator
{
public:
QQueue<QPair<QString,QString> > data_structure;
AbstractIpCalculator();
bool setParkNetzId(int index, int value);
void setCommunicationNumber(int communication_number);
virtual QString calculate(QString) = 0;
protected:
QMap<int, int> park_netz_list;
int communication_number;
int getParkNetId();
static const int bla_1 = 360;
static const int bal_2 = 363;
private:
virtual QString calculateOctetOne(QString) = 0;
virtual QString calculateOctetTwo(QString) = 0;
virtual QString calculateOctetThree(QString) = 0;
virtual QString calculateOctetFour(QString) = 0;
};
#endif // ABSTRACTIPCALCULATOR_H
Unit test Main
#include <QTest>
#include "qtestqstring.h"
#include "ip_calculator/qttestcomboxone.h"
int main()
{
QTestQString string_test;
QtTestComboxOne combox_one;
QTest::qExec(&string_test);
QTest::qExec(&combox_one);
}
I get this error messages:
qttestcomboxone.obj:-1: Fehler:LNK2019: unresolved external symbol "public: virtual class QString __thiscall ComboxOne::calculate(class QString)" (?calculate@ComboxOne@@UAE?AVQString@@V2@@Z) referenced in function "private: void __thiscall QtTestComboxOne::QtTestCalculateIndustrialSwitchOne(void)" (?QtTestCalculateIndustrialSwitchOne@QtTestComboxOne@@AAEXXZ)
qttestcomboxone.obj:-1: Fehler:LNK2019: unresolved external symbol "public: void __thiscall AbstractIpCalculator::setCommunicationNumber(int)" (?setCommunicationNumber@AbstractIpCalculator@@QAEXH@Z) referenced in function "private: void __thiscall QtTestComboxOne::QtTestCalculateIndustrialSwitchOne(void)" (?QtTestCalculateIndustrialSwitchOne@QtTestComboxOne@@AAEXXZ)
qttestcomboxone.obj:-1: Fehler:LNK2019: unresolved external symbol "public: bool __thiscall AbstractIpCalculator::setParkNetzId(int,int)" (?setParkNetzId@AbstractIpCalculator@@QAE_NHH@Z) referenced in function "private: void __thiscall QtTestComboxOne::QtTestCalculateIndustrialSwitchOne(void)" (?QtTestCalculateIndustrialSwitchOne@QtTestComboxOne@@AAEXXZ)
I think the class "ComboxOne" can't find the parent class.