我正在尝试对我的应用程序进行单元测试,但我不会让它工作。
我有2个.pro文件。一个在我的“主要”项目中,一个在“单元测试”中。
我的文件夹结构是:
offline_ip_adress_calculator
├───updater
│ └───updater
├───classes
│ ├───ip_calculator
│ └───threads
├───image
└───tests
└───Unittest
└───ip_calculator
Unittest .pro文件:
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
我的单元测试课程:
#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类:
#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
单元测试主
#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);
}
我收到此错误消息:
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)
我认为“ComboxOne”类无法找到父类。
答案 0 :(得分:0)
问题源于ComboxOne.cpp
未列入SOURCES
。由于项目文件本质上是一个makefile,并且构建的链接阶段基于SOURCES
,如果不存在所需的源文件,链接器将找不到符号......