當您使用以下標誌:
LIBS += -L"$$OUT_PWD/../HelloWorld/release"
LIBS += -lHelloWorld
您必須編譯動態或靜態庫。因此您必須創建一個生成庫的項目。在下一部分中,我向您展示如何創建一個動態庫。
HelloWorldLib.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-01-06T12:37:49
#
#-------------------------------------------------
QT -= gui
TARGET = HelloWorldLib
TEMPLATE = lib
DEFINES += HELLOWORLDLIB_LIBRARY
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
INCLUDEPATH += $$PWD/include
SOURCES += src/helloworldlib.cpp
HEADERS += include/helloworldlib.h\
include/helloworldlib_global.h
unix {
target.path = /usr/lib
INSTALLS += target
}
DESTDIR = $$PWD/lib
包括/ helloworldlib.h
#ifndef HELLOWORLDLIB_H
#define HELLOWORLDLIB_H
#include "helloworldlib_global.h"
#include <QDebug>
class HELLOWORLDLIBSHARED_EXPORT HelloWorldLib: public QObject
{
Q_OBJECT
public:
HelloWorldLib(){
}
static bool returnTrue()
{
return true;
}
public slots:
void someSlot()
{
qDebug() << "test";
}
};
#endif // HELLOWORLDLIB_H
包括/ helloworldlib_global.h
#ifndef HELLOWORLDLIB_GLOBAL_H
#define HELLOWORLDLIB_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(HELLOWORLDLIB_LIBRARY)
# define HELLOWORLDLIBSHARED_EXPORT Q_DECL_EXPORT
#else
# define HELLOWORLDLIBSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // HELLOWORLDLIB_GLOBAL_H
的src/helloworldlib.cpp
#include "helloworldlib.h"
在這裏,我展示了測試項目。
HelloWorldTest.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-01-06T12:42:42
#
#-------------------------------------------------
QT += testlib
QT -= gui
# greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = tst_helloworldtesttest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += tst_helloworldtesttest.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../HelloWorldLib/lib/release/ -lHelloWorldLib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../HelloWorldLib/lib/debug/ -lHelloWorldLib
else:unix: LIBS += -L$$PWD/../HelloWorldLib/lib/ -lHelloWorldLib
INCLUDEPATH += $$PWD/../HelloWorldLib/include
DEPENDPATH += $$PWD/../HelloWorldLib/include
tst_helloworldtesttest。CPP
#include <QString>
#include <QtTest>
#include <helloworldlib.h>
#include <QDebug>
class HelloWorldTestTest : public QObject
{
Q_OBJECT
public:
HelloWorldTestTest();
private Q_SLOTS:
void testCase1_data();
void testCase1();
};
HelloWorldTestTest::HelloWorldTestTest()
{
}
void HelloWorldTestTest::testCase1_data()
{
QTest::addColumn<QString>("data");
QTest::newRow("0") << QString();
}
void HelloWorldTestTest::testCase1()
{
QFETCH(QString, data);
QVERIFY2(true, "Failure");
HelloWorldLib hw;
QVERIFY(hw.returnTrue());
}
QTEST_APPLESS_MAIN(HelloWorldTestTest)
#include "tst_helloworldtesttest.moc"
輸出:
********* Start testing of HelloWorldTestTest *********
Config: Using QtTest library 5.7.1, Qt 5.7.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 6.2.1 20160830)
PASS : HelloWorldTestTest::initTestCase()
PASS : HelloWorldTestTest::testCase1(0)
PASS : HelloWorldTestTest::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 0ms
********* Finished testing of HelloWorldTestTest *********
在下面的環節是完整的項目:https://github.com/eyllanesc/stackoverflow/tree/master/QtUnitTestingTest
我查閱了您的項目,我知道你要測試的項目的HelloWorld,內該項目有2個類:HelloWorld和MainWindow。我不明白你爲什麼要鏈接一個你還沒有創建的庫。除了HelloWorld類從未使用過。究竟你想測試什麼(HelloWorld類或MainWindow類)? – eyllanesc
在這個模擬的例子中,'HelloWorld'。我正在測試'returnsTrue'函數,它確實返回'true'。構建該項目會創建HelloWorld.lib,儘管我不確定是否需要進行單元測試,因爲源代碼仍然存在。 – quimnuss
如果有必要構建庫,因爲在.pro中寫入LIBS + = -lHelloWorld。我已經在一段時間內完成了測試,我將上傳代碼。 – eyllanesc