微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 运行Qt单元测试

查看 Qt’s siteanother 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库中.

原文地址:https://www.jb51.cc/c/110322.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐