加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 运行Qt单元测试

发布时间:2020-12-16 03:45:11 所属栏目:百科 来源:网络整理
导读:查看 Qt’s site和 another Stackoverflow answer,因为我不想为每个要测试的类创建一个单独的项目,我想出了以下代码: testqstring.h #ifndef TESTQSTRING_H#define TESTQSTRING_H#include QtTest/QTestclass TestQString : public QObject{ Q_OBJECTprivate
查看 Qt’s site和 another Stackoverflow answer,因为我不想为每个要测试的类创建一个单独的项目,我想出了以下代码:

testqstring.h

#ifndef TESTQSTRING_H
#define TESTQSTRING_H

#include <QtTest/QTest>

class TestQString : public QObject
{
    Q_OBJECT

private slots:
    void toUpper();
};

#endif // TESTQSTRING_H

testqstring.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*,int)",referenced from: 
bool QTest::qCompare<QString>(QString const&,QString const&,int)in testqstring.o 
... and more like that ...

我在这做错了什么?

解决方法

加:
CONFIG += qtestlib

到.pro文件,让qmake链接到qtest库中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读