QT单元测试MoC“未解析的外部符号”

qui

我正在尝试第一次将单元测试添加到我的项目中。

我可以运行模拟测试(不使用我项目的类),然后运行应用程序。但是,如果我从项目中实例化对象,则会得到一个未解析的QMetaObject外部符号。如果我没记错的话,这意味着该项目中没有包含该对象的Moc。

我该如何解决?我在使用googletests时遇到了同样的问题。该指南对此也无济于事。我试过安装qt单元测试插件,结果相同。

我上传了一个模拟项目,该项目遵循与上述项目相同的结构,请在此处获取:https : //github.com/quimnuss/QtUnitTestingTest

我在Windows上使用的是qt的静态版本,但是我想这是不礼貌的。使用QtCreator作为IDE和NMAke构建。

我也尝试添加HelloWorld.lib,但不使用Makefile.release。

有人知道我在做什么错吗?

这是.pro单元测试:

QT       += widgets network testlib

TARGET = tst_someunittesttest
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

INCLUDEPATH += $$PWD/../HelloWorld

include($$PWD/../HelloWorld/helloworldCommon.pri)

LIBS += -L"$$OUT_PWD/../HelloWorld/release"
LIBS += -lHelloWorld

message("Searching libs here $$LIBS")

SOURCES += tst_someunittesttest.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"

第一个错误的完整消息:

tst_someunittesttest.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl HelloWorld::metaObject(void)const " (?metaObject@HelloWorld@@UEBAPEBUQMetaObject@@XZ)
乙二醛

当您使用以下标志时:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Qt未解析的外部符号LNK2019

来自分类Dev

Qt 4.8.5 QVariant未解析的外部符号Visual Studio 2012

来自分类Dev

单元测试未运行

来自分类Dev

未解析的外部符号CLRCreateInstance

来自分类Dev

opencv:未解析的外部符号

来自分类Dev

yyparse()的未解析外部符号

来自分类Dev

未解析的外部符号_stricoll

来自分类Dev

SOIL未解析的外部符号

来自分类Dev

未解析的外部符号_stricoll

来自分类Dev

未解析的外部符号CLRCreateInstance

来自分类Dev

未解析的外部符号 - GRPC

来自分类Dev

从外部库中排除单元测试

来自分类Dev

在单元测试中调用外部函数

来自分类Dev

AngularJS单元测试在带有外部模板的自定义指令中解析Promise

来自分类Dev

未构建 subdir 项目中的单元测试 [Qt/C++]

来自分类Dev

大型Qt项目的单元测试

来自分类Dev

Android单元测试未嘲笑

来自分类Dev

Xcode 5:单元测试未运行

来自分类Dev

ngAnimate单元测试未添加类

来自分类Dev

Resharper单元测试未运行

来自分类Dev

单元测试服务未注入工厂

来自分类Dev

rxjava 单元测试 onError 未调用

来自分类Dev

Python:单元测试未运行

来自分类Dev

文本解析器的单元测试

来自分类Dev

Qt C ++ LNK2019:使用QNetworkAccessManager的未解析外部符号

来自分类Dev

Qt:Windows函数是无法解析的外部符号

来自分类Dev

Qt单元测试-仅显示失败的测试

来自分类Dev

xcodebuild测试未运行XCTestCase单元测试文件

来自分类Dev

单元测试未执行时,鼻子测试失败