简体   繁体   English

运行Qt单元测试

[英]Running Qt Unit tests

Looking at Qt's site , and at another Stackoverflow answer because I don't want to create a separate project for each class I want to test, I've come up with the following code: 看看Qt的网站 ,并在另一个Stackoverflow回答,因为我不想为我想测试的每个类创建一个单独的项目,我想出了以下代码:

testqstring.h 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 testqstring.cpp

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

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

main.cpp main.cpp中

#include "testqstring.h"

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

However, I receive the following linker errors: 但是,我收到以下链接器错误:

...
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 ...

What am I doing wrong here? 我在这做错了什么?

Add: 加:

CONFIG += qtestlib 

to the .pro file to get qmake to link in the qtest library. 到.pro文件,让qmake链接到qtest库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM