运行 Qt 单元测试

2023-12-27

看着,并且在另一个 Stackoverflow 答案 https://stackoverflow.com/questions/1524390/qt-what-unit-testing-framework/3804999#3804999因为我不想为我想要测试的每个类创建一个单独的项目,所以我想出了以下代码:

测试字符串.h

#ifndef TESTQSTRING_H
#define TESTQSTRING_H

#include <QtTest/QTest>

class TestQString : public QObject
{
    Q_OBJECT

private slots:
    void toUpper();
};

#endif // TESTQSTRING_H

测试qstring.cpp

#include "testqstring.h"
#include <QString>

void TestQString::toUpper()
{
    QString str = "Hello";
    QCOMPARE(str.toUpper(), QString("HELLO"));
}

main.cpp

#include "testqstring.h"

int main(int argc, char *argv[])
{
    TestQString testqstring;
    QTest::qExec(&testqstring, argc, argv);
    return 0;
}

但是,我收到以下链接器错误:

...
g++ -headerpad_max_install_names -arch i386 -o tester main.o testqstring.o moc_testqstring.o -F/Library/Frameworks -L/Library/Frameworks -framework QtCore 
Undefined symbols: 
"QTest::qExec(QObject*, int, char**)", referenced from: 
_main in main.o 
"QTest::compare_helper(bool, char const*, char*, char*, char const*, char const*, char const*, int)", referenced from: 
bool QTest::qCompare<QString>(QString const&, QString const&, char const*, char const*, char const*, int)in testqstring.o 
... and more like that ...

我在这里做错了什么?


Add:

CONFIG += qtestlib 

到 .pro 文件以使 qmake 链接到 qtest 库中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

运行 Qt 单元测试 的相关文章

随机推荐